-
Notifications
You must be signed in to change notification settings - Fork 883
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pylint] - implement W0128/redeclared-assigned-name
- Loading branch information
1 parent
6e65601
commit 4bbb3c4
Showing
8 changed files
with
132 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 = (1, 2) # 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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
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. | ||
/// | ||
#[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 names.contains(&id.to_string()) { | ||
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 = (1, 2) # 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 = (1, 2) # OK | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.