From 762a5aa013c7919ef90e4d23b16ecfa838c023f4 Mon Sep 17 00:00:00 2001 From: Samuel Cormier-Iijima Date: Tue, 12 Dec 2023 11:03:30 -0500 Subject: [PATCH] F841: support fixing unused assignments in tuples by renaming variables --- .../rules/pyflakes/rules/unused_variable.rs | 8 + ...__pyflakes__tests__F841_F841_0.py.snap.new | 243 ++++++ ...__pyflakes__tests__F841_F841_1.py.snap.new | 137 ++++ ...__pyflakes__tests__F841_F841_3.py.snap.new | 717 ++++++++++++++++++ ...s__tests__preview__F841_F841_4.py.snap.new | 62 ++ 5 files changed, 1167 insertions(+) create mode 100644 crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_0.py.snap.new create mode 100644 crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_1.py.snap.new create mode 100644 crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_3.py.snap.new create mode 100644 crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__preview__F841_F841_4.py.snap.new diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/unused_variable.rs b/crates/ruff_linter/src/rules/pyflakes/rules/unused_variable.rs index f5eb6944836173..bae8f64c3b00ef 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/unused_variable.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/unused_variable.rs @@ -247,6 +247,14 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option { 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)); + } } } diff --git a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_0.py.snap.new b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_0.py.snap.new new file mode 100644 index 00000000000000..b5f1bfe1a2820a --- /dev/null +++ b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_0.py.snap.new @@ -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` + + diff --git a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_1.py.snap.new b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_1.py.snap.new new file mode 100644 index 00000000000000..f55a4ee31db578 --- /dev/null +++ b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_1.py.snap.new @@ -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 + + diff --git a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_3.py.snap.new b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_3.py.snap.new new file mode 100644 index 00000000000000..c49622d9ad8054 --- /dev/null +++ b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F841_F841_3.py.snap.new @@ -0,0 +1,717 @@ +--- +source: crates/ruff_linter/src/rules/pyflakes/mod.rs +assertion_line: 161 +--- +F841_3.py:5:5: F841 [*] Local variable `x` is assigned to but never used + | +4 | def f(): +5 | x = 1 + | ^ F841 +6 | y = 2 + | + = help: Remove assignment to unused variable `x` + +ℹ Unsafe fix +2 2 | +3 3 | +4 4 | def f(): +5 |- x = 1 +6 5 | y = 2 +7 6 | +8 7 | z = 3 + +F841_3.py:6:5: F841 [*] Local variable `y` is assigned to but never used + | +4 | def f(): +5 | x = 1 +6 | y = 2 + | ^ F841 +7 | +8 | z = 3 + | + = help: Remove assignment to unused variable `y` + +ℹ Unsafe fix +3 3 | +4 4 | def f(): +5 5 | x = 1 +6 |- y = 2 +7 6 | +8 7 | z = 3 +9 8 | print(z) + +F841_3.py:13:5: F841 [*] Local variable `x` is assigned to but never used + | +12 | def f(): +13 | x: int = 1 + | ^ F841 +14 | y: int = 2 + | + = help: Remove assignment to unused variable `x` + +ℹ Unsafe fix +10 10 | +11 11 | +12 12 | def f(): +13 |- x: int = 1 +14 13 | y: int = 2 +15 14 | +16 15 | z: int = 3 + +F841_3.py:14:5: F841 [*] Local variable `y` is assigned to but never used + | +12 | def f(): +13 | x: int = 1 +14 | y: int = 2 + | ^ F841 +15 | +16 | z: int = 3 + | + = help: Remove assignment to unused variable `y` + +ℹ Unsafe fix +11 11 | +12 12 | def f(): +13 13 | x: int = 1 +14 |- y: int = 2 +15 14 | +16 15 | z: int = 3 +17 16 | print(z) + +F841_3.py:21:19: F841 [*] Local variable `x1` is assigned to but never used + | +20 | def f(): +21 | with foo() as x1: + | ^^ F841 +22 | pass + | + = help: Remove assignment to unused variable `x1` + +ℹ Unsafe fix +18 18 | +19 19 | +20 20 | def f(): +21 |- with foo() as x1: + 21 |+ with foo(): +22 22 | pass +23 23 | +24 24 | with foo() as (x2, y2): + +F841_3.py:27:20: F841 [*] Local variable `x3` is assigned to but never used + | +25 | pass +26 | +27 | with (foo() as x3, foo() as y3, foo() as z3): + | ^^ F841 +28 | pass + | + = help: Remove assignment to unused variable `x3` + +ℹ Unsafe fix +24 24 | with foo() as (x2, y2): +25 25 | pass +26 26 | +27 |- with (foo() as x3, foo() as y3, foo() as z3): + 27 |+ with (foo(), foo() as y3, foo() as z3): +28 28 | pass +29 29 | +30 30 | + +F841_3.py:27:33: F841 [*] Local variable `y3` is assigned to but never used + | +25 | pass +26 | +27 | with (foo() as x3, foo() as y3, foo() as z3): + | ^^ F841 +28 | pass + | + = help: Remove assignment to unused variable `y3` + +ℹ Unsafe fix +24 24 | with foo() as (x2, y2): +25 25 | pass +26 26 | +27 |- with (foo() as x3, foo() as y3, foo() as z3): + 27 |+ with (foo() as x3, foo(), foo() as z3): +28 28 | pass +29 29 | +30 30 | + +F841_3.py:27:46: F841 [*] Local variable `z3` is assigned to but never used + | +25 | pass +26 | +27 | with (foo() as x3, foo() as y3, foo() as z3): + | ^^ F841 +28 | pass + | + = help: Remove assignment to unused variable `z3` + +ℹ Unsafe fix +24 24 | with foo() as (x2, y2): +25 25 | pass +26 26 | +27 |- with (foo() as x3, foo() as y3, foo() as z3): + 27 |+ with (foo() as x3, foo() as y3, foo()): +28 28 | pass +29 29 | +30 30 | + +F841_3.py:32:6: F841 [*] Local variable `x1` is assigned to but never used + | +31 | def f(): +32 | (x1, y1) = (1, 2) + | ^^ F841 +33 | (x2, y2) = coords2 = (1, 2) +34 | coords3 = (x3, y3) = (1, 2) + | + = help: Remove assignment to unused variable `x1` + +ℹ Unsafe fix +29 29 | +30 30 | +31 31 | def f(): +32 |- (x1, y1) = (1, 2) + 32 |+ (_x1, y1) = (1, 2) +33 33 | (x2, y2) = coords2 = (1, 2) +34 34 | coords3 = (x3, y3) = (1, 2) +35 35 | + +F841_3.py:32:10: F841 [*] Local variable `y1` is assigned to but never used + | +31 | def f(): +32 | (x1, y1) = (1, 2) + | ^^ F841 +33 | (x2, y2) = coords2 = (1, 2) +34 | coords3 = (x3, y3) = (1, 2) + | + = help: Remove assignment to unused variable `y1` + +ℹ Unsafe fix +29 29 | +30 30 | +31 31 | def f(): +32 |- (x1, y1) = (1, 2) + 32 |+ (x1, _y1) = (1, 2) +33 33 | (x2, y2) = coords2 = (1, 2) +34 34 | coords3 = (x3, y3) = (1, 2) +35 35 | + +F841_3.py:33:16: F841 [*] Local variable `coords2` is assigned to but never used + | +31 | def f(): +32 | (x1, y1) = (1, 2) +33 | (x2, y2) = coords2 = (1, 2) + | ^^^^^^^ F841 +34 | coords3 = (x3, y3) = (1, 2) + | + = help: Remove assignment to unused variable `coords2` + +ℹ Unsafe fix +30 30 | +31 31 | def f(): +32 32 | (x1, y1) = (1, 2) +33 |- (x2, y2) = coords2 = (1, 2) + 33 |+ (x2, y2) = (1, 2) +34 34 | coords3 = (x3, y3) = (1, 2) +35 35 | +36 36 | + +F841_3.py:34:5: F841 [*] Local variable `coords3` is assigned to but never used + | +32 | (x1, y1) = (1, 2) +33 | (x2, y2) = coords2 = (1, 2) +34 | coords3 = (x3, y3) = (1, 2) + | ^^^^^^^ F841 + | + = help: Remove assignment to unused variable `coords3` + +ℹ Unsafe fix +31 31 | def f(): +32 32 | (x1, y1) = (1, 2) +33 33 | (x2, y2) = coords2 = (1, 2) +34 |- coords3 = (x3, y3) = (1, 2) + 34 |+ (x3, y3) = (1, 2) +35 35 | +36 36 | +37 37 | def f(): + +F841_3.py:40:26: F841 [*] Local variable `x1` is assigned to but never used + | +38 | try: +39 | 1 / 0 +40 | except ValueError as x1: + | ^^ F841 +41 | pass + | + = help: Remove assignment to unused variable `x1` + +ℹ Safe fix +37 37 | def f(): +38 38 | try: +39 39 | 1 / 0 +40 |- except ValueError as x1: + 40 |+ except ValueError: +41 41 | pass +42 42 | +43 43 | try: + +F841_3.py:45:47: F841 [*] Local variable `x2` is assigned to but never used + | +43 | try: +44 | 1 / 0 +45 | except (ValueError, ZeroDivisionError) as x2: + | ^^ F841 +46 | pass + | + = help: Remove assignment to unused variable `x2` + +ℹ Safe fix +42 42 | +43 43 | try: +44 44 | 1 / 0 +45 |- except (ValueError, ZeroDivisionError) as x2: + 45 |+ except (ValueError, ZeroDivisionError): +46 46 | pass +47 47 | +48 48 | + +F841_3.py:50:5: F841 [*] Local variable `x` is assigned to but never used + | +49 | def f(a, b): +50 | x = ( + | ^ F841 +51 | a() +52 | if a is not None + | + = help: Remove assignment to unused variable `x` + +ℹ Unsafe fix +47 47 | +48 48 | +49 49 | def f(a, b): +50 |- x = ( + 50 |+ ( +51 51 | a() +52 52 | if a is not None +53 53 | else b + +F841_3.py:56:5: F841 [*] Local variable `y` is assigned to but never used + | +54 | ) +55 | +56 | y = \ + | ^ F841 +57 | a() if a is not None else b + | + = help: Remove assignment to unused variable `y` + +ℹ Unsafe fix +53 53 | else b +54 54 | ) +55 55 | +56 |- y = \ +57 |- a() if a is not None else b + 56 |+ a() if a is not None else b +58 57 | +59 58 | +60 59 | def f(a, b): + +F841_3.py:61:5: F841 [*] Local variable `x` is assigned to but never used + | +60 | def f(a, b): +61 | x = ( + | ^ F841 +62 | a +63 | if a is not None + | + = help: Remove assignment to unused variable `x` + +ℹ Unsafe fix +58 58 | +59 59 | +60 60 | def f(a, b): +61 |- x = ( +62 |- a +63 |- if a is not None +64 |- else b +65 |- ) +66 61 | +67 62 | y = \ +68 63 | a if a is not None else b + +F841_3.py:67:5: F841 [*] Local variable `y` is assigned to but never used + | +65 | ) +66 | +67 | y = \ + | ^ F841 +68 | a if a is not None else b + | + = help: Remove assignment to unused variable `y` + +ℹ Unsafe fix +64 64 | else b +65 65 | ) +66 66 | +67 |- y = \ +68 |- a if a is not None else b +69 67 | +70 68 | +71 69 | def f(): + +F841_3.py:72:24: F841 [*] Local variable `cm` is assigned to but never used + | +71 | def f(): +72 | with Nested(m) as (cm): + | ^^ F841 +73 | pass + | + = help: Remove assignment to unused variable `cm` + +ℹ Unsafe fix +69 69 | +70 70 | +71 71 | def f(): +72 |- with Nested(m) as (cm): + 72 |+ with Nested(m): +73 73 | pass +74 74 | +75 75 | + +F841_3.py:77:25: F841 [*] Local variable `cm` is assigned to but never used + | +76 | def f(): +77 | with (Nested(m) as (cm),): + | ^^ F841 +78 | pass + | + = help: Remove assignment to unused variable `cm` + +ℹ Unsafe fix +74 74 | +75 75 | +76 76 | def f(): +77 |- with (Nested(m) as (cm),): + 77 |+ with (Nested(m),): +78 78 | pass +79 79 | +80 80 | + +F841_3.py:87:26: F841 [*] Local variable `cm` is assigned to but never used + | +86 | def f(): +87 | with (Nested(m)) as (cm): + | ^^ F841 +88 | pass + | + = help: Remove assignment to unused variable `cm` + +ℹ Unsafe fix +84 84 | +85 85 | +86 86 | def f(): +87 |- with (Nested(m)) as (cm): + 87 |+ with (Nested(m)): +88 88 | pass +89 89 | +90 90 | + +F841_3.py:92:5: F841 [*] Local variable `toplevel` is assigned to but never used + | +91 | def f(): +92 | toplevel = tt = lexer.get_token() + | ^^^^^^^^ F841 +93 | if not tt: +94 | break + | + = help: Remove assignment to unused variable `toplevel` + +ℹ Unsafe fix +89 89 | +90 90 | +91 91 | def f(): +92 |- toplevel = tt = lexer.get_token() + 92 |+ tt = lexer.get_token() +93 93 | if not tt: +94 94 | break +95 95 | + +F841_3.py:98:5: F841 [*] Local variable `toplevel` is assigned to but never used + | +97 | def f(): +98 | toplevel = tt = lexer.get_token() + | ^^^^^^^^ F841 + | + = help: Remove assignment to unused variable `toplevel` + +ℹ Unsafe fix +95 95 | +96 96 | +97 97 | def f(): +98 |- toplevel = tt = lexer.get_token() + 98 |+ tt = lexer.get_token() +99 99 | +100 100 | +101 101 | def f(): + +F841_3.py:98:16: F841 [*] Local variable `tt` is assigned to but never used + | +97 | def f(): +98 | toplevel = tt = lexer.get_token() + | ^^ F841 + | + = help: Remove assignment to unused variable `tt` + +ℹ Unsafe fix +95 95 | +96 96 | +97 97 | def f(): +98 |- toplevel = tt = lexer.get_token() + 98 |+ toplevel = lexer.get_token() +99 99 | +100 100 | +101 101 | def f(): + +F841_3.py:102:5: F841 [*] Local variable `toplevel` is assigned to but never used + | +101 | def f(): +102 | toplevel = (a, b) = lexer.get_token() + | ^^^^^^^^ F841 + | + = help: Remove assignment to unused variable `toplevel` + +ℹ Unsafe fix +99 99 | +100 100 | +101 101 | def f(): +102 |- toplevel = (a, b) = lexer.get_token() + 102 |+ (a, b) = lexer.get_token() +103 103 | +104 104 | +105 105 | def f(): + +F841_3.py:106:14: F841 [*] Local variable `toplevel` is assigned to but never used + | +105 | def f(): +106 | (a, b) = toplevel = lexer.get_token() + | ^^^^^^^^ F841 + | + = help: Remove assignment to unused variable `toplevel` + +ℹ Unsafe fix +103 103 | +104 104 | +105 105 | def f(): +106 |- (a, b) = toplevel = lexer.get_token() + 106 |+ (a, b) = lexer.get_token() +107 107 | +108 108 | +109 109 | def f(): + +F841_3.py:110:5: F841 [*] Local variable `toplevel` is assigned to but never used + | +109 | def f(): +110 | toplevel = tt = 1 + | ^^^^^^^^ F841 + | + = help: Remove assignment to unused variable `toplevel` + +ℹ Unsafe fix +107 107 | +108 108 | +109 109 | def f(): +110 |- toplevel = tt = 1 + 110 |+ tt = 1 +111 111 | +112 112 | +113 113 | def f(provided: int) -> int: + +F841_3.py:110:16: F841 [*] Local variable `tt` is assigned to but never used + | +109 | def f(): +110 | toplevel = tt = 1 + | ^^ F841 + | + = help: Remove assignment to unused variable `tt` + +ℹ Unsafe fix +107 107 | +108 108 | +109 109 | def f(): +110 |- toplevel = tt = 1 + 110 |+ toplevel = 1 +111 111 | +112 112 | +113 113 | def f(provided: int) -> int: + +F841_3.py:115:19: F841 Local variable `x` is assigned to but never used + | +113 | def f(provided: int) -> int: +114 | match provided: +115 | case [_, *x]: + | ^ F841 +116 | pass + | + = help: Remove assignment to unused variable `x` + +F841_3.py:121:14: F841 Local variable `x` is assigned to but never used + | +119 | def f(provided: int) -> int: +120 | match provided: +121 | case x: + | ^ F841 +122 | pass + | + = help: Remove assignment to unused variable `x` + +F841_3.py:127:18: F841 Local variable `bar` is assigned to but never used + | +125 | def f(provided: int) -> int: +126 | match provided: +127 | case Foo(bar) as x: + | ^^^ F841 +128 | pass + | + = help: Remove assignment to unused variable `bar` + +F841_3.py:127:26: F841 Local variable `x` is assigned to but never used + | +125 | def f(provided: int) -> int: +126 | match provided: +127 | case Foo(bar) as x: + | ^ F841 +128 | pass + | + = help: Remove assignment to unused variable `x` + +F841_3.py:133:27: F841 Local variable `x` is assigned to but never used + | +131 | def f(provided: int) -> int: +132 | match provided: +133 | case {"foo": 0, **x}: + | ^ F841 +134 | pass + | + = help: Remove assignment to unused variable `x` + +F841_3.py:139:17: F841 Local variable `x` is assigned to but never used + | +137 | def f(provided: int) -> int: +138 | match provided: +139 | case {**x}: + | ^ F841 +140 | pass + | + = help: Remove assignment to unused variable `x` + +F841_3.py:155:17: F841 [*] Local variable `e` is assigned to but never used + | +153 | try: +154 | print("hello") +155 | except A as e : + | ^ F841 +156 | print("oh no!") + | + = help: Remove assignment to unused variable `e` + +ℹ Safe fix +152 152 | def f() -> None: +153 153 | try: +154 154 | print("hello") +155 |- except A as e : + 155 |+ except A: +156 156 | print("oh no!") +157 157 | +158 158 | + +F841_3.py:160:5: F841 [*] Local variable `x` is assigned to but never used + | +159 | def f(): +160 | x = 1 + | ^ F841 +161 | y = 2 + | + = help: Remove assignment to unused variable `x` + +ℹ Unsafe fix +157 157 | +158 158 | +159 159 | def f(): +160 |- x = 1 +161 160 | y = 2 +162 161 | +163 162 | + +F841_3.py:161:5: F841 [*] Local variable `y` is assigned to but never used + | +159 | def f(): +160 | x = 1 +161 | y = 2 + | ^ F841 + | + = help: Remove assignment to unused variable `y` + +ℹ Unsafe fix +158 158 | +159 159 | def f(): +160 160 | x = 1 +161 |- y = 2 +162 161 | +163 162 | +164 163 | def f(): + +F841_3.py:165:5: F841 [*] Local variable `x` is assigned to but never used + | +164 | def f(): +165 | x = 1 + | ^ F841 +166 | +167 | y = 2 + | + = help: Remove assignment to unused variable `x` + +ℹ Unsafe fix +162 162 | +163 163 | +164 164 | def f(): +165 |- x = 1 +166 165 | +167 166 | y = 2 +168 167 | + +F841_3.py:167:5: F841 [*] Local variable `y` is assigned to but never used + | +165 | x = 1 +166 | +167 | y = 2 + | ^ F841 + | + = help: Remove assignment to unused variable `y` + +ℹ Unsafe fix +164 164 | def f(): +165 165 | x = 1 +166 166 | +167 |- y = 2 +168 167 | +169 168 | +170 169 | def f(): + +F841_3.py:173:6: F841 [*] Local variable `x` is assigned to but never used + | +171 | (x) = foo() +172 | ((x)) = foo() +173 | (x) = (y.z) = foo() + | ^ F841 + | + = help: Remove assignment to unused variable `x` + +ℹ Unsafe fix +170 170 | def f(): +171 171 | (x) = foo() +172 172 | ((x)) = foo() +173 |- (x) = (y.z) = foo() + 173 |+ (y.z) = foo() + + diff --git a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__preview__F841_F841_4.py.snap.new b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__preview__F841_F841_4.py.snap.new new file mode 100644 index 00000000000000..a813ef554efa1b --- /dev/null +++ b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__preview__F841_F841_4.py.snap.new @@ -0,0 +1,62 @@ +--- +source: crates/ruff_linter/src/rules/pyflakes/mod.rs +assertion_line: 179 +--- +F841_4.py:12:5: F841 [*] Local variable `a` is assigned to but never used + | +11 | def bar(): +12 | a = foo() + | ^ F841 +13 | b, c = foo() + | + = help: Remove assignment to unused variable `a` + +ℹ Unsafe fix +9 9 | +10 10 | +11 11 | def bar(): +12 |- a = foo() + 12 |+ foo() +13 13 | b, c = foo() +14 14 | +15 15 | + +F841_4.py:13:5: F841 [*] Local variable `b` is assigned to but never used + | +11 | def bar(): +12 | a = foo() +13 | b, c = foo() + | ^ F841 + | + = help: Remove assignment to unused variable `b` + +ℹ Unsafe fix +10 10 | +11 11 | def bar(): +12 12 | a = foo() +13 |- b, c = foo() + 13 |+ _b, c = foo() +14 14 | +15 15 | +16 16 | def baz(): + +F841_4.py:13:8: F841 [*] Local variable `c` is assigned to but never used + | +11 | def bar(): +12 | a = foo() +13 | b, c = foo() + | ^ F841 + | + = help: Remove assignment to unused variable `c` + +ℹ Unsafe fix +10 10 | +11 11 | def bar(): +12 12 | a = foo() +13 |- b, c = foo() + 13 |+ b, _c = foo() +14 14 | +15 15 | +16 16 | def baz(): + +