Fix SIM300 to take Python constants into account (#2255)

SIM300 currently doesn't take Python constants into account when looking for Yoda conditions, this PR fixes that behavior.

```python
# Errors
YODA == age  # SIM300
YODA > age  # SIM300
YODA >= age  # SIM300

# OK
age == YODA
age < YODA
age <= YODA
```

Ref: <https://github.com/home-assistant/core/pull/86793>
This commit is contained in:
Franck Nijhof 2023-01-27 17:20:21 +01:00 committed by GitHub
parent 779b232db9
commit ca26f664ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 4 deletions

View File

@ -5,6 +5,9 @@
"yoda" <= compare # SIM300
'yoda' < compare # SIM300
42 > age # SIM300
YODA == age # SIM300
YODA > age # SIM300
YODA >= age # SIM300
# OK
compare == "yoda"
@ -13,3 +16,7 @@ x == y
"yoda" == compare == 1
"yoda" == compare == someothervar
"yoda" == "yoda"
age == YODA
age < YODA
age <= YODA
YODA == YODA

View File

@ -3,9 +3,19 @@ use rustpython_ast::{Cmpop, Expr, ExprKind};
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::fix::Fix;
use crate::python::string::{self};
use crate::registry::Diagnostic;
use crate::violations;
/// Return `true` if an [`Expr`] is a constant or a constant-like name.
fn is_constant(expr: &Expr) -> bool {
match &expr.node {
ExprKind::Constant { .. } => true,
ExprKind::Name { id, .. } => string::is_upper(id),
_ => false,
}
}
/// SIM300
pub fn yoda_conditions(
checker: &mut Checker,
@ -24,10 +34,8 @@ pub fn yoda_conditions(
) {
return;
}
if !matches!(&left.node, &ExprKind::Constant { .. }) {
return;
}
if matches!(&right.node, &ExprKind::Constant { .. }) {
if !is_constant(left) || is_constant(right) {
return;
}

View File

@ -116,4 +116,61 @@ expression: diagnostics
row: 7
column: 8
parent: ~
- kind:
YodaConditions:
suggestion: age == YODA
location:
row: 8
column: 0
end_location:
row: 8
column: 11
fix:
content:
- age == YODA
location:
row: 8
column: 0
end_location:
row: 8
column: 11
parent: ~
- kind:
YodaConditions:
suggestion: age < YODA
location:
row: 9
column: 0
end_location:
row: 9
column: 10
fix:
content:
- age < YODA
location:
row: 9
column: 0
end_location:
row: 9
column: 10
parent: ~
- kind:
YodaConditions:
suggestion: age <= YODA
location:
row: 10
column: 0
end_location:
row: 10
column: 11
fix:
content:
- age <= YODA
location:
row: 10
column: 0
end_location:
row: 10
column: 11
parent: ~