-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make SIM401 catch ternary operations (#7415)
## Summary Make SIM401 rules to catch ternary operations when preview is enabled. Fixes #7288. ## Test Plan Tested against `SIM401.py` fixtures.
- Loading branch information
Showing
5 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
...lify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM401_SIM401.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs | ||
--- | ||
SIM401.py:6:1: SIM401 [*] Use `var = a_dict.get(key, "default1")` instead of an `if` block | ||
| | ||
5 | # SIM401 (pattern-1) | ||
6 | / if key in a_dict: | ||
7 | | var = a_dict[key] | ||
8 | | else: | ||
9 | | var = "default1" | ||
| |____________________^ SIM401 | ||
10 | | ||
11 | # SIM401 (pattern-2) | ||
| | ||
= help: Replace with `var = a_dict.get(key, "default1")` | ||
|
||
ℹ Suggested fix | ||
3 3 | ### | ||
4 4 | | ||
5 5 | # SIM401 (pattern-1) | ||
6 |-if key in a_dict: | ||
7 |- var = a_dict[key] | ||
8 |-else: | ||
9 |- var = "default1" | ||
6 |+var = a_dict.get(key, "default1") | ||
10 7 | | ||
11 8 | # SIM401 (pattern-2) | ||
12 9 | if key not in a_dict: | ||
|
||
SIM401.py:12:1: SIM401 [*] Use `var = a_dict.get(key, "default2")` instead of an `if` block | ||
| | ||
11 | # SIM401 (pattern-2) | ||
12 | / if key not in a_dict: | ||
13 | | var = "default2" | ||
14 | | else: | ||
15 | | var = a_dict[key] | ||
| |_____________________^ SIM401 | ||
16 | | ||
17 | # OK (default contains effect) | ||
| | ||
= help: Replace with `var = a_dict.get(key, "default2")` | ||
|
||
ℹ Suggested fix | ||
9 9 | var = "default1" | ||
10 10 | | ||
11 11 | # SIM401 (pattern-2) | ||
12 |-if key not in a_dict: | ||
13 |- var = "default2" | ||
14 |-else: | ||
15 |- var = a_dict[key] | ||
12 |+var = a_dict.get(key, "default2") | ||
16 13 | | ||
17 14 | # OK (default contains effect) | ||
18 15 | if key in a_dict: | ||
|
||
SIM401.py:24:1: SIM401 [*] Use `var = a_dict.get(keys[idx], "default")` instead of an `if` block | ||
| | ||
23 | # SIM401 (complex expression in key) | ||
24 | / if keys[idx] in a_dict: | ||
25 | | var = a_dict[keys[idx]] | ||
26 | | else: | ||
27 | | var = "default" | ||
| |___________________^ SIM401 | ||
28 | | ||
29 | # SIM401 (complex expression in dict) | ||
| | ||
= help: Replace with `var = a_dict.get(keys[idx], "default")` | ||
|
||
ℹ Suggested fix | ||
21 21 | var = val1 + val2 | ||
22 22 | | ||
23 23 | # SIM401 (complex expression in key) | ||
24 |-if keys[idx] in a_dict: | ||
25 |- var = a_dict[keys[idx]] | ||
26 |-else: | ||
27 |- var = "default" | ||
24 |+var = a_dict.get(keys[idx], "default") | ||
28 25 | | ||
29 26 | # SIM401 (complex expression in dict) | ||
30 27 | if key in dicts[idx]: | ||
|
||
SIM401.py:30:1: SIM401 [*] Use `var = dicts[idx].get(key, "default")` instead of an `if` block | ||
| | ||
29 | # SIM401 (complex expression in dict) | ||
30 | / if key in dicts[idx]: | ||
31 | | var = dicts[idx][key] | ||
32 | | else: | ||
33 | | var = "default" | ||
| |___________________^ SIM401 | ||
34 | | ||
35 | # SIM401 (complex expression in var) | ||
| | ||
= help: Replace with `var = dicts[idx].get(key, "default")` | ||
|
||
ℹ Suggested fix | ||
27 27 | var = "default" | ||
28 28 | | ||
29 29 | # SIM401 (complex expression in dict) | ||
30 |-if key in dicts[idx]: | ||
31 |- var = dicts[idx][key] | ||
32 |-else: | ||
33 |- var = "default" | ||
30 |+var = dicts[idx].get(key, "default") | ||
34 31 | | ||
35 32 | # SIM401 (complex expression in var) | ||
36 33 | if key in a_dict: | ||
|
||
SIM401.py:36:1: SIM401 [*] Use `vars[idx] = a_dict.get(key, "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789")` instead of an `if` block | ||
| | ||
35 | # SIM401 (complex expression in var) | ||
36 | / if key in a_dict: | ||
37 | | vars[idx] = a_dict[key] | ||
38 | | else: | ||
39 | | vars[idx] = "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789" | ||
| |___________________________________________________________________________^ SIM401 | ||
40 | | ||
41 | # SIM401 | ||
| | ||
= help: Replace with `vars[idx] = a_dict.get(key, "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789")` | ||
|
||
ℹ Suggested fix | ||
33 33 | var = "default" | ||
34 34 | | ||
35 35 | # SIM401 (complex expression in var) | ||
36 |-if key in a_dict: | ||
37 |- vars[idx] = a_dict[key] | ||
38 |-else: | ||
39 |- vars[idx] = "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789" | ||
36 |+vars[idx] = a_dict.get(key, "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789") | ||
40 37 | | ||
41 38 | # SIM401 | ||
42 39 | if foo(): | ||
|
||
SIM401.py:45:5: SIM401 [*] Use `vars[idx] = a_dict.get(key, "default")` instead of an `if` block | ||
| | ||
43 | pass | ||
44 | else: | ||
45 | if key in a_dict: | ||
| _____^ | ||
46 | | vars[idx] = a_dict[key] | ||
47 | | else: | ||
48 | | vars[idx] = "default" | ||
| |_____________________________^ SIM401 | ||
49 | | ||
50 | ### | ||
| | ||
= help: Replace with `vars[idx] = a_dict.get(key, "default")` | ||
|
||
ℹ Suggested fix | ||
42 42 | if foo(): | ||
43 43 | pass | ||
44 44 | else: | ||
45 |- if key in a_dict: | ||
46 |- vars[idx] = a_dict[key] | ||
47 |- else: | ||
48 |- vars[idx] = "default" | ||
45 |+ vars[idx] = a_dict.get(key, "default") | ||
49 46 | | ||
50 47 | ### | ||
51 48 | # Negative cases | ||
|
||
SIM401.py:123:7: SIM401 [*] Use `a_dict.get(key, "default3")` instead of an `if` block | ||
| | ||
122 | # SIM401 | ||
123 | var = a_dict[key] if key in a_dict else "default3" | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM401 | ||
124 | | ||
125 | # SIM401 | ||
| | ||
= help: Replace with `a_dict.get(key, "default3")` | ||
|
||
ℹ Suggested fix | ||
120 120 | ### | ||
121 121 | | ||
122 122 | # SIM401 | ||
123 |-var = a_dict[key] if key in a_dict else "default3" | ||
123 |+var = a_dict.get(key, "default3") | ||
124 124 | | ||
125 125 | # SIM401 | ||
126 126 | var = "default-1" if key not in a_dict else a_dict[key] | ||
|
||
SIM401.py:126:7: SIM401 [*] Use `a_dict.get(key, "default-1")` instead of an `if` block | ||
| | ||
125 | # SIM401 | ||
126 | var = "default-1" if key not in a_dict else a_dict[key] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM401 | ||
127 | | ||
128 | # OK (default contains effect) | ||
| | ||
= help: Replace with `a_dict.get(key, "default-1")` | ||
|
||
ℹ Suggested fix | ||
123 123 | var = a_dict[key] if key in a_dict else "default3" | ||
124 124 | | ||
125 125 | # SIM401 | ||
126 |-var = "default-1" if key not in a_dict else a_dict[key] | ||
126 |+var = a_dict.get(key, "default-1") | ||
127 127 | | ||
128 128 | # OK (default contains effect) | ||
129 129 | var = a_dict[key] if key in a_dict else val1 + val2 | ||
|
||
|