mirror of
https://github.com/astral-sh/ruff
synced 2026-01-24 06:50:59 -05:00
Invert order of yoda-conditions message (#1979)
The suggestion was wrong!
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user