Skip to content

Commit

Permalink
Fix FURB115 being emitted when using len(x) with non-boolean operat…
Browse files Browse the repository at this point in the history
…ors (#319):

Closes #318.
  • Loading branch information
dosisod authored Dec 22, 2023
1 parent fc48b56 commit 0755650
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions refurb/checks/readability/no_len_cmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def inner(self: "LenComparisonVisitor", _: Node) -> None:

setattr(self, name, inner.__get__(self))

def visit_op_expr(self, o: OpExpr) -> None:
if o.op in {"and", "or"}:
super().visit_op_expr(o)

def visit_comparison_expr(self, node: ComparisonExpr) -> None:
match node:
case ComparisonExpr(
Expand Down
5 changes: 5 additions & 0 deletions test/data/err_115.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
assert authors == {}
assert authors != {}

assert len(nums) and True
assert len(nums) or False


# these should not

Expand Down Expand Up @@ -98,3 +101,5 @@ def __len__(self) -> int:
assert nums == [1, 2, 3]
assert authors == {"author": "book"}
assert nums <= []

assert len(nums) % 2
2 changes: 2 additions & 0 deletions test/data/err_115.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ test/data/err_115.py:67:8 [FURB115]: Replace `x == []` with `not x`
test/data/err_115.py:68:8 [FURB115]: Replace `x != []` with `x`
test/data/err_115.py:70:8 [FURB115]: Replace `x == {}` with `not x`
test/data/err_115.py:71:8 [FURB115]: Replace `x != {}` with `x`
test/data/err_115.py:73:8 [FURB115]: Replace `len(x)` with `x`
test/data/err_115.py:74:8 [FURB115]: Replace `len(x)` with `x`

0 comments on commit 0755650

Please sign in to comment.