-
Notifications
You must be signed in to change notification settings - Fork 881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flake8-simplify
] Detect implicit else
cases in needless-bool
(SIM103
)
#10414
Merged
charliermarsh
merged 20 commits into
astral-sh:main
from
ottaviohartman:thartman/sim103-detect-implicit-else
Mar 18, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f1c3626
this is certainly some code
ottaviohartman f663b85
broke a test
ottaviohartman cf62508
fixed test mostly
ottaviohartman 683be70
is that right?
ottaviohartman 29b4c84
working
ottaviohartman 9755bb2
more tests
ottaviohartman 5f7a51a
some cleanup
ottaviohartman c66caca
rm
ottaviohartman db10fe7
comment and small change
ottaviohartman f92dad8
working
ottaviohartman 86e6af3
move snap test
ottaviohartman 915371f
clean up diff
ottaviohartman 66f6ee4
add preview snap
ottaviohartman 93dbba3
get rid of clone()
ottaviohartman 13a73c1
forward declare vec option
ottaviohartman 482aa69
cleanup
ottaviohartman b85a8db
clippy
ottaviohartman 03170c1
small change to reduce blocks
ottaviohartman 1b7d160
Merge branch 'main' into thartman/sim103-detect-implicit-else
charliermarsh 7ecc53d
Defer fetch
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
189 changes: 189 additions & 0 deletions
189
...lify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM103_SIM103.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,189 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs | ||
--- | ||
SIM103.py:3:5: SIM103 [*] Return the condition `a` directly | ||
| | ||
1 | def f(): | ||
2 | # SIM103 | ||
3 | if a: | ||
| _____^ | ||
4 | | return True | ||
5 | | else: | ||
6 | | return False | ||
| |____________________^ SIM103 | ||
| | ||
= help: Replace with `return bool(a)` | ||
|
||
ℹ Unsafe fix | ||
1 1 | def f(): | ||
2 2 | # SIM103 | ||
3 |- if a: | ||
4 |- return True | ||
5 |- else: | ||
6 |- return False | ||
3 |+ return bool(a) | ||
7 4 | | ||
8 5 | | ||
9 6 | def f(): | ||
|
||
SIM103.py:11:5: SIM103 [*] Return the condition `a == b` directly | ||
| | ||
9 | def f(): | ||
10 | # SIM103 | ||
11 | if a == b: | ||
| _____^ | ||
12 | | return True | ||
13 | | else: | ||
14 | | return False | ||
| |____________________^ SIM103 | ||
| | ||
= help: Replace with `return a == b` | ||
|
||
ℹ Unsafe fix | ||
8 8 | | ||
9 9 | def f(): | ||
10 10 | # SIM103 | ||
11 |- if a == b: | ||
12 |- return True | ||
13 |- else: | ||
14 |- return False | ||
11 |+ return a == b | ||
15 12 | | ||
16 13 | | ||
17 14 | def f(): | ||
|
||
SIM103.py:21:5: SIM103 [*] Return the condition `b` directly | ||
| | ||
19 | if a: | ||
20 | return 1 | ||
21 | elif b: | ||
| _____^ | ||
22 | | return True | ||
23 | | else: | ||
24 | | return False | ||
| |____________________^ SIM103 | ||
| | ||
= help: Replace with `return bool(b)` | ||
|
||
ℹ Unsafe fix | ||
18 18 | # SIM103 | ||
19 19 | if a: | ||
20 20 | return 1 | ||
21 |- elif b: | ||
22 |- return True | ||
23 |- else: | ||
24 |- return False | ||
21 |+ return bool(b) | ||
25 22 | | ||
26 23 | | ||
27 24 | def f(): | ||
|
||
SIM103.py:32:9: SIM103 [*] Return the condition `b` directly | ||
| | ||
30 | return 1 | ||
31 | else: | ||
32 | if b: | ||
| _________^ | ||
33 | | return True | ||
34 | | else: | ||
35 | | return False | ||
| |________________________^ SIM103 | ||
| | ||
= help: Replace with `return bool(b)` | ||
|
||
ℹ Unsafe fix | ||
29 29 | if a: | ||
30 30 | return 1 | ||
31 31 | else: | ||
32 |- if b: | ||
33 |- return True | ||
34 |- else: | ||
35 |- return False | ||
32 |+ return bool(b) | ||
36 33 | | ||
37 34 | | ||
38 35 | def f(): | ||
|
||
SIM103.py:57:5: SIM103 [*] Return the condition `a` directly | ||
| | ||
55 | def f(): | ||
56 | # SIM103 (but not fixable) | ||
57 | if a: | ||
| _____^ | ||
58 | | return False | ||
59 | | else: | ||
60 | | return True | ||
| |___________________^ SIM103 | ||
| | ||
= help: Replace with `return not a` | ||
|
||
ℹ Unsafe fix | ||
54 54 | | ||
55 55 | def f(): | ||
56 56 | # SIM103 (but not fixable) | ||
57 |- if a: | ||
58 |- return False | ||
59 |- else: | ||
60 |- return True | ||
57 |+ return not a | ||
61 58 | | ||
62 59 | | ||
63 60 | def f(): | ||
|
||
SIM103.py:83:5: SIM103 Return the condition `a` directly | ||
| | ||
81 | def bool(): | ||
82 | return False | ||
83 | if a: | ||
| _____^ | ||
84 | | return True | ||
85 | | else: | ||
86 | | return False | ||
| |____________________^ SIM103 | ||
| | ||
= help: Inline condition | ||
|
||
SIM103.py:96:5: SIM103 [*] Return the condition `a` directly | ||
| | ||
94 | def f(): | ||
95 | # SIM103 | ||
96 | if a: | ||
| _____^ | ||
97 | | return True | ||
98 | | return False | ||
| |________________^ SIM103 | ||
| | ||
= help: Replace with `return bool(a)` | ||
|
||
ℹ Unsafe fix | ||
93 93 | | ||
94 94 | def f(): | ||
95 95 | # SIM103 | ||
96 |- if a: | ||
97 |- return True | ||
98 |- return False | ||
96 |+ return bool(a) | ||
99 97 | | ||
100 98 | | ||
101 99 | def f(): | ||
|
||
SIM103.py:103:5: SIM103 [*] Return the condition `a` directly | ||
| | ||
101 | def f(): | ||
102 | # SIM103 | ||
103 | if a: | ||
| _____^ | ||
104 | | return False | ||
105 | | return True | ||
| |_______________^ SIM103 | ||
| | ||
= help: Replace with `return not a` | ||
|
||
ℹ Unsafe fix | ||
100 100 | | ||
101 101 | def f(): | ||
102 102 | # SIM103 | ||
103 |- if a: | ||
104 |- return False | ||
105 |- return True | ||
103 |+ return not a |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ottaviohartman -- I was able to remove the vector allocation here by making every branch return
&[Stmt]
instead of&Vec<Stmt>
. Then, you can do a nice trick whereby if you have an&T
, you can create&[T]
viastd::slice::from_ref
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome - I'll use that in the future.