-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Implements [`W0128`/`redeclared-assigned-name`](https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/redeclared-assigned-name.html) See: #970 ## Test Plan `cargo test`
- Loading branch information
1 parent
7e652e8
commit 740c08b
Showing
8 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
crates/ruff_linter/resources/test/fixtures/pylint/redeclared_assigned_name.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FIRST, FIRST = (1, 2) # PLW0128 | ||
FIRST, (FIRST, SECOND) = (1, (1, 2)) # PLW0128 | ||
FIRST, (FIRST, SECOND, (THIRD, FIRST)) = (1, (1, 2)) # PLW0128 | ||
FIRST, SECOND, THIRD, FIRST, SECOND = (1, 2, 3, 4) # PLW0128 | ||
|
||
FIRST, SECOND, _, _, _ignored = (1, 2, 3, 4, 5) # OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
crates/ruff_linter/src/rules/pylint/rules/redeclared_assigned_name.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use ruff_python_ast::{self as ast, Expr}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for declared assignments to the same variable multiple times | ||
/// in the same assignment. | ||
/// | ||
/// ## Why is this bad? | ||
/// Assigning a variable multiple times in the same assignment is redundant, | ||
/// as the final assignment to the variable is what the value will be. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// a, b, a = (1, 2, 3) | ||
/// print(a) # 3 | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// # this is assuming you want to assign 3 to `a` | ||
/// _, b, a = (1, 2, 3) | ||
/// print(a) # 3 | ||
/// ``` | ||
/// | ||
#[violation] | ||
pub struct RedeclaredAssignedName { | ||
name: String, | ||
} | ||
|
||
impl Violation for RedeclaredAssignedName { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let RedeclaredAssignedName { name } = self; | ||
format!("Redeclared variable `{name}` in assignment") | ||
} | ||
} | ||
|
||
/// PLW0128 | ||
pub(crate) fn redeclared_assigned_name(checker: &mut Checker, targets: &Vec<Expr>) { | ||
let mut names: Vec<String> = Vec::new(); | ||
|
||
for target in targets { | ||
check_expr(checker, target, &mut names); | ||
} | ||
} | ||
|
||
fn check_expr(checker: &mut Checker, expr: &Expr, names: &mut Vec<String>) { | ||
match expr { | ||
Expr::Tuple(ast::ExprTuple { elts, .. }) => { | ||
for target in elts { | ||
check_expr(checker, target, names); | ||
} | ||
} | ||
Expr::Name(ast::ExprName { id, .. }) => { | ||
if checker.settings.dummy_variable_rgx.is_match(id) { | ||
// Ignore dummy variable assignments | ||
return; | ||
} | ||
if names.contains(id) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
RedeclaredAssignedName { | ||
name: id.to_string(), | ||
}, | ||
expr.range(), | ||
)); | ||
} | ||
names.push(id.to_string()); | ||
} | ||
_ => {} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...int/snapshots/ruff_linter__rules__pylint__tests__PLW0128_redeclared_assigned_name.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
redeclared_assigned_name.py:1:8: PLW0128 Redeclared variable `FIRST` in assignment | ||
| | ||
1 | FIRST, FIRST = (1, 2) # PLW0128 | ||
| ^^^^^ PLW0128 | ||
2 | FIRST, (FIRST, SECOND) = (1, (1, 2)) # PLW0128 | ||
3 | FIRST, (FIRST, SECOND, (THIRD, FIRST)) = (1, (1, 2)) # PLW0128 | ||
| | ||
|
||
redeclared_assigned_name.py:2:9: PLW0128 Redeclared variable `FIRST` in assignment | ||
| | ||
1 | FIRST, FIRST = (1, 2) # PLW0128 | ||
2 | FIRST, (FIRST, SECOND) = (1, (1, 2)) # PLW0128 | ||
| ^^^^^ PLW0128 | ||
3 | FIRST, (FIRST, SECOND, (THIRD, FIRST)) = (1, (1, 2)) # PLW0128 | ||
4 | FIRST, SECOND, THIRD, FIRST, SECOND = (1, 2, 3, 4) # PLW0128 | ||
| | ||
|
||
redeclared_assigned_name.py:3:9: PLW0128 Redeclared variable `FIRST` in assignment | ||
| | ||
1 | FIRST, FIRST = (1, 2) # PLW0128 | ||
2 | FIRST, (FIRST, SECOND) = (1, (1, 2)) # PLW0128 | ||
3 | FIRST, (FIRST, SECOND, (THIRD, FIRST)) = (1, (1, 2)) # PLW0128 | ||
| ^^^^^ PLW0128 | ||
4 | FIRST, SECOND, THIRD, FIRST, SECOND = (1, 2, 3, 4) # PLW0128 | ||
| | ||
|
||
redeclared_assigned_name.py:3:32: PLW0128 Redeclared variable `FIRST` in assignment | ||
| | ||
1 | FIRST, FIRST = (1, 2) # PLW0128 | ||
2 | FIRST, (FIRST, SECOND) = (1, (1, 2)) # PLW0128 | ||
3 | FIRST, (FIRST, SECOND, (THIRD, FIRST)) = (1, (1, 2)) # PLW0128 | ||
| ^^^^^ PLW0128 | ||
4 | FIRST, SECOND, THIRD, FIRST, SECOND = (1, 2, 3, 4) # PLW0128 | ||
| | ||
|
||
redeclared_assigned_name.py:4:23: PLW0128 Redeclared variable `FIRST` in assignment | ||
| | ||
2 | FIRST, (FIRST, SECOND) = (1, (1, 2)) # PLW0128 | ||
3 | FIRST, (FIRST, SECOND, (THIRD, FIRST)) = (1, (1, 2)) # PLW0128 | ||
4 | FIRST, SECOND, THIRD, FIRST, SECOND = (1, 2, 3, 4) # PLW0128 | ||
| ^^^^^ PLW0128 | ||
5 | | ||
6 | FIRST, SECOND, _, _, _ignored = (1, 2, 3, 4, 5) # OK | ||
| | ||
|
||
redeclared_assigned_name.py:4:30: PLW0128 Redeclared variable `SECOND` in assignment | ||
| | ||
2 | FIRST, (FIRST, SECOND) = (1, (1, 2)) # PLW0128 | ||
3 | FIRST, (FIRST, SECOND, (THIRD, FIRST)) = (1, (1, 2)) # PLW0128 | ||
4 | FIRST, SECOND, THIRD, FIRST, SECOND = (1, 2, 3, 4) # PLW0128 | ||
| ^^^^^^ PLW0128 | ||
5 | | ||
6 | FIRST, SECOND, _, _, _ignored = (1, 2, 3, 4, 5) # OK | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.