Skip to content

Commit 0b15f17

Browse files
[flake8-simplify] More precise inference for dictionaries (SIM300) (#15164)
Co-authored-by: Micha Reiser <[email protected]>
1 parent 0caab81 commit 0b15f17

File tree

3 files changed

+67
-42
lines changed

3 files changed

+67
-42
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM300.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
0 < (number - 100) # SIM300
1515
B<A[0][0]or B
1616
B or(B)<A[0][0]
17+
{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
1718

1819
# Errors in preview
1920
['upper'] == UPPER_LIST
@@ -39,4 +40,7 @@
3940
(number - 100) > 0
4041
SECONDS_IN_DAY == 60 * 60 * 24 # Error in 0.1.8
4142
SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # Error in 0.1.8
42-
{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
43+
44+
# https://github.com/astral-sh/ruff/issues/14761
45+
{"": print(1)} == print(2)
46+
{0: 1, **print(2)} == print(4)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ impl From<&Expr> for ConstantLikelihood {
101101
.map(ConstantLikelihood::from)
102102
.min()
103103
.unwrap_or(ConstantLikelihood::Definitely),
104-
Expr::Dict(dict) => {
105-
if dict.is_empty() {
106-
ConstantLikelihood::Definitely
107-
} else {
108-
ConstantLikelihood::Probably
109-
}
110-
}
104+
Expr::Dict(dict) => dict
105+
.items
106+
.iter()
107+
.flat_map(|item| std::iter::once(&item.value).chain(item.key.as_ref()))
108+
.map(ConstantLikelihood::from)
109+
.min()
110+
.unwrap_or(ConstantLikelihood::Definitely),
111111
Expr::BinOp(ast::ExprBinOp { left, right, .. }) => cmp::min(
112112
ConstantLikelihood::from(&**left),
113113
ConstantLikelihood::from(&**right),

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

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ SIM300.py:14:1: SIM300 [*] Yoda condition detected
269269
14 |+(number - 100) > 0 # SIM300
270270
15 15 | B<A[0][0]or B
271271
16 16 | B or(B)<A[0][0]
272-
17 17 |
272+
17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
273273

274274
SIM300.py:15:1: SIM300 [*] Yoda condition detected
275275
|
@@ -278,6 +278,7 @@ SIM300.py:15:1: SIM300 [*] Yoda condition detected
278278
15 | B<A[0][0]or B
279279
| ^^^^^^^^^ SIM300
280280
16 | B or(B)<A[0][0]
281+
17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
281282
|
282283
= help: Rewrite as `A[0][0] > B`
283284

@@ -288,17 +289,16 @@ SIM300.py:15:1: SIM300 [*] Yoda condition detected
288289
15 |-B<A[0][0]or B
289290
15 |+A[0][0] > B or B
290291
16 16 | B or(B)<A[0][0]
291-
17 17 |
292-
18 18 | # Errors in preview
292+
17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
293+
18 18 |
293294

294295
SIM300.py:16:5: SIM300 [*] Yoda condition detected
295296
|
296297
14 | 0 < (number - 100) # SIM300
297298
15 | B<A[0][0]or B
298299
16 | B or(B)<A[0][0]
299300
| ^^^^^^^^^^^ SIM300
300-
17 |
301-
18 | # Errors in preview
301+
17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
302302
|
303303
= help: Rewrite as `A[0][0] > (B)`
304304

@@ -308,46 +308,67 @@ SIM300.py:16:5: SIM300 [*] Yoda condition detected
308308
15 15 | B<A[0][0]or B
309309
16 |-B or(B)<A[0][0]
310310
16 |+B or A[0][0] > (B)
311-
17 17 |
312-
18 18 | # Errors in preview
313-
19 19 | ['upper'] == UPPER_LIST
311+
17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
312+
18 18 |
313+
19 19 | # Errors in preview
314314

315-
SIM300.py:19:1: SIM300 [*] Yoda condition detected
315+
SIM300.py:17:1: SIM300 [*] Yoda condition detected
316316
|
317-
18 | # Errors in preview
318-
19 | ['upper'] == UPPER_LIST
319-
| ^^^^^^^^^^^^^^^^^^^^^^^ SIM300
320-
20 | {} == DummyHandler.CONFIG
317+
15 | B<A[0][0]or B
318+
16 | B or(B)<A[0][0]
319+
17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
320+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM300
321+
18 |
322+
19 | # Errors in preview
321323
|
322-
= help: Rewrite as `UPPER_LIST == ['upper']`
324+
= help: Rewrite as `DummyHandler.CONFIG == {"non-empty-dict": "is-ok"}`
323325

324326
Safe fix
327+
14 14 | 0 < (number - 100) # SIM300
328+
15 15 | B<A[0][0]or B
325329
16 16 | B or(B)<A[0][0]
326-
17 17 |
327-
18 18 | # Errors in preview
328-
19 |-['upper'] == UPPER_LIST
329-
19 |+UPPER_LIST == ['upper']
330-
20 20 | {} == DummyHandler.CONFIG
331-
21 21 |
332-
22 22 | # Errors in stable
330+
17 |-{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
331+
17 |+DummyHandler.CONFIG == {"non-empty-dict": "is-ok"}
332+
18 18 |
333+
19 19 | # Errors in preview
334+
20 20 | ['upper'] == UPPER_LIST
333335

334336
SIM300.py:20:1: SIM300 [*] Yoda condition detected
335337
|
336-
18 | # Errors in preview
337-
19 | ['upper'] == UPPER_LIST
338-
20 | {} == DummyHandler.CONFIG
338+
19 | # Errors in preview
339+
20 | ['upper'] == UPPER_LIST
340+
| ^^^^^^^^^^^^^^^^^^^^^^^ SIM300
341+
21 | {} == DummyHandler.CONFIG
342+
|
343+
= help: Rewrite as `UPPER_LIST == ['upper']`
344+
345+
Safe fix
346+
17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
347+
18 18 |
348+
19 19 | # Errors in preview
349+
20 |-['upper'] == UPPER_LIST
350+
20 |+UPPER_LIST == ['upper']
351+
21 21 | {} == DummyHandler.CONFIG
352+
22 22 |
353+
23 23 | # Errors in stable
354+
355+
SIM300.py:21:1: SIM300 [*] Yoda condition detected
356+
|
357+
19 | # Errors in preview
358+
20 | ['upper'] == UPPER_LIST
359+
21 | {} == DummyHandler.CONFIG
339360
| ^^^^^^^^^^^^^^^^^^^^^^^^^ SIM300
340-
21 |
341-
22 | # Errors in stable
361+
22 |
362+
23 | # Errors in stable
342363
|
343364
= help: Rewrite as `DummyHandler.CONFIG == {}`
344365

345366
Safe fix
346-
17 17 |
347-
18 18 | # Errors in preview
348-
19 19 | ['upper'] == UPPER_LIST
349-
20 |-{} == DummyHandler.CONFIG
350-
20 |+DummyHandler.CONFIG == {}
351-
21 21 |
352-
22 22 | # Errors in stable
353-
23 23 | UPPER_LIST == ['upper']
367+
18 18 |
368+
19 19 | # Errors in preview
369+
20 20 | ['upper'] == UPPER_LIST
370+
21 |-{} == DummyHandler.CONFIG
371+
21 |+DummyHandler.CONFIG == {}
372+
22 22 |
373+
23 23 | # Errors in stable
374+
24 24 | UPPER_LIST == ['upper']

0 commit comments

Comments
 (0)