mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 02:12:22 +00:00
Treat attribute constants as constant for yoda-conditions (#2266)
Accessed attributes that are Python constants should be considered for yoda-conditions ```py ## Error JediOrder.YODA == age # SIM300 ## OK age == JediOrder.YODA ``` ~~PS: This PR will fail CI, as the `main` branch currently failing.~~
This commit is contained in:
parent
2c415016a6
commit
8149c8cbc4
3 changed files with 22 additions and 0 deletions
|
@ -9,6 +9,7 @@
|
|||
YODA == age # SIM300
|
||||
YODA > age # SIM300
|
||||
YODA >= age # SIM300
|
||||
JediOrder.YODA == age # SIM300
|
||||
|
||||
# OK
|
||||
compare == "yoda"
|
||||
|
@ -22,3 +23,4 @@ age == YODA
|
|||
age < YODA
|
||||
age <= YODA
|
||||
YODA == YODA
|
||||
age == JediOrder.YODA
|
||||
|
|
|
@ -10,6 +10,7 @@ use crate::violations;
|
|||
/// Return `true` if an [`Expr`] is a constant or a constant-like name.
|
||||
fn is_constant_like(expr: &Expr) -> bool {
|
||||
match &expr.node {
|
||||
ExprKind::Attribute { attr, .. } => string::is_upper(attr),
|
||||
ExprKind::Constant { .. } => true,
|
||||
ExprKind::Tuple { elts, .. } => elts.iter().all(is_constant_like),
|
||||
ExprKind::Name { id, .. } => string::is_upper(id),
|
||||
|
|
|
@ -192,4 +192,23 @@ expression: diagnostics
|
|||
row: 11
|
||||
column: 11
|
||||
parent: ~
|
||||
- kind:
|
||||
YodaConditions:
|
||||
suggestion: age == JediOrder.YODA
|
||||
location:
|
||||
row: 12
|
||||
column: 0
|
||||
end_location:
|
||||
row: 12
|
||||
column: 21
|
||||
fix:
|
||||
content:
|
||||
- age == JediOrder.YODA
|
||||
location:
|
||||
row: 12
|
||||
column: 0
|
||||
end_location:
|
||||
row: 12
|
||||
column: 21
|
||||
parent: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue