From 9a6556c53603717eeacec3f99a6deba0cac45560 Mon Sep 17 00:00:00 2001 From: dosisod <39638017+dosisod@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:36:48 -0800 Subject: [PATCH] Fix FURB115 being emitted when using `len(x)` with non-boolean operators: Closes #318. --- refurb/checks/readability/no_len_cmp.py | 4 ++++ test/data/err_115.py | 5 +++++ test/data/err_115.txt | 2 ++ 3 files changed, 11 insertions(+) diff --git a/refurb/checks/readability/no_len_cmp.py b/refurb/checks/readability/no_len_cmp.py index 2ecd225..83995a4 100644 --- a/refurb/checks/readability/no_len_cmp.py +++ b/refurb/checks/readability/no_len_cmp.py @@ -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( diff --git a/test/data/err_115.py b/test/data/err_115.py index cec354c..f929a79 100644 --- a/test/data/err_115.py +++ b/test/data/err_115.py @@ -70,6 +70,9 @@ assert authors == {} assert authors != {} +assert len(nums) and True +assert len(nums) or False + # these should not @@ -98,3 +101,5 @@ def __len__(self) -> int: assert nums == [1, 2, 3] assert authors == {"author": "book"} assert nums <= [] + +assert len(nums) % 2 diff --git a/test/data/err_115.txt b/test/data/err_115.txt index e0a1f34..327f80f 100644 --- a/test/data/err_115.txt +++ b/test/data/err_115.txt @@ -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`