mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 21:40:51 -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
|
||||
|
||||
@@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user