Skip to content

Commit

Permalink
Avoid syntax errors when fixing parenthesized unused variables (#1919)
Browse files Browse the repository at this point in the history
Closes #1917.
  • Loading branch information
charliermarsh authored Jan 16, 2023
1 parent c0845a8 commit 6abf716
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
11 changes: 11 additions & 0 deletions resources/test/fixtures/pyflakes/F841_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ def f():
1 / 0
except (ValueError, ZeroDivisionError) as x2:
pass


def f(a, b):
x = (
a
if a is not None
else b
)

y = \
a if a is not None else b
32 changes: 29 additions & 3 deletions src/rules/pyflakes/rules/unused_variable.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use itertools::Itertools;
use log::error;
use rustpython_ast::{Expr, ExprKind, Stmt, StmtKind};
use rustpython_ast::{Expr, ExprKind, Location, Stmt, StmtKind};
use rustpython_parser::lexer;
use rustpython_parser::lexer::Tok;

use crate::ast::types::{BindingKind, Range, RefEquality, ScopeKind};
use crate::autofix::helpers::delete_stmt;
use crate::checkers::ast::Checker;
use crate::fix::Fix;
use crate::registry::{Diagnostic, RuleCode};
use crate::source_code::Locator;
use crate::violations;

fn is_literal_or_name(expr: &Expr, checker: &Checker) -> bool {
Expand Down Expand Up @@ -43,6 +47,22 @@ fn is_literal_or_name(expr: &Expr, checker: &Checker) -> bool {
false
}

fn match_token_after<F>(stmt: &Stmt, locator: &Locator, f: F) -> Location
where
F: Fn(Tok) -> bool,
{
let contents = locator.slice_source_code_range(&Range::from_located(stmt));
for ((_, tok, _), (start, ..)) in lexer::make_tokenizer_located(&contents, stmt.location)
.flatten()
.tuple_windows()
{
if f(tok) {
return start;
}
}
unreachable!("No token after matched");
}

enum DeletionKind {
Whole,
Partial,
Expand Down Expand Up @@ -83,7 +103,10 @@ fn remove_unused_variable(
// but preserve the right-hand side.
Some((
DeletionKind::Partial,
Fix::deletion(stmt.location, value.location),
Fix::deletion(
stmt.location,
match_token_after(stmt, checker.locator, |tok| tok == Tok::Equal),
),
))
};
}
Expand Down Expand Up @@ -122,7 +145,10 @@ fn remove_unused_variable(
// but preserve the right-hand side.
Some((
DeletionKind::Partial,
Fix::deletion(stmt.location, value.location),
Fix::deletion(
stmt.location,
match_token_after(stmt, checker.locator, |tok| tok == Tok::Equal),
),
))
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,38 @@ expression: diagnostics
row: 45
column: 48
parent: ~
- kind:
UnusedVariable: x
location:
row: 50
column: 4
end_location:
row: 50
column: 5
fix:
content: ""
location:
row: 50
column: 4
end_location:
row: 50
column: 8
parent: ~
- kind:
UnusedVariable: y
location:
row: 56
column: 4
end_location:
row: 56
column: 5
fix:
content: ""
location:
row: 56
column: 4
end_location:
row: 57
column: 8
parent: ~

0 comments on commit 6abf716

Please sign in to comment.