Skip to content

Commit

Permalink
[refurb] Add test for for-loop-set-mutations lint
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-700 committed Mar 26, 2024
1 parent c970c74 commit 02fa99e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB142.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
for x in (1, 2, 3):
s.add(num)

for x in (1, 2, 3):
s.add((num, x))

for x in (1, 2, 3):
s.add(x + num)

# False negative

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ FURB142.py:25:1: FURB142 [*] Use of `set.add()` in a for loop
25 | / for x in (1, 2, 3):
26 | | s.add(num)
| |______________^ FURB142
27 |
28 | for x in (1, 2, 3):
|
= help: Replace with `.update()`

Expand All @@ -157,5 +159,51 @@ FURB142.py:25:1: FURB142 [*] Use of `set.add()` in a for loop
26 |- s.add(num)
25 |+s.update(num for x in (1, 2, 3))
27 26 |
28 27 |
29 28 | # False negative
28 27 | for x in (1, 2, 3):
29 28 | s.add((num, x))

FURB142.py:28:1: FURB142 [*] Use of `set.add()` in a for loop
|
26 | s.add(num)
27 |
28 | / for x in (1, 2, 3):
29 | | s.add((num, x))
| |___________________^ FURB142
30 |
31 | for x in (1, 2, 3):
|
= help: Replace with `.update()`

Safe fix
25 25 | for x in (1, 2, 3):
26 26 | s.add(num)
27 27 |
28 |-for x in (1, 2, 3):
29 |- s.add((num, x))
28 |+s.update((num, x) for x in (1, 2, 3))
30 29 |
31 30 | for x in (1, 2, 3):
32 31 | s.add(x + num)

FURB142.py:31:1: FURB142 [*] Use of `set.add()` in a for loop
|
29 | s.add((num, x))
30 |
31 | / for x in (1, 2, 3):
32 | | s.add(x + num)
| |__________________^ FURB142
33 |
34 | # False negative
|
= help: Replace with `.update()`

Safe fix
28 28 | for x in (1, 2, 3):
29 29 | s.add((num, x))
30 30 |
31 |-for x in (1, 2, 3):
32 |- s.add(x + num)
31 |+s.update(x + num for x in (1, 2, 3))
33 32 |
34 33 | # False negative
35 34 |

0 comments on commit 02fa99e

Please sign in to comment.