Skip to content

Commit 04c8597

Browse files
charliermarshAlexWaygood
authored andcommitted
[flake8-simplify] Stabilize detection of Yoda conditions for "constant" collections (SIM300) (#12050)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 4029a25 commit 04c8597

File tree

4 files changed

+69
-456
lines changed

4 files changed

+69
-456
lines changed

crates/ruff_linter/src/rules/flake8_simplify/mod.rs

-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ mod tests {
99
use test_case::test_case;
1010

1111
use crate::registry::Rule;
12-
use crate::settings::types::PreviewMode;
1312
use crate::test::test_path;
1413
use crate::{assert_messages, settings};
1514

@@ -55,22 +54,4 @@ mod tests {
5554
assert_messages!(snapshot, diagnostics);
5655
Ok(())
5756
}
58-
59-
#[test_case(Rule::YodaConditions, Path::new("SIM300.py"))]
60-
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
61-
let snapshot = format!(
62-
"preview__{}_{}",
63-
rule_code.noqa_code(),
64-
path.to_string_lossy()
65-
);
66-
let diagnostics = test_path(
67-
Path::new("flake8_simplify").join(path).as_path(),
68-
&settings::LinterSettings {
69-
preview: PreviewMode::Enabled,
70-
..settings::LinterSettings::for_rule(rule_code)
71-
},
72-
)?;
73-
assert_messages!(snapshot, diagnostics);
74-
Ok(())
75-
}
7657
}

crates/ruff_linter/src/rules/flake8_simplify/rules/yoda_conditions.rs

+17-29
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::cst::helpers::or_space;
1616
use crate::cst::matchers::{match_comparison, transform_expression};
1717
use crate::fix::edits::pad;
1818
use crate::fix::snippet::SourceCodeSnippet;
19-
use crate::settings::types::PreviewMode;
2019

2120
/// ## What it does
2221
/// Checks for conditions that position a constant on the left-hand side of the
@@ -58,26 +57,15 @@ impl Violation for YodaConditions {
5857

5958
#[derive_message_formats]
6059
fn message(&self) -> String {
61-
let YodaConditions { suggestion } = self;
62-
if let Some(suggestion) = suggestion
63-
.as_ref()
64-
.and_then(SourceCodeSnippet::full_display)
65-
{
66-
format!("Yoda conditions are discouraged, use `{suggestion}` instead")
67-
} else {
68-
format!("Yoda conditions are discouraged")
69-
}
60+
format!("Yoda condition detected")
7061
}
7162

7263
fn fix_title(&self) -> Option<String> {
7364
let YodaConditions { suggestion } = self;
74-
suggestion.as_ref().map(|suggestion| {
75-
if let Some(suggestion) = suggestion.full_display() {
76-
format!("Replace Yoda condition with `{suggestion}`")
77-
} else {
78-
format!("Replace Yoda condition")
79-
}
80-
})
65+
suggestion
66+
.as_ref()
67+
.and_then(|suggestion| suggestion.full_display())
68+
.map(|suggestion| format!("Rewrite as `{suggestion}`"))
8169
}
8270
}
8371

@@ -94,9 +82,9 @@ enum ConstantLikelihood {
9482
Definitely = 2,
9583
}
9684

97-
impl ConstantLikelihood {
85+
impl From<&Expr> for ConstantLikelihood {
9886
/// Determine the [`ConstantLikelihood`] of an expression.
99-
fn from_expression(expr: &Expr, preview: PreviewMode) -> Self {
87+
fn from(expr: &Expr) -> Self {
10088
match expr {
10189
_ if expr.is_literal_expr() => ConstantLikelihood::Definitely,
10290
Expr::Attribute(ast::ExprAttribute { attr, .. }) => {
@@ -105,34 +93,36 @@ impl ConstantLikelihood {
10593
Expr::Name(ast::ExprName { id, .. }) => ConstantLikelihood::from_identifier(id),
10694
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts
10795
.iter()
108-
.map(|expr| ConstantLikelihood::from_expression(expr, preview))
96+
.map(ConstantLikelihood::from)
10997
.min()
11098
.unwrap_or(ConstantLikelihood::Definitely),
111-
Expr::List(ast::ExprList { elts, .. }) if preview.is_enabled() => elts
99+
Expr::List(ast::ExprList { elts, .. }) => elts
112100
.iter()
113-
.map(|expr| ConstantLikelihood::from_expression(expr, preview))
101+
.map(ConstantLikelihood::from)
114102
.min()
115103
.unwrap_or(ConstantLikelihood::Definitely),
116-
Expr::Dict(ast::ExprDict { items, .. }) if preview.is_enabled() => {
104+
Expr::Dict(ast::ExprDict { items, .. }) => {
117105
if items.is_empty() {
118106
ConstantLikelihood::Definitely
119107
} else {
120108
ConstantLikelihood::Probably
121109
}
122110
}
123111
Expr::BinOp(ast::ExprBinOp { left, right, .. }) => cmp::min(
124-
ConstantLikelihood::from_expression(left, preview),
125-
ConstantLikelihood::from_expression(right, preview),
112+
ConstantLikelihood::from(&**left),
113+
ConstantLikelihood::from(&**right),
126114
),
127115
Expr::UnaryOp(ast::ExprUnaryOp {
128116
op: UnaryOp::UAdd | UnaryOp::USub | UnaryOp::Invert,
129117
operand,
130118
range: _,
131-
}) => ConstantLikelihood::from_expression(operand, preview),
119+
}) => ConstantLikelihood::from(&**operand),
132120
_ => ConstantLikelihood::Unlikely,
133121
}
134122
}
123+
}
135124

125+
impl ConstantLikelihood {
136126
/// Determine the [`ConstantLikelihood`] of an identifier.
137127
fn from_identifier(identifier: &str) -> Self {
138128
if str::is_cased_uppercase(identifier) {
@@ -230,9 +220,7 @@ pub(crate) fn yoda_conditions(
230220
return;
231221
}
232222

233-
if ConstantLikelihood::from_expression(left, checker.settings.preview)
234-
<= ConstantLikelihood::from_expression(right, checker.settings.preview)
235-
{
223+
if ConstantLikelihood::from(left) <= ConstantLikelihood::from(right) {
236224
return;
237225
}
238226

0 commit comments

Comments
 (0)