diff --git a/refurb/checks/readability/no_len_cmp.py b/refurb/checks/readability/no_len_cmp.py index 39a587b..c3e41e3 100644 --- a/refurb/checks/readability/no_len_cmp.py +++ b/refurb/checks/readability/no_len_cmp.py @@ -17,11 +17,19 @@ NameExpr, Node, OpExpr, + TupleExpr, UnaryExpr, WhileStmt, ) -from refurb.checks.common import is_mapping, is_sized, stringify +from refurb.checks.common import ( + get_mypy_type, + is_mapping, + is_same_type, + is_sized, + mypy_type_to_python_type, + stringify, +) from refurb.error import Error from refurb.visitor import METHOD_NODE_MAPPINGS, TraverserVisitor @@ -139,8 +147,19 @@ def visit_comparison_expr(self, node: ComparisonExpr) -> None: case ComparisonExpr( operators=["==" | "!=" as oper], - operands=[lhs, (ListExpr(items=[]) | DictExpr(items=[]))], - ) if is_sized(lhs): + operands=[ + lhs, + ( + ListExpr(items=[]) + | DictExpr(items=[]) + | TupleExpr(items=[]) + | CallExpr( + callee=NameExpr(fullname="builtins.set" | "builtins.frozenset"), + args=[], + ) + ) as rhs, + ], + ) if is_same_type(get_mypy_type(lhs), mypy_type_to_python_type(get_mypy_type(rhs))): old = stringify(node) new = stringify(lhs) diff --git a/test/data/err_115.py b/test/data/err_115.py index cbfcf5f..79a405b 100644 --- a/test/data/err_115.py +++ b/test/data/err_115.py @@ -98,6 +98,11 @@ def mapping_check(m: Mapping[str, str]): assert len(authors) == 0 +assert fruits == frozenset() +assert primes == set() +assert data == () + + # these should not if len(nums) == 1: ... @@ -127,3 +132,7 @@ def __len__(self) -> int: assert nums <= [] assert len(nums) % 2 + +assert data == [] +assert data == {} +assert data == set() diff --git a/test/data/err_115.txt b/test/data/err_115.txt index f7f5af7..0449234 100644 --- a/test/data/err_115.txt +++ b/test/data/err_115.txt @@ -45,3 +45,6 @@ test/data/err_115.py:95:8 [FURB115]: Replace `len(list(authors)) == 0` with `not test/data/err_115.py:96:8 [FURB115]: Replace `len(authors.keys()) == 0` with `not authors` test/data/err_115.py:97:8 [FURB115]: Replace `len(authors.values()) == 0` with `not authors` test/data/err_115.py:98:8 [FURB115]: Replace `len(authors) == 0` with `not authors` +test/data/err_115.py:101:8 [FURB115]: Replace `fruits == frozenset()` with `not fruits` +test/data/err_115.py:102:8 [FURB115]: Replace `primes == set()` with `not primes` +test/data/err_115.py:103:8 [FURB115]: Replace `data == ()` with `not data`