Skip to content

Commit

Permalink
Invert order of yoda-conditions message (#1979)
Browse files Browse the repository at this point in the history
The suggestion was wrong!
  • Loading branch information
charliermarsh authored Jan 18, 2023
1 parent ef355e5 commit 7628876
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 8 additions & 12 deletions src/rules/flake8_simplify/rules/yoda_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ expression: diagnostics
---
- kind:
YodaConditions:
- "\"yoda\""
- compare
variable: compare
constant: "\"yoda\""
location:
row: 2
column: 0
Expand All @@ -23,8 +23,8 @@ expression: diagnostics
parent: ~
- kind:
YodaConditions:
- "'yoda'"
- compare
variable: compare
constant: "'yoda'"
location:
row: 3
column: 0
Expand All @@ -42,8 +42,8 @@ expression: diagnostics
parent: ~
- kind:
YodaConditions:
- "42"
- age
variable: age
constant: "42"
location:
row: 4
column: 0
Expand Down
18 changes: 12 additions & 6 deletions src/violations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}

Expand Down

0 comments on commit 7628876

Please sign in to comment.