Skip to content

Commit f20d098

Browse files
dylwil3ntBre
authored andcommitted
[flake8-simplify] Stabilize further simplification to binary expressions in autofix for if-else-block-instead-of-if-exp (SIM108) (#18506)
1 parent a70d40c commit f20d098

File tree

5 files changed

+22
-413
lines changed

5 files changed

+22
-413
lines changed

crates/ruff_linter/src/preview.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ pub(crate) const fn is_only_add_return_none_at_end_enabled(settings: &LinterSett
7373
settings.preview.is_enabled()
7474
}
7575

76-
// https://github.com/astral-sh/ruff/pull/12796
77-
pub(crate) const fn is_simplify_ternary_to_binary_enabled(settings: &LinterSettings) -> bool {
78-
settings.preview.is_enabled()
79-
}
80-
8176
// https://github.com/astral-sh/ruff/pull/16719
8277
pub(crate) const fn is_fix_manual_dict_comprehension_enabled(settings: &LinterSettings) -> bool {
8378
settings.preview.is_enabled()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ mod tests {
5858
Ok(())
5959
}
6060

61-
#[test_case(Rule::IfElseBlockInsteadOfIfExp, Path::new("SIM108.py"))]
6261
#[test_case(Rule::MultipleWithStatements, Path::new("SIM117.py"))]
6362
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
6463
let snapshot = format!(

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ use ruff_text_size::{Ranged, TextRange};
77

88
use crate::checkers::ast::Checker;
99
use crate::fix::edits::fits;
10-
use crate::preview::is_simplify_ternary_to_binary_enabled;
1110
use crate::{Edit, Fix, FixAvailability, Violation};
1211

1312
/// ## What it does
14-
/// Check for `if`-`else`-blocks that can be replaced with a ternary operator.
15-
/// Moreover, in [preview], check if these ternary expressions can be
16-
/// further simplified to binary expressions.
13+
/// Check for `if`-`else`-blocks that can be replaced with a ternary
14+
/// or binary operator.
15+
///
16+
/// The lint is suppressed if the suggested replacement would exceed
17+
/// the maximum line length configured in [pycodestyle.max-line-length].
1718
///
1819
/// ## Why is this bad?
1920
/// `if`-`else`-blocks that assign a value to a variable in both branches can
@@ -33,7 +34,7 @@ use crate::{Edit, Fix, FixAvailability, Violation};
3334
/// bar = x if foo else y
3435
/// ```
3536
///
36-
/// Or, in [preview]:
37+
/// Or:
3738
///
3839
/// ```python
3940
/// if cond:
@@ -57,8 +58,8 @@ use crate::{Edit, Fix, FixAvailability, Violation};
5758
/// ## References
5859
/// - [Python documentation: Conditional expressions](https://docs.python.org/3/reference/expressions.html#conditional-expressions)
5960
///
60-
/// [preview]: https://docs.astral.sh/ruff/preview/
6161
/// [code coverage]: https://github.com/nedbat/coveragepy/issues/509
62+
/// [pycodestyle.max-line-length]: https://docs.astral.sh/ruff/settings/#lint_pycodestyle_max-line-length
6263
#[derive(ViolationMetadata)]
6364
pub(crate) struct IfElseBlockInsteadOfIfExp {
6465
/// The ternary or binary expression to replace the `if`-`else`-block.
@@ -183,24 +184,20 @@ pub(crate) fn if_else_block_instead_of_if_exp(checker: &Checker, stmt_if: &ast::
183184
//
184185
// The match statement below implements the following
185186
// logic:
186-
// - If `test == body_value` and preview enabled, replace with `target_var = test or else_value`
187-
// - If `test == not body_value` and preview enabled, replace with `target_var = body_value and else_value`
188-
// - If `not test == body_value` and preview enabled, replace with `target_var = body_value and else_value`
187+
// - If `test == body_value`, replace with `target_var = test or else_value`
188+
// - If `test == not body_value`, replace with `target_var = body_value and else_value`
189+
// - If `not test == body_value`, replace with `target_var = body_value and else_value`
189190
// - Otherwise, replace with `target_var = body_value if test else else_value`
190-
let (contents, assignment_kind) = match (
191-
is_simplify_ternary_to_binary_enabled(checker.settings),
192-
test,
193-
body_value,
194-
) {
195-
(true, test_node, body_node)
191+
let (contents, assignment_kind) = match (test, body_value) {
192+
(test_node, body_node)
196193
if ComparableExpr::from(test_node) == ComparableExpr::from(body_node)
197194
&& !contains_effect(test_node, |id| checker.semantic().has_builtin_binding(id)) =>
198195
{
199196
let target_var = &body_target;
200197
let binary = assignment_binary_or(target_var, body_value, else_value);
201198
(checker.generator().stmt(&binary), AssignmentKind::Binary)
202199
}
203-
(true, test_node, body_node)
200+
(test_node, body_node)
204201
if (test_node.as_unary_op_expr().is_some_and(|op_expr| {
205202
op_expr.op.is_not()
206203
&& ComparableExpr::from(&op_expr.operand) == ComparableExpr::from(body_node)

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM108_SIM108.py.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ SIM108.py:117:1: SIM108 Use ternary operator `x = 3 if True else 5` instead of `
118118
|
119119
= help: Replace `if`-`else`-block with `x = 3 if True else 5`
120120

121-
SIM108.py:141:1: SIM108 [*] Use ternary operator `z = cond if cond else other_cond` instead of `if`-`else`-block
121+
SIM108.py:141:1: SIM108 [*] Use binary operator `z = cond or other_cond` instead of `if`-`else`-block
122122
|
123123
139 | # SIM108 - should suggest
124124
140 | # z = cond or other_cond
@@ -130,7 +130,7 @@ SIM108.py:141:1: SIM108 [*] Use ternary operator `z = cond if cond else other_co
130130
145 |
131131
146 | # SIM108 - should suggest
132132
|
133-
= help: Replace `if`-`else`-block with `z = cond if cond else other_cond`
133+
= help: Replace `if`-`else`-block with `z = cond or other_cond`
134134

135135
Unsafe fix
136136
138 138 |
@@ -140,12 +140,12 @@ SIM108.py:141:1: SIM108 [*] Use ternary operator `z = cond if cond else other_co
140140
142 |- z = cond
141141
143 |-else:
142142
144 |- z = other_cond
143-
141 |+z = cond if cond else other_cond
143+
141 |+z = cond or other_cond
144144
145 142 |
145145
146 143 | # SIM108 - should suggest
146146
147 144 | # z = cond and other_cond
147147

148-
SIM108.py:148:1: SIM108 [*] Use ternary operator `z = cond if not cond else other_cond` instead of `if`-`else`-block
148+
SIM108.py:148:1: SIM108 [*] Use binary operator `z = cond and other_cond` instead of `if`-`else`-block
149149
|
150150
146 | # SIM108 - should suggest
151151
147 | # z = cond and other_cond
@@ -157,7 +157,7 @@ SIM108.py:148:1: SIM108 [*] Use ternary operator `z = cond if not cond else othe
157157
152 |
158158
153 | # SIM108 - should suggest
159159
|
160-
= help: Replace `if`-`else`-block with `z = cond if not cond else other_cond`
160+
= help: Replace `if`-`else`-block with `z = cond and other_cond`
161161

162162
Unsafe fix
163163
145 145 |
@@ -167,12 +167,12 @@ SIM108.py:148:1: SIM108 [*] Use ternary operator `z = cond if not cond else othe
167167
149 |- z = cond
168168
150 |-else:
169169
151 |- z = other_cond
170-
148 |+z = cond if not cond else other_cond
170+
148 |+z = cond and other_cond
171171
152 149 |
172172
153 150 | # SIM108 - should suggest
173173
154 151 | # z = not cond and other_cond
174174

175-
SIM108.py:155:1: SIM108 [*] Use ternary operator `z = not cond if cond else other_cond` instead of `if`-`else`-block
175+
SIM108.py:155:1: SIM108 [*] Use binary operator `z = not cond and other_cond` instead of `if`-`else`-block
176176
|
177177
153 | # SIM108 - should suggest
178178
154 | # z = not cond and other_cond
@@ -184,7 +184,7 @@ SIM108.py:155:1: SIM108 [*] Use ternary operator `z = not cond if cond else othe
184184
159 |
185185
160 | # SIM108 does not suggest
186186
|
187-
= help: Replace `if`-`else`-block with `z = not cond if cond else other_cond`
187+
= help: Replace `if`-`else`-block with `z = not cond and other_cond`
188188

189189
Unsafe fix
190190
152 152 |
@@ -194,7 +194,7 @@ SIM108.py:155:1: SIM108 [*] Use ternary operator `z = not cond if cond else othe
194194
156 |- z = not cond
195195
157 |-else:
196196
158 |- z = other_cond
197-
155 |+z = not cond if cond else other_cond
197+
155 |+z = not cond and other_cond
198198
159 156 |
199199
160 157 | # SIM108 does not suggest
200200
161 158 | # a binary option in these cases,

0 commit comments

Comments
 (0)