diff --git a/crates/ruff/resources/test/fixtures/pyflakes/F841_3.py b/crates/ruff/resources/test/fixtures/pyflakes/F841_3.py index ce7b19a1b3..00c2962223 100644 --- a/crates/ruff/resources/test/fixtures/pyflakes/F841_3.py +++ b/crates/ruff/resources/test/fixtures/pyflakes/F841_3.py @@ -165,3 +165,9 @@ def f(): x = 1 y = 2 + + +def f(): + (x) = foo() + ((x)) = foo() + (x) = (y.z) = foo() diff --git a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs index 4058c3e748..89eebff824 100644 --- a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs +++ b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs @@ -3,6 +3,7 @@ use itertools::Itertools; use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::contains_effect; +use ruff_python_ast::parenthesize::parenthesized_range; use ruff_python_ast::{self as ast, PySourceType, Stmt}; use ruff_python_parser::{lexer, AsMode, Tok}; use ruff_python_semantic::{Binding, Scope}; @@ -220,12 +221,20 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option { { // If the expression is complex (`x = foo()`), remove the assignment, // but preserve the right-hand side. - let start = target.start(); - let end = - match_token_after(start, checker.locator(), checker.source_type, |tok| { - tok == Tok::Equal - })? - .start(); + let start = parenthesized_range( + target.into(), + statement.into(), + checker.locator().contents(), + ) + .unwrap_or(target.range()) + .start(); + let end = match_token_after( + target.end(), + checker.locator(), + checker.source_type, + |tok| tok == Tok::Equal, + )? + .start(); let edit = Edit::deletion(start, end); Some(Fix::suggested(edit)) } else { diff --git a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F841_F841_3.py.snap b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F841_F841_3.py.snap index 85c614015c..c428d3b5ec 100644 --- a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F841_F841_3.py.snap +++ b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F841_F841_3.py.snap @@ -657,6 +657,7 @@ F841_3.py:165:5: F841 [*] Local variable `x` is assigned to but never used 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 | @@ -672,5 +673,24 @@ F841_3.py:167:5: F841 [*] Local variable `y` is assigned to but never used 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` + +ℹ Suggested fix +170 170 | def f(): +171 171 | (x) = foo() +172 172 | ((x)) = foo() +173 |- (x) = (y.z) = foo() + 173 |+ (y.z) = foo()