[pylint] Fix false negatives for ascii and sorted in len-as-condition (PLC1802) (#14692)

This commit is contained in:
Simon Brugman 2024-11-30 21:10:30 +01:00 committed by GitHub
parent be07424e80
commit 56ae73a925
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 41 additions and 0 deletions

View file

@ -232,3 +232,7 @@ def j():
x = [1, 2, 3]
if len(x): # [PLC1802] should be fine
print(x)
# regression tests for https://github.com/astral-sh/ruff/issues/14690
bool(len(ascii(1)))
bool(len(sorted("")))

View file

@ -186,6 +186,8 @@ fn is_sequence(expr: &Expr, semantic: &SemanticModel) -> bool {
| "hex"
| "memoryview"
| "oct"
| "ascii"
| "sorted"
)
})
}

View file

@ -434,3 +434,38 @@ len_as_condition.py:233:8: PLC1802 [*] `len(x)` used as condition without compar
233 |- if len(x): # [PLC1802] should be fine
233 |+ if x: # [PLC1802] should be fine
234 234 | print(x)
235 235 |
236 236 | # regression tests for https://github.com/astral-sh/ruff/issues/14690
len_as_condition.py:237:6: PLC1802 [*] `len(ascii(1))` used as condition without comparison
|
236 | # regression tests for https://github.com/astral-sh/ruff/issues/14690
237 | bool(len(ascii(1)))
| ^^^^^^^^^^^^^ PLC1802
238 | bool(len(sorted("")))
|
= help: Remove `len`
Safe fix
234 234 | print(x)
235 235 |
236 236 | # regression tests for https://github.com/astral-sh/ruff/issues/14690
237 |-bool(len(ascii(1)))
237 |+bool(ascii(1))
238 238 | bool(len(sorted("")))
len_as_condition.py:238:6: PLC1802 [*] `len(sorted(""))` used as condition without comparison
|
236 | # regression tests for https://github.com/astral-sh/ruff/issues/14690
237 | bool(len(ascii(1)))
238 | bool(len(sorted("")))
| ^^^^^^^^^^^^^^^ PLC1802
|
= help: Remove `len`
Safe fix
235 235 |
236 236 | # regression tests for https://github.com/astral-sh/ruff/issues/14690
237 237 | bool(len(ascii(1)))
238 |-bool(len(sorted("")))
238 |+bool(sorted(""))