From 45f590d7da8a7ac46a96b83a4da5519515a2ae0f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 23 Jul 2023 22:14:47 -0400 Subject: [PATCH] Tweak --- crates/ruff/src/checkers/ast/mod.rs | 10 ++++------ .../pylint/rules/self_assigning_variable.rs | 18 +++++++----------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 5df7e9ae876a3..4cc48804c0fbb 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -1584,7 +1584,9 @@ where } } if self.settings.rules.enabled(Rule::SelfAssigningVariable) { - pylint::rules::self_assigning_variable(self, targets, value); + if let [target] = targets.as_slice() { + pylint::rules::self_assigning_variable(self, target, value); + } } if self.settings.rules.enabled(Rule::TypeParamNameMismatch) { pylint::rules::type_param_name_mismatch(self, value, targets); @@ -1649,11 +1651,7 @@ where ); } if self.enabled(Rule::SelfAssigningVariable) { - pylint::rules::self_assigning_variable( - self, - &[target.as_ref().clone()], - value, - ); + pylint::rules::self_assigning_variable(self, target, value); } } if self.enabled(Rule::UnintentionalTypeAnnotation) { diff --git a/crates/ruff/src/rules/pylint/rules/self_assigning_variable.rs b/crates/ruff/src/rules/pylint/rules/self_assigning_variable.rs index 59427f20d94cf..1cc8f1712df53 100644 --- a/crates/ruff/src/rules/pylint/rules/self_assigning_variable.rs +++ b/crates/ruff/src/rules/pylint/rules/self_assigning_variable.rs @@ -35,17 +35,7 @@ impl Violation for SelfAssigningVariable { } /// PLW0127 -pub(crate) fn self_assigning_variable(checker: &mut Checker, targets: &[Expr], value: &Expr) { - // 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; - } - - let [target] = targets else { - return; - }; - +pub(crate) fn self_assigning_variable(checker: &mut Checker, target: &Expr, value: &Expr) { fn inner(left: &Expr, right: &Expr, diagnostics: &mut Vec) { match (left, right) { ( @@ -70,5 +60,11 @@ pub(crate) fn self_assigning_variable(checker: &mut Checker, targets: &[Expr], v } } + // 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); }