mirror of https://github.com/astral-sh/ruff
Add autofix for SIM300 (#1588)
This commit is contained in:
parent
bfdab4ac94
commit
da5a25b421
|
|
@ -960,7 +960,7 @@ For more, see [flake8-simplify](https://pypi.org/project/flake8-simplify/0.19.3/
|
|||
| SIM118 | KeyInDict | Use `key in dict` instead of `key in dict.keys()` | 🛠 |
|
||||
| SIM222 | OrTrue | Use `True` instead of `... or True` | 🛠 |
|
||||
| SIM223 | AndFalse | Use `False` instead of `... and False` | 🛠 |
|
||||
| SIM300 | YodaConditions | Use `left == right` instead of `right == left (Yoda-conditions)` | |
|
||||
| SIM300 | YodaConditions | Use `left == right` instead of `right == left (Yoda-conditions)` | 🛠 |
|
||||
|
||||
### flake8-tidy-imports (TID)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
use rustpython_ast::{Cmpop, Expr, ExprKind};
|
||||
use rustpython_ast::{Cmpop, Expr, ExprKind, Location};
|
||||
|
||||
use crate::ast::types::Range;
|
||||
use crate::autofix::Fix;
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::registry::{Check, CheckKind};
|
||||
use crate::source_code_generator::SourceCodeGenerator;
|
||||
|
||||
/// SIM300
|
||||
pub fn yoda_conditions(
|
||||
|
|
@ -31,10 +33,36 @@ pub fn yoda_conditions(
|
|||
return;
|
||||
}
|
||||
|
||||
let check = Check::new(
|
||||
let mut check = Check::new(
|
||||
CheckKind::YodaConditions(left.to_string(), right.to_string()),
|
||||
Range::from_located(expr),
|
||||
);
|
||||
|
||||
if checker.patch(check.kind.code()) {
|
||||
let cmp = Expr::new(
|
||||
Location::default(),
|
||||
Location::default(),
|
||||
ExprKind::Compare {
|
||||
left: Box::new(right.clone()),
|
||||
ops: vec![Cmpop::Eq],
|
||||
comparators: vec![left.clone()],
|
||||
},
|
||||
);
|
||||
let mut generator = SourceCodeGenerator::new(
|
||||
checker.style.indentation(),
|
||||
checker.style.quote(),
|
||||
checker.style.line_ending(),
|
||||
);
|
||||
generator.unparse_expr(&cmp, 0);
|
||||
|
||||
if let Ok(content) = generator.generate() {
|
||||
check.amend(Fix::replacement(
|
||||
content,
|
||||
left.location,
|
||||
right.end_location.unwrap(),
|
||||
));
|
||||
};
|
||||
}
|
||||
|
||||
checker.add_check(check);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,14 @@ expression: checks
|
|||
end_location:
|
||||
row: 2
|
||||
column: 17
|
||||
fix: ~
|
||||
fix:
|
||||
content: "compare == \"yoda\""
|
||||
location:
|
||||
row: 2
|
||||
column: 0
|
||||
end_location:
|
||||
row: 2
|
||||
column: 17
|
||||
parent: ~
|
||||
- kind:
|
||||
YodaConditions:
|
||||
|
|
@ -24,6 +31,13 @@ expression: checks
|
|||
end_location:
|
||||
row: 3
|
||||
column: 9
|
||||
fix: ~
|
||||
fix:
|
||||
content: age == 42
|
||||
location:
|
||||
row: 3
|
||||
column: 0
|
||||
end_location:
|
||||
row: 3
|
||||
column: 9
|
||||
parent: ~
|
||||
|
||||
|
|
|
|||
|
|
@ -3594,6 +3594,7 @@ impl CheckKind {
|
|||
| CheckKind::UselessImportAlias
|
||||
| CheckKind::UselessMetaclassType
|
||||
| CheckKind::UselessObjectInheritance(..)
|
||||
| CheckKind::YodaConditions(..)
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -3856,6 +3857,9 @@ impl CheckKind {
|
|||
CheckKind::UselessObjectInheritance(..) => {
|
||||
Some("Remove `object` inheritance".to_string())
|
||||
}
|
||||
CheckKind::YodaConditions(left, right) => Some(format!(
|
||||
"Replace with `{left} == {right}` (Yoda-conditions)`"
|
||||
)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue