diff --git a/README.md b/README.md index 31daf84428..2b20a24dac 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/flake8_simplify/plugins/yoda_conditions.rs b/src/flake8_simplify/plugins/yoda_conditions.rs index fe8e17813a..958cdf4f6c 100644 --- a/src/flake8_simplify/plugins/yoda_conditions.rs +++ b/src/flake8_simplify/plugins/yoda_conditions.rs @@ -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); } diff --git a/src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM300_SIM300.py.snap b/src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM300_SIM300.py.snap index a154666829..0a663742eb 100644 --- a/src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM300_SIM300.py.snap +++ b/src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM300_SIM300.py.snap @@ -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: ~ diff --git a/src/registry.rs b/src/registry.rs index 55dbb6d345..87909760a8 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -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, } }