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

[refurb] Add coverage for using set(...) in single-item-membership-test (FURB171) #15793

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB171.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

if 1 in {1}:
print("Single-element set")

if 1 in set(1):
print("Single-element set")
Comment on lines +12 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, this fails at runtime:

>>> set(1)
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    set(1)
    ~~~^^^
TypeError: 'int' object is not iterable


if "a" in "a":
print("Single-element string")
Expand All @@ -29,6 +32,9 @@
if 1 in {1, 2}:
pass

if 1 in set(1, 2):
pass
Comment on lines +35 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also fails at runtime:

>>> set(1, 2)
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    set(1, 2)
    ~~~^^^^^^
TypeError: set expected at most 1 argument, got 2


if "a" in "ab":
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ fn single_item(expr: &Expr) -> Option<&Expr> {
[item] => Some(item),
_ => None,
},
Expr::Call(ast::ExprCall {
func,
arguments,
range: _,
}) => {
if let Expr::Name(ast::ExprName {
id,
ctx: _,
range: _,
}) = func.as_ref()
{
if id == "set" && arguments.args.len() == 1 {
return Some(&arguments.args[0]);
}
}
None
}
string_expr @ Expr::StringLiteral(ExprStringLiteral { value: string, .. })
if string.chars().count() == 1 =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,65 +57,85 @@ FURB171.py:9:4: FURB171 [*] Membership test against single-item container
9 |-if 1 in {1}:
9 |+if 1 == 1:
10 10 | print("Single-element set")
11 11 |
12 12 | if "a" in "a":
11 11 |
12 12 | if 1 in set(1):

FURB171.py:12:4: FURB171 [*] Membership test against single-item container
|
10 | print("Single-element set")
11 |
12 | if "a" in "a":
| ^^^^^^^^^^ FURB171
13 | print("Single-element string")
11 |
12 | if 1 in set(1):
| ^^^^^^^^^^^ FURB171
13 | print("Single-element set")
|
= help: Convert to equality test

Unsafe fix
Safe fix
9 9 | if 1 in {1}:
10 10 | print("Single-element set")
11 11 |
12 |-if "a" in "a":
12 |+if "a" == "a":
13 13 | print("Single-element string")
11 11 |
12 |-if 1 in set(1):
12 |+if 1 == (1):
13 13 | print("Single-element set")
14 14 |
15 15 | if 1 not in (1,):
15 15 | if "a" in "a":

FURB171.py:15:4: FURB171 [*] Membership test against single-item container
|
13 | print("Single-element string")
13 | print("Single-element set")
14 |
15 | if 1 not in (1,):
15 | if "a" in "a":
| ^^^^^^^^^^ FURB171
16 | print("Single-element string")
|
= help: Convert to equality test

ℹ Unsafe fix
12 12 | if 1 in set(1):
13 13 | print("Single-element set")
14 14 |
15 |-if "a" in "a":
15 |+if "a" == "a":
16 16 | print("Single-element string")
17 17 |
18 18 | if 1 not in (1,):

FURB171.py:18:4: FURB171 [*] Membership test against single-item container
|
16 | print("Single-element string")
17 |
18 | if 1 not in (1,):
| ^^^^^^^^^^^^^ FURB171
16 | print("Check `not in` membership test")
19 | print("Check `not in` membership test")
|
= help: Convert to inequality test

ℹ Safe fix
12 12 | if "a" in "a":
13 13 | print("Single-element string")
14 14 |
15 |-if 1 not in (1,):
15 |+if 1 != 1:
16 16 | print("Check `not in` membership test")
15 15 | if "a" in "a":
16 16 | print("Single-element string")
17 17 |
18 18 | if not 1 in (1,):
18 |-if 1 not in (1,):
18 |+if 1 != 1:
19 19 | print("Check `not in` membership test")
20 20 |
21 21 | if not 1 in (1,):

FURB171.py:18:8: FURB171 [*] Membership test against single-item container
FURB171.py:21:8: FURB171 [*] Membership test against single-item container
|
16 | print("Check `not in` membership test")
17 |
18 | if not 1 in (1,):
19 | print("Check `not in` membership test")
20 |
21 | if not 1 in (1,):
| ^^^^^^^^^ FURB171
19 | print("Check the negated membership test")
22 | print("Check the negated membership test")
|
= help: Convert to equality test

ℹ Safe fix
15 15 | if 1 not in (1,):
16 16 | print("Check `not in` membership test")
17 17 |
18 |-if not 1 in (1,):
18 |+if not 1 == 1:
19 19 | print("Check the negated membership test")
18 18 | if 1 not in (1,):
19 19 | print("Check `not in` membership test")
20 20 |
21 21 | # Non-errors.
21 |-if not 1 in (1,):
21 |+if not 1 == 1:
22 22 | print("Check the negated membership test")
23 23 |
24 24 | # Non-errors.
Loading