Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FURB115 being emitted when using len(x) with non-boolean operators #319

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`