Skip to content

Commit c19cd58

Browse files
committed
[flake8-simplify] Avoid double negation in fixes (SIM103) (#16684)
## Summary This PR stabilizes the fixes improvements made in #15562 (released with ruff 0.9.3 in mid January). There's no open issue or PR related to the changed fix behavior. This is not a breaking change. The fix was only gated behind preview to get some more test coverage before releasing.
1 parent 8155197 commit c19cd58

File tree

4 files changed

+28
-416
lines changed

4 files changed

+28
-416
lines changed

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::NeedlessBool, Path::new("SIM103.py"))]
6261
#[test_case(Rule::IfElseBlockInsteadOfIfExp, Path::new("SIM108.py"))]
6362
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
6463
let snapshot = format!(

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::fix::snippet::SourceCodeSnippet;
1414
///
1515
/// ## Why is this bad?
1616
/// `if` statements that return `True` for a truthy condition and `False` for
17-
/// a falsey condition can be replaced with boolean casts.
17+
/// a falsy condition can be replaced with boolean casts.
1818
///
1919
/// ## Example
2020
/// Given:
@@ -42,10 +42,6 @@ use crate::fix::snippet::SourceCodeSnippet;
4242
/// return x > 0
4343
/// ```
4444
///
45-
/// ## Preview
46-
/// In preview, double negations such as `not a != b`, `not a not in b`, `not a is not b`
47-
/// will be simplified to `a == b`, `a in b` and `a is b`, respectively.
48-
///
4945
/// ## References
5046
/// - [Python documentation: Truth Value Testing](https://docs.python.org/3/library/stdtypes.html#truth-value-testing)
5147
#[derive(ViolationMetadata)]
@@ -222,16 +218,15 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
222218
left,
223219
comparators,
224220
..
225-
}) if checker.settings.preview.is_enabled()
226-
&& matches!(
227-
ops.as_ref(),
228-
[ast::CmpOp::Eq
229-
| ast::CmpOp::NotEq
230-
| ast::CmpOp::In
231-
| ast::CmpOp::NotIn
232-
| ast::CmpOp::Is
233-
| ast::CmpOp::IsNot]
234-
) =>
221+
}) if matches!(
222+
ops.as_ref(),
223+
[ast::CmpOp::Eq
224+
| ast::CmpOp::NotEq
225+
| ast::CmpOp::In
226+
| ast::CmpOp::NotIn
227+
| ast::CmpOp::Is
228+
| ast::CmpOp::IsNot]
229+
) =>
235230
{
236231
let ([op], [right]) = (ops.as_ref(), comparators.as_ref()) else {
237232
unreachable!("Single comparison with multiple comparators");

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ SIM103.py:123:5: SIM103 [*] Return the condition `not 10 < a` directly
252252
127 125 |
253253
128 126 | def f():
254254

255-
SIM103.py:129:5: SIM103 [*] Return the condition `not 10 in a` directly
255+
SIM103.py:129:5: SIM103 [*] Return the condition `10 not in a` directly
256256
|
257257
128 | def f():
258258
129 | / if 10 in a:
259259
130 | | return False
260260
131 | | return True
261261
| |_______________^ SIM103
262262
|
263-
= help: Replace with `return not 10 in a`
263+
= help: Replace with `return 10 not in a`
264264

265265
Unsafe fix
266266
126 126 |
@@ -269,20 +269,20 @@ SIM103.py:129:5: SIM103 [*] Return the condition `not 10 in a` directly
269269
129 |- if 10 in a:
270270
130 |- return False
271271
131 |- return True
272-
129 |+ return not 10 in a
272+
129 |+ return 10 not in a
273273
132 130 |
274274
133 131 |
275275
134 132 | def f():
276276

277-
SIM103.py:135:5: SIM103 [*] Return the condition `not 10 not in a` directly
277+
SIM103.py:135:5: SIM103 [*] Return the condition `10 in a` directly
278278
|
279279
134 | def f():
280280
135 | / if 10 not in a:
281281
136 | | return False
282282
137 | | return True
283283
| |_______________^ SIM103
284284
|
285-
= help: Replace with `return not 10 not in a`
285+
= help: Replace with `return 10 in a`
286286

287287
Unsafe fix
288288
132 132 |
@@ -291,20 +291,20 @@ SIM103.py:135:5: SIM103 [*] Return the condition `not 10 not in a` directly
291291
135 |- if 10 not in a:
292292
136 |- return False
293293
137 |- return True
294-
135 |+ return not 10 not in a
294+
135 |+ return 10 in a
295295
138 136 |
296296
139 137 |
297297
140 138 | def f():
298298

299-
SIM103.py:141:5: SIM103 [*] Return the condition `not a is 10` directly
299+
SIM103.py:141:5: SIM103 [*] Return the condition `a is not 10` directly
300300
|
301301
140 | def f():
302302
141 | / if a is 10:
303303
142 | | return False
304304
143 | | return True
305305
| |_______________^ SIM103
306306
|
307-
= help: Replace with `return not a is 10`
307+
= help: Replace with `return a is not 10`
308308

309309
Unsafe fix
310310
138 138 |
@@ -313,20 +313,20 @@ SIM103.py:141:5: SIM103 [*] Return the condition `not a is 10` directly
313313
141 |- if a is 10:
314314
142 |- return False
315315
143 |- return True
316-
141 |+ return not a is 10
316+
141 |+ return a is not 10
317317
144 142 |
318318
145 143 |
319319
146 144 | def f():
320320

321-
SIM103.py:147:5: SIM103 [*] Return the condition `not a is not 10` directly
321+
SIM103.py:147:5: SIM103 [*] Return the condition `a is 10` directly
322322
|
323323
146 | def f():
324324
147 | / if a is not 10:
325325
148 | | return False
326326
149 | | return True
327327
| |_______________^ SIM103
328328
|
329-
= help: Replace with `return not a is not 10`
329+
= help: Replace with `return a is 10`
330330

331331
Unsafe fix
332332
144 144 |
@@ -335,20 +335,20 @@ SIM103.py:147:5: SIM103 [*] Return the condition `not a is not 10` directly
335335
147 |- if a is not 10:
336336
148 |- return False
337337
149 |- return True
338-
147 |+ return not a is not 10
338+
147 |+ return a is 10
339339
150 148 |
340340
151 149 |
341341
152 150 | def f():
342342

343-
SIM103.py:153:5: SIM103 [*] Return the condition `not a == 10` directly
343+
SIM103.py:153:5: SIM103 [*] Return the condition `a != 10` directly
344344
|
345345
152 | def f():
346346
153 | / if a == 10:
347347
154 | | return False
348348
155 | | return True
349349
| |_______________^ SIM103
350350
|
351-
= help: Replace with `return not a == 10`
351+
= help: Replace with `return a != 10`
352352

353353
Unsafe fix
354354
150 150 |
@@ -357,20 +357,20 @@ SIM103.py:153:5: SIM103 [*] Return the condition `not a == 10` directly
357357
153 |- if a == 10:
358358
154 |- return False
359359
155 |- return True
360-
153 |+ return not a == 10
360+
153 |+ return a != 10
361361
156 154 |
362362
157 155 |
363363
158 156 | def f():
364364

365-
SIM103.py:159:5: SIM103 [*] Return the condition `not a != 10` directly
365+
SIM103.py:159:5: SIM103 [*] Return the condition `a == 10` directly
366366
|
367367
158 | def f():
368368
159 | / if a != 10:
369369
160 | | return False
370370
161 | | return True
371371
| |_______________^ SIM103
372372
|
373-
= help: Replace with `return not a != 10`
373+
= help: Replace with `return a == 10`
374374

375375
Unsafe fix
376376
156 156 |
@@ -379,4 +379,4 @@ SIM103.py:159:5: SIM103 [*] Return the condition `not a != 10` directly
379379
159 |- if a != 10:
380380
160 |- return False
381381
161 |- return True
382-
159 |+ return not a != 10
382+
159 |+ return a == 10

0 commit comments

Comments
 (0)