mirror of https://github.com/astral-sh/ruff
Expand F841 fixes to handle parenthesized targets (#7110)
This commit is contained in:
parent
7da99cc756
commit
d9cf31f355
|
|
@ -165,3 +165,9 @@ def f():
|
||||||
x = 1
|
x = 1
|
||||||
|
|
||||||
y = 2
|
y = 2
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
(x) = foo()
|
||||||
|
((x)) = foo()
|
||||||
|
(x) = (y.z) = foo()
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ use itertools::Itertools;
|
||||||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::contains_effect;
|
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_ast::{self as ast, PySourceType, Stmt};
|
||||||
use ruff_python_parser::{lexer, AsMode, Tok};
|
use ruff_python_parser::{lexer, AsMode, Tok};
|
||||||
use ruff_python_semantic::{Binding, Scope};
|
use ruff_python_semantic::{Binding, Scope};
|
||||||
|
|
@ -220,11 +221,19 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
|
||||||
{
|
{
|
||||||
// If the expression is complex (`x = foo()`), remove the assignment,
|
// If the expression is complex (`x = foo()`), remove the assignment,
|
||||||
// but preserve the right-hand side.
|
// but preserve the right-hand side.
|
||||||
let start = target.start();
|
let start = parenthesized_range(
|
||||||
let end =
|
target.into(),
|
||||||
match_token_after(start, checker.locator(), checker.source_type, |tok| {
|
statement.into(),
|
||||||
tok == Tok::Equal
|
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();
|
.start();
|
||||||
let edit = Edit::deletion(start, end);
|
let edit = Edit::deletion(start, end);
|
||||||
Some(Fix::suggested(edit))
|
Some(Fix::suggested(edit))
|
||||||
|
|
|
||||||
|
|
@ -657,6 +657,7 @@ F841_3.py:165:5: F841 [*] Local variable `x` is assigned to but never used
|
||||||
165 |- x = 1
|
165 |- x = 1
|
||||||
166 165 |
|
166 165 |
|
||||||
167 166 | y = 2
|
167 166 | y = 2
|
||||||
|
168 167 |
|
||||||
|
|
||||||
F841_3.py:167:5: F841 [*] Local variable `y` is assigned to but never used
|
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
|
165 165 | x = 1
|
||||||
166 166 |
|
166 166 |
|
||||||
167 |- y = 2
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue