From a6e19c29c9c89c27b83a403c86ee67a10dc233b2 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 18 Jan 2023 18:27:15 -0500 Subject: [PATCH] Invert order of yoda-conditions message --- README.md | 2 +- .../flake8_simplify/rules/yoda_conditions.rs | 20 ++++++++----------- ...ke8_simplify__tests__SIM300_SIM300.py.snap | 12 +++++------ src/violations.rs | 18 +++++++++++------ 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 208dd11137bd7..52cd272fe3872 100644 --- a/README.md +++ b/README.md @@ -1026,7 +1026,7 @@ For more, see [flake8-simplify](https://pypi.org/project/flake8-simplify/0.19.3/ | SIM221 | AOrNotA | Use `True` instead of `... or not ...` | 🛠 | | SIM222 | OrTrue | Use `True` instead of `... or True` | 🛠 | | SIM223 | AndFalse | Use `False` instead of `... and False` | 🛠 | -| SIM300 | YodaConditions | Yoda conditions are discouraged, use `left == right` instead | 🛠 | +| SIM300 | YodaConditions | Yoda conditions are discouraged, use `x == 1` instead | 🛠 | | SIM401 | DictGetWithDefault | Use `var = dict.get(key, "default")` instead of an `if` block | 🛠 | ### flake8-tidy-imports (TID) diff --git a/src/rules/flake8_simplify/rules/yoda_conditions.rs b/src/rules/flake8_simplify/rules/yoda_conditions.rs index 29cc571cbee7f..dccf39c74c7fb 100644 --- a/src/rules/flake8_simplify/rules/yoda_conditions.rs +++ b/src/rules/flake8_simplify/rules/yoda_conditions.rs @@ -17,42 +17,38 @@ pub fn yoda_conditions( if !matches!(ops[..], [Cmpop::Eq]) { return; } - if comparators.len() != 1 { return; } - if !matches!(left.node, ExprKind::Constant { .. }) { return; } - let right = comparators.first().unwrap(); - if matches!(left.node, ExprKind::Constant { .. }) - & matches!(right.node, ExprKind::Constant { .. }) - { + if matches!(right.node, ExprKind::Constant { .. }) { return; } // Slice exact content to preserve formatting. - let left_content = checker + let constant = checker .locator .slice_source_code_range(&Range::from_located(left)); - let right_content = checker + let variable = checker .locator .slice_source_code_range(&Range::from_located(right)); let mut diagnostic = Diagnostic::new( - violations::YodaConditions(left_content.to_string(), right_content.to_string()), + violations::YodaConditions { + constant: constant.to_string(), + variable: variable.to_string(), + }, Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { diagnostic.amend(Fix::replacement( - format!("{right_content} == {left_content}"), + format!("{variable} == {constant}"), left.location, right.end_location.unwrap(), )); } - checker.diagnostics.push(diagnostic); } diff --git a/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM300_SIM300.py.snap b/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM300_SIM300.py.snap index a51b234628732..e01845138b0b2 100644 --- a/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM300_SIM300.py.snap +++ b/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM300_SIM300.py.snap @@ -4,8 +4,8 @@ expression: diagnostics --- - kind: YodaConditions: - - "\"yoda\"" - - compare + variable: compare + constant: "\"yoda\"" location: row: 2 column: 0 @@ -23,8 +23,8 @@ expression: diagnostics parent: ~ - kind: YodaConditions: - - "'yoda'" - - compare + variable: compare + constant: "'yoda'" location: row: 3 column: 0 @@ -42,8 +42,8 @@ expression: diagnostics parent: ~ - kind: YodaConditions: - - "42" - - age + variable: age + constant: "42" location: row: 4 column: 0 diff --git a/src/violations.rs b/src/violations.rs index e35498eb31314..29e0a90bf0236 100644 --- a/src/violations.rs +++ b/src/violations.rs @@ -3134,21 +3134,27 @@ impl AlwaysAutofixableViolation for AndFalse { } define_violation!( - pub struct YodaConditions(pub String, pub String); + pub struct YodaConditions { + pub variable: String, + pub constant: String, + } ); impl AlwaysAutofixableViolation for YodaConditions { fn message(&self) -> String { - let YodaConditions(left, right) = self; - format!("Yoda conditions are discouraged, use `{left} == {right}` instead") + let YodaConditions { variable, constant } = self; + format!("Yoda conditions are discouraged, use `{variable} == {constant}` instead") } fn autofix_title(&self) -> String { - let YodaConditions(left, right) = self; - format!("Replace Yoda condition with `{left} == {right}`") + let YodaConditions { variable, constant } = self; + format!("Replace Yoda condition with `{variable} == {constant}`") } fn placeholder() -> Self { - YodaConditions("left".to_string(), "right".to_string()) + YodaConditions { + variable: "x".to_string(), + constant: "1".to_string(), + } } }