Skip to content

Commit

Permalink
F841: support fixing unused assignments in tuples by renaming variables
Browse files Browse the repository at this point in the history
  • Loading branch information
sciyoshi committed Dec 12, 2023
1 parent b972455 commit 762a5aa
Show file tree
Hide file tree
Showing 5 changed files with 1,167 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
Some(Fix::unsafe_edit(edit).isolate(isolation))
};
}
} else {
let name = binding.name(checker.locator());
let renamed = format!("_{}", name);
if checker.settings.dummy_variable_rgx.is_match(&renamed) {
let edit = Edit::range_replacement(renamed, binding.range());

return Some(Fix::unsafe_edit(edit).isolate(isolation));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
assertion_line: 161
---
F841_0.py:3:22: F841 [*] Local variable `e` is assigned to but never used
|
1 | try:
2 | 1 / 0
3 | except ValueError as e:
| ^ F841
4 | pass
|
= help: Remove assignment to unused variable `e`

ℹ Safe fix
1 1 | try:
2 2 | 1 / 0
3 |-except ValueError as e:
3 |+except ValueError:
4 4 | pass
5 5 |
6 6 |

F841_0.py:16:5: F841 [*] Local variable `z` is assigned to but never used
|
14 | x = 1
15 | y = 2
16 | z = x + y
| ^ F841
|
= help: Remove assignment to unused variable `z`

ℹ Unsafe fix
13 13 | def f():
14 14 | x = 1
15 15 | y = 2
16 |- z = x + y
16 |+ x + y
17 17 |
18 18 |
19 19 | def f():

F841_0.py:20:5: F841 [*] Local variable `foo` is assigned to but never used
|
19 | def f():
20 | foo = (1, 2)
| ^^^ F841
21 | (a, b) = (1, 2)
|
= help: Remove assignment to unused variable `foo`

ℹ Unsafe fix
17 17 |
18 18 |
19 19 | def f():
20 |- foo = (1, 2)
21 20 | (a, b) = (1, 2)
22 21 |
23 22 | bar = (1, 2)

F841_0.py:21:6: F841 [*] Local variable `a` is assigned to but never used
|
19 | def f():
20 | foo = (1, 2)
21 | (a, b) = (1, 2)
| ^ F841
22 |
23 | bar = (1, 2)
|
= help: Remove assignment to unused variable `a`

ℹ Unsafe fix
18 18 |
19 19 | def f():
20 20 | foo = (1, 2)
21 |- (a, b) = (1, 2)
21 |+ (_a, b) = (1, 2)
22 22 |
23 23 | bar = (1, 2)
24 24 | (c, d) = bar

F841_0.py:21:9: F841 [*] Local variable `b` is assigned to but never used
|
19 | def f():
20 | foo = (1, 2)
21 | (a, b) = (1, 2)
| ^ F841
22 |
23 | bar = (1, 2)
|
= help: Remove assignment to unused variable `b`

ℹ Unsafe fix
18 18 |
19 19 | def f():
20 20 | foo = (1, 2)
21 |- (a, b) = (1, 2)
21 |+ (a, _b) = (1, 2)
22 22 |
23 23 | bar = (1, 2)
24 24 | (c, d) = bar

F841_0.py:26:14: F841 [*] Local variable `baz` is assigned to but never used
|
24 | (c, d) = bar
25 |
26 | (x, y) = baz = bar
| ^^^ F841
|
= help: Remove assignment to unused variable `baz`

ℹ Unsafe fix
23 23 | bar = (1, 2)
24 24 | (c, d) = bar
25 25 |
26 |- (x, y) = baz = bar
26 |+ (x, y) = bar
27 27 |
28 28 |
29 29 | def f():

F841_0.py:51:9: F841 [*] Local variable `b` is assigned to but never used
|
49 | def c():
50 | # F841
51 | b = 1
| ^ F841
52 |
53 | def d():
|
= help: Remove assignment to unused variable `b`

ℹ Unsafe fix
48 48 |
49 49 | def c():
50 50 | # F841
51 |- b = 1
51 |+ pass
52 52 |
53 53 | def d():
54 54 | nonlocal b

F841_0.py:79:26: F841 [*] Local variable `my_file` is assigned to but never used
|
78 | def f():
79 | with open("file") as my_file, open("") as ((this, that)):
| ^^^^^^^ F841
80 | print("hello")
|
= help: Remove assignment to unused variable `my_file`

ℹ Unsafe fix
76 76 |
77 77 |
78 78 | def f():
79 |- with open("file") as my_file, open("") as ((this, that)):
79 |+ with open("file"), open("") as ((this, that)):
80 80 | print("hello")
81 81 |
82 82 |

F841_0.py:85:25: F841 [*] Local variable `my_file` is assigned to but never used
|
83 | def f():
84 | with (
85 | open("file") as my_file,
| ^^^^^^^ F841
86 | open("") as ((this, that)),
87 | ):
|
= help: Remove assignment to unused variable `my_file`

ℹ Unsafe fix
82 82 |
83 83 | def f():
84 84 | with (
85 |- open("file") as my_file,
85 |+ open("file"),
86 86 | open("") as ((this, that)),
87 87 | ):
88 88 | print("hello")

F841_0.py:102:5: F841 [*] Local variable `msg3` is assigned to but never used
|
100 | msg1 = "Hello, world!"
101 | msg2 = "Hello, world!"
102 | msg3 = "Hello, world!"
| ^^^^ F841
103 | match x:
104 | case 1:
|
= help: Remove assignment to unused variable `msg3`

ℹ Unsafe fix
99 99 | def f(x: int):
100 100 | msg1 = "Hello, world!"
101 101 | msg2 = "Hello, world!"
102 |- msg3 = "Hello, world!"
103 102 | match x:
104 103 | case 1:
105 104 | print(msg1)

F841_0.py:115:5: F841 [*] Local variable `Baz` is assigned to but never used
|
113 | Foo = enum.Enum("Foo", "A B")
114 | Bar = enum.Enum("Bar", "A B")
115 | Baz = enum.Enum("Baz", "A B")
| ^^^ F841
116 |
117 | match x:
|
= help: Remove assignment to unused variable `Baz`

ℹ Unsafe fix
112 112 |
113 113 | Foo = enum.Enum("Foo", "A B")
114 114 | Bar = enum.Enum("Bar", "A B")
115 |- Baz = enum.Enum("Baz", "A B")
115 |+ enum.Enum("Baz", "A B")
116 116 |
117 117 | match x:
118 118 | case (Foo.A):

F841_0.py:122:14: F841 Local variable `y` is assigned to but never used
|
120 | case [Bar.A, *_]:
121 | print("A")
122 | case y:
| ^ F841
123 | pass
|
= help: Remove assignment to unused variable `y`

F841_0.py:127:21: F841 Local variable `value` is assigned to but never used
|
126 | def f():
127 | if any((key := (value := x)) for x in ["ok"]):
| ^^^^^ F841
128 | print(key)
|
= help: Remove assignment to unused variable `value`


Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
assertion_line: 161
---
F841_1.py:6:5: F841 [*] Local variable `x` is assigned to but never used
|
5 | def f():
6 | x, y = 1, 2 # this triggers F841 as it's just a simple assignment where unpacking isn't needed
| ^ F841
|
= help: Remove assignment to unused variable `x`

ℹ Unsafe fix
3 3 |
4 4 |
5 5 | def f():
6 |- x, y = 1, 2 # this triggers F841 as it's just a simple assignment where unpacking isn't needed
6 |+ _x, y = 1, 2 # this triggers F841 as it's just a simple assignment where unpacking isn't needed
7 7 |
8 8 |
9 9 | def f():

F841_1.py:6:8: F841 [*] Local variable `y` is assigned to but never used
|
5 | def f():
6 | x, y = 1, 2 # this triggers F841 as it's just a simple assignment where unpacking isn't needed
| ^ F841
|
= help: Remove assignment to unused variable `y`

ℹ Unsafe fix
3 3 |
4 4 |
5 5 | def f():
6 |- x, y = 1, 2 # this triggers F841 as it's just a simple assignment where unpacking isn't needed
6 |+ x, _y = 1, 2 # this triggers F841 as it's just a simple assignment where unpacking isn't needed
7 7 |
8 8 |
9 9 | def f():

F841_1.py:16:14: F841 [*] Local variable `coords` is assigned to but never used
|
15 | def f():
16 | (x, y) = coords = 1, 2
| ^^^^^^ F841
|
= help: Remove assignment to unused variable `coords`

ℹ Unsafe fix
13 13 |
14 14 |
15 15 | def f():
16 |- (x, y) = coords = 1, 2
16 |+ (x, y) = 1, 2
17 17 |
18 18 |
19 19 | def f():

F841_1.py:20:5: F841 [*] Local variable `coords` is assigned to but never used
|
19 | def f():
20 | coords = (x, y) = 1, 2
| ^^^^^^ F841
|
= help: Remove assignment to unused variable `coords`

ℹ Unsafe fix
17 17 |
18 18 |
19 19 | def f():
20 |- coords = (x, y) = 1, 2
20 |+ (x, y) = 1, 2
21 21 |
22 22 |
23 23 | def f():

F841_1.py:24:6: F841 [*] Local variable `a` is assigned to but never used
|
23 | def f():
24 | (a, b) = (x, y) = 1, 2 # this triggers F841 on everything
| ^ F841
|
= help: Remove assignment to unused variable `a`

ℹ Unsafe fix
21 21 |
22 22 |
23 23 | def f():
24 |- (a, b) = (x, y) = 1, 2 # this triggers F841 on everything
24 |+ (_a, b) = (x, y) = 1, 2 # this triggers F841 on everything

F841_1.py:24:9: F841 [*] Local variable `b` is assigned to but never used
|
23 | def f():
24 | (a, b) = (x, y) = 1, 2 # this triggers F841 on everything
| ^ F841
|
= help: Remove assignment to unused variable `b`

ℹ Unsafe fix
21 21 |
22 22 |
23 23 | def f():
24 |- (a, b) = (x, y) = 1, 2 # this triggers F841 on everything
24 |+ (a, _b) = (x, y) = 1, 2 # this triggers F841 on everything

F841_1.py:24:15: F841 [*] Local variable `x` is assigned to but never used
|
23 | def f():
24 | (a, b) = (x, y) = 1, 2 # this triggers F841 on everything
| ^ F841
|
= help: Remove assignment to unused variable `x`

ℹ Unsafe fix
21 21 |
22 22 |
23 23 | def f():
24 |- (a, b) = (x, y) = 1, 2 # this triggers F841 on everything
24 |+ (a, b) = (_x, y) = 1, 2 # this triggers F841 on everything

F841_1.py:24:18: F841 [*] Local variable `y` is assigned to but never used
|
23 | def f():
24 | (a, b) = (x, y) = 1, 2 # this triggers F841 on everything
| ^ F841
|
= help: Remove assignment to unused variable `y`

ℹ Unsafe fix
21 21 |
22 22 |
23 23 | def f():
24 |- (a, b) = (x, y) = 1, 2 # this triggers F841 on everything
24 |+ (a, b) = (x, _y) = 1, 2 # this triggers F841 on everything


Loading

0 comments on commit 762a5aa

Please sign in to comment.