Skip to content

Commit

Permalink
[pylint] Fix false negatives for ascii and sorted in `len-as-co…
Browse files Browse the repository at this point in the history
…ndition` (PLC1802) (#14692)
  • Loading branch information
sbrugman authored Nov 30, 2024
1 parent be07424 commit 56ae73a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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("")))
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/pylint/rules/len_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ fn is_sequence(expr: &Expr, semantic: &SemanticModel) -> bool {
| "hex"
| "memoryview"
| "oct"
| "ascii"
| "sorted"
)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))

0 comments on commit 56ae73a

Please sign in to comment.