mirror of https://github.com/astral-sh/ruff
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:
parent
779b232db9
commit
ca26f664ec
|
|
@ -5,6 +5,9 @@
|
||||||
"yoda" <= compare # SIM300
|
"yoda" <= compare # SIM300
|
||||||
'yoda' < compare # SIM300
|
'yoda' < compare # SIM300
|
||||||
42 > age # SIM300
|
42 > age # SIM300
|
||||||
|
YODA == age # SIM300
|
||||||
|
YODA > age # SIM300
|
||||||
|
YODA >= age # SIM300
|
||||||
|
|
||||||
# OK
|
# OK
|
||||||
compare == "yoda"
|
compare == "yoda"
|
||||||
|
|
@ -13,3 +16,7 @@ x == y
|
||||||
"yoda" == compare == 1
|
"yoda" == compare == 1
|
||||||
"yoda" == compare == someothervar
|
"yoda" == compare == someothervar
|
||||||
"yoda" == "yoda"
|
"yoda" == "yoda"
|
||||||
|
age == YODA
|
||||||
|
age < YODA
|
||||||
|
age <= YODA
|
||||||
|
YODA == YODA
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,19 @@ use rustpython_ast::{Cmpop, Expr, ExprKind};
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
|
use crate::python::string::{self};
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::violations;
|
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
|
/// SIM300
|
||||||
pub fn yoda_conditions(
|
pub fn yoda_conditions(
|
||||||
checker: &mut Checker,
|
checker: &mut Checker,
|
||||||
|
|
@ -24,10 +34,8 @@ pub fn yoda_conditions(
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if !matches!(&left.node, &ExprKind::Constant { .. }) {
|
|
||||||
return;
|
if !is_constant(left) || is_constant(right) {
|
||||||
}
|
|
||||||
if matches!(&right.node, &ExprKind::Constant { .. }) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -116,4 +116,61 @@ expression: diagnostics
|
||||||
row: 7
|
row: 7
|
||||||
column: 8
|
column: 8
|
||||||
parent: ~
|
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: ~
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue