-
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 Pylint rule [`self-assigning-variable` (`W0127`)](https://pylint.pycqa.org/en/latest/user_guide/messages/warning/self-assigning-variable.html) as `self-assigning-variable` (`PLW0127`). Includes documentation. Related to #970. ## Test Plan `cargo test`
- Loading branch information
Showing
8 changed files
with
488 additions
and
2 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
crates/ruff/resources/test/fixtures/pylint/self_assigning_variable.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,41 @@ | ||
foo = 1 | ||
bar = 2 | ||
baz = 3 | ||
|
||
# Errors. | ||
foo = foo | ||
bar = bar | ||
foo, bar = foo, bar | ||
bar, foo = bar, foo | ||
(foo, bar) = (foo, bar) | ||
(bar, foo) = (bar, foo) | ||
foo, (bar, baz) = foo, (bar, baz) | ||
bar, (foo, baz) = bar, (foo, baz) | ||
(foo, bar), baz = (foo, bar), baz | ||
(foo, (bar, baz)) = (foo, (bar, baz)) | ||
foo, bar = foo, 1 | ||
bar, foo = bar, 1 | ||
(foo, bar) = (foo, 1) | ||
(bar, foo) = (bar, 1) | ||
foo, (bar, baz) = foo, (bar, 1) | ||
bar, (foo, baz) = bar, (foo, 1) | ||
(foo, bar), baz = (foo, bar), 1 | ||
(foo, (bar, baz)) = (foo, (bar, 1)) | ||
foo: int = foo | ||
bar: int = bar | ||
|
||
# Non-errors. | ||
foo = bar | ||
bar = foo | ||
foo, bar = bar, foo | ||
foo, bar = bar, foo | ||
(foo, bar) = (bar, foo) | ||
foo, bar = bar, 1 | ||
bar, foo = foo, 1 | ||
foo: int = bar | ||
bar: int = 1 | ||
|
||
|
||
class Foo: | ||
foo = foo | ||
bar = bar |
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
70 changes: 70 additions & 0 deletions
70
crates/ruff/src/rules/pylint/rules/self_assigning_variable.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,70 @@ | ||
use rustpython_parser::ast::{self, Expr, Ranged}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for self-assignment of variables. | ||
/// | ||
/// ## Why is this bad? | ||
/// Self-assignment of variables is redundant and likely a mistake. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// country = "Poland" | ||
/// country = country | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// country = "Poland" | ||
/// ``` | ||
#[violation] | ||
pub struct SelfAssigningVariable { | ||
name: String, | ||
} | ||
|
||
impl Violation for SelfAssigningVariable { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let SelfAssigningVariable { name } = self; | ||
format!("Self-assignment of variable `{name}`") | ||
} | ||
} | ||
|
||
/// PLW0127 | ||
pub(crate) fn self_assigning_variable(checker: &mut Checker, target: &Expr, value: &Expr) { | ||
fn inner(left: &Expr, right: &Expr, diagnostics: &mut Vec<Diagnostic>) { | ||
match (left, right) { | ||
( | ||
Expr::Tuple(ast::ExprTuple { elts: lhs_elts, .. }), | ||
Expr::Tuple(ast::ExprTuple { elts: rhs_elts, .. }), | ||
) if lhs_elts.len() == rhs_elts.len() => lhs_elts | ||
.iter() | ||
.zip(rhs_elts.iter()) | ||
.for_each(|(lhs, rhs)| inner(lhs, rhs, diagnostics)), | ||
( | ||
Expr::Name(ast::ExprName { id: lhs_name, .. }), | ||
Expr::Name(ast::ExprName { id: rhs_name, .. }), | ||
) if lhs_name == rhs_name => { | ||
diagnostics.push(Diagnostic::new( | ||
SelfAssigningVariable { | ||
name: lhs_name.to_string(), | ||
}, | ||
left.range(), | ||
)); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
// Assignments in class bodies are attributes (e.g., `x = x` assigns `x` to `self.x`, and thus | ||
// is not a self-assignment). | ||
if checker.semantic().scope().kind.is_class() { | ||
return; | ||
} | ||
|
||
inner(target, value, &mut checker.diagnostics); | ||
} |
Oops, something went wrong.