Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invert order of yoda-conditions message #1979

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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