Skip to content

Commit

Permalink
recursively check tuple exprs
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Jan 20, 2024
1 parent 9cc9277 commit 8fbfbf9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
class Fruit:
@classmethod
def list_fruits(cls) -> None:
cls = "apple"
cls: Fruit = "apple"
cls += "orange"
cls, blah = "apple", "orange"
cls = "apple" # PLW0642
cls: Fruit = "apple" # PLW0642
cls += "orange" # PLW0642
cls, blah = "apple", "orange" # PLW0642
blah, (cls, blah2) = "apple", ("orange", "banana") # PLW0642

def print_color(self) -> None:
self = "red"
self: Self = "red"
self += "blue"
self, blah = "red", "blue"
self = "red" # PLW0642
self: Self = "red" # PLW0642
self += "blue" # PLW0642
self, blah = "red", "blue" # PLW0642
blah, (self, blah2) = "apple", ("orange", "banana") # PLW0642

def ok(self) -> None:
cls = None # OK because the rule looks for the name in the signature
32 changes: 17 additions & 15 deletions crates/ruff_linter/src/rules/pylint/rules/self_cls_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,28 @@ pub(crate) fn self_cls_assignment(checker: &mut Checker, target: &Expr) {
_ => return,
};

check_expr(checker, target, keyword);
}

fn check_expr(checker: &mut Checker, target: &Expr, keyword: &str) {
match target {
Expr::Name(_) => check_name(checker, target, keyword),
Expr::Name(_) => {
if let Expr::Name(ast::ExprName { id, .. }) = target {
if id.as_str() == keyword {
checker.diagnostics.push(Diagnostic::new(
SelfClsAssignment {
keyword: keyword.to_string(),
},
target.range(),
));
}
}
}
Expr::Tuple(ast::ExprTuple { elts, .. }) => {
for element in elts {
check_name(checker, element, keyword);
check_expr(checker, element, keyword);
}
}
_ => {}
}
}

fn check_name(checker: &mut Checker, target: &Expr, keyword: &str) {
if let Expr::Name(ast::ExprName { id, .. }) = target {
if id.as_str() == keyword {
checker.diagnostics.push(Diagnostic::new(
SelfClsAssignment {
keyword: keyword.to_string(),
},
target.range(),
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,97 @@ self_cls_assignment.py:4:9: PLW0642 Assignment of variable `cls`
|
2 | @classmethod
3 | def list_fruits(cls) -> None:
4 | cls = "apple"
4 | cls = "apple" # PLW0642
| ^^^ PLW0642
5 | cls: Fruit = "apple"
6 | cls += "orange"
5 | cls: Fruit = "apple" # PLW0642
6 | cls += "orange" # PLW0642
|

self_cls_assignment.py:5:9: PLW0642 Assignment of variable `cls`
|
3 | def list_fruits(cls) -> None:
4 | cls = "apple"
5 | cls: Fruit = "apple"
4 | cls = "apple" # PLW0642
5 | cls: Fruit = "apple" # PLW0642
| ^^^ PLW0642
6 | cls += "orange"
7 | cls, blah = "apple", "orange"
6 | cls += "orange" # PLW0642
7 | cls, blah = "apple", "orange" # PLW0642
|

self_cls_assignment.py:6:9: PLW0642 Assignment of variable `cls`
|
4 | cls = "apple"
5 | cls: Fruit = "apple"
6 | cls += "orange"
4 | cls = "apple" # PLW0642
5 | cls: Fruit = "apple" # PLW0642
6 | cls += "orange" # PLW0642
| ^^^ PLW0642
7 | cls, blah = "apple", "orange"
7 | cls, blah = "apple", "orange" # PLW0642
8 | blah, (cls, blah2) = "apple", ("orange", "banana") # PLW0642
|

self_cls_assignment.py:7:9: PLW0642 Assignment of variable `cls`
|
5 | cls: Fruit = "apple"
6 | cls += "orange"
7 | cls, blah = "apple", "orange"
5 | cls: Fruit = "apple" # PLW0642
6 | cls += "orange" # PLW0642
7 | cls, blah = "apple", "orange" # PLW0642
| ^^^ PLW0642
8 |
9 | def print_color(self) -> None:
8 | blah, (cls, blah2) = "apple", ("orange", "banana") # PLW0642
|

self_cls_assignment.py:10:9: PLW0642 Assignment of variable `self`
self_cls_assignment.py:8:16: PLW0642 Assignment of variable `cls`
|
9 | def print_color(self) -> None:
10 | self = "red"
| ^^^^ PLW0642
11 | self: Self = "red"
12 | self += "blue"
6 | cls += "orange" # PLW0642
7 | cls, blah = "apple", "orange" # PLW0642
8 | blah, (cls, blah2) = "apple", ("orange", "banana") # PLW0642
| ^^^ PLW0642
9 |
10 | def print_color(self) -> None:
|

self_cls_assignment.py:11:9: PLW0642 Assignment of variable `self`
|
9 | def print_color(self) -> None:
10 | self = "red"
11 | self: Self = "red"
10 | def print_color(self) -> None:
11 | self = "red" # PLW0642
| ^^^^ PLW0642
12 | self += "blue"
13 | self, blah = "red", "blue"
12 | self: Self = "red" # PLW0642
13 | self += "blue" # PLW0642
|

self_cls_assignment.py:12:9: PLW0642 Assignment of variable `self`
|
10 | self = "red"
11 | self: Self = "red"
12 | self += "blue"
10 | def print_color(self) -> None:
11 | self = "red" # PLW0642
12 | self: Self = "red" # PLW0642
| ^^^^ PLW0642
13 | self, blah = "red", "blue"
13 | self += "blue" # PLW0642
14 | self, blah = "red", "blue" # PLW0642
|

self_cls_assignment.py:13:9: PLW0642 Assignment of variable `self`
|
11 | self: Self = "red"
12 | self += "blue"
13 | self, blah = "red", "blue"
11 | self = "red" # PLW0642
12 | self: Self = "red" # PLW0642
13 | self += "blue" # PLW0642
| ^^^^ PLW0642
14 | self, blah = "red", "blue" # PLW0642
15 | blah, (self, blah2) = "apple", ("orange", "banana") # PLW0642
|

self_cls_assignment.py:14:9: PLW0642 Assignment of variable `self`
|
12 | self: Self = "red" # PLW0642
13 | self += "blue" # PLW0642
14 | self, blah = "red", "blue" # PLW0642
| ^^^^ PLW0642
14 |
15 | def ok(self) -> None:
15 | blah, (self, blah2) = "apple", ("orange", "banana") # PLW0642
|

self_cls_assignment.py:15:16: PLW0642 Assignment of variable `self`
|
13 | self += "blue" # PLW0642
14 | self, blah = "red", "blue" # PLW0642
15 | blah, (self, blah2) = "apple", ("orange", "banana") # PLW0642
| ^^^^ PLW0642
16 |
17 | def ok(self) -> None:
|


0 comments on commit 8fbfbf9

Please sign in to comment.