Expand F841 fixes to handle parenthesized targets (#7110)

This commit is contained in:
Charlie Marsh 2023-09-03 22:00:44 +01:00 committed by GitHub
parent 7da99cc756
commit d9cf31f355
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 6 deletions

View file

@ -165,3 +165,9 @@ def f():
x = 1
y = 2
def f():
(x) = foo()
((x)) = foo()
(x) = (y.z) = foo()

View file

@ -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<Fix> {
{
// 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 {

View file

@ -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()