-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[perflint
] implement quick-fix for manual-list-comprehension
(PERF401
)
#13919
Merged
Merged
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
98c14e3
implement quick fix for manual list comprehensions
w0nder1ng d89950f
convert into list comprehension when the variable is an empty list li…
w0nder1ng f3867ee
update tests and only offer fix when binding is a single assignment
w0nder1ng 13592b4
update test output
w0nder1ng 27f8ba8
fix false negative when binding statement did not exist
w0nder1ng 0aece9c
use slice instead of .name()
w0nder1ng 1f7f4c0
modify fix to preserve comments
w0nder1ng 499bdca
modify fix to use direct string modification instead of generation
w0nder1ng 158b2e2
use LineRanges trait
w0nder1ng 4d441c3
update snapshot
w0nder1ng 5dbc764
remove leftover indentation from for loop
w0nder1ng a3b42e6
uncomment preview gate
w0nder1ng aa36f1b
add preview test case
w0nder1ng 0078d62
change fix to FixAvailability::Sometimes
w0nder1ng 9aa5627
uncomment LineRanges import
w0nder1ng 64f0f56
fix nits and improve diagnostic message
w0nder1ng d89c634
Merge branch 'main' into perf401
w0nder1ng bfa0971
add missing comma
w0nder1ng 1954267
change format! to .to_string()
w0nder1ng 37e1e81
Use improved message in stable and preview
MichaReiser fdfcc81
don't try to insert a comment when comments are empty
w0nder1ng e91b2d6
Merge branch 'perf401' of https://github.com/w0nder1ng/ruff into perf401
w0nder1ng 2d09381
update test output
w0nder1ng 9564443
improve empty comment handling
w0nder1ng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
95 changes: 91 additions & 4 deletions
95
...src/rules/perflint/snapshots/ruff_linter__rules__perflint__tests__PERF401_PERF401.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 |
---|---|---|
@@ -1,34 +1,121 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/perflint/mod.rs | ||
--- | ||
PERF401.py:6:13: PERF401 Use a list comprehension to create a transformed list | ||
PERF401.py:6:13: PERF401 [*] Use a list comprehension to create a transformed list | ||
| | ||
4 | for i in items: | ||
5 | if i % 2: | ||
6 | result.append(i) # PERF401 | ||
| ^^^^^^^^^^^^^^^^ PERF401 | ||
| | ||
= help: Replace for loop with list comprehension | ||
|
||
PERF401.py:13:9: PERF401 Use a list comprehension to create a transformed list | ||
ℹ Unsafe fix | ||
1 1 | def f(): | ||
2 2 | items = [1, 2, 3, 4] | ||
3 |- result = [] | ||
4 |- for i in items: | ||
5 |- if i % 2: | ||
6 |- result.append(i) # PERF401 | ||
3 |+ result = [i for i in items if i % 2] | ||
4 |+ # PERF401 | ||
w0nder1ng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
7 5 | | ||
8 6 | | ||
9 7 | def f(): | ||
|
||
PERF401.py:13:9: PERF401 [*] Use a list comprehension to create a transformed list | ||
| | ||
11 | result = [] | ||
12 | for i in items: | ||
13 | result.append(i * i) # PERF401 | ||
| ^^^^^^^^^^^^^^^^^^^^ PERF401 | ||
| | ||
= help: Replace for loop with list comprehension | ||
|
||
ℹ Unsafe fix | ||
8 8 | | ||
9 9 | def f(): | ||
10 10 | items = [1, 2, 3, 4] | ||
11 |- result = [] | ||
12 |- for i in items: | ||
13 |- result.append(i * i) # PERF401 | ||
11 |+ result = [i * i for i in items] | ||
12 |+ # PERF401 | ||
14 13 | | ||
15 14 | | ||
16 15 | def f(): | ||
|
||
PERF401.py:82:13: PERF401 Use an async list comprehension to create a transformed list | ||
PERF401.py:82:13: PERF401 [*] Use an async list comprehension to create a transformed list | ||
| | ||
80 | async for i in items: | ||
81 | if i % 2: | ||
82 | result.append(i) # PERF401 | ||
| ^^^^^^^^^^^^^^^^ PERF401 | ||
| | ||
= help: Replace for loop with list comprehension | ||
|
||
ℹ Unsafe fix | ||
76 76 | | ||
77 77 | def f(): | ||
78 78 | items = [1, 2, 3, 4] | ||
79 |- result = [] | ||
80 |- async for i in items: | ||
81 |- if i % 2: | ||
82 |- result.append(i) # PERF401 | ||
79 |+ result = [i async for i in items if i % 2] | ||
80 |+ # PERF401 | ||
83 81 | | ||
84 82 | | ||
85 83 | def f(): | ||
|
||
PERF401.py:89:9: PERF401 Use an async list comprehension to create a transformed list | ||
PERF401.py:89:9: PERF401 [*] Use an async list comprehension to create a transformed list | ||
| | ||
87 | result = [] | ||
88 | async for i in items: | ||
89 | result.append(i) # PERF401 | ||
| ^^^^^^^^^^^^^^^^ PERF401 | ||
| | ||
= help: Replace for loop with list comprehension | ||
|
||
ℹ Unsafe fix | ||
84 84 | | ||
85 85 | def f(): | ||
86 86 | items = [1, 2, 3, 4] | ||
87 |- result = [] | ||
88 |- async for i in items: | ||
89 |- result.append(i) # PERF401 | ||
87 |+ result = [i async for i in items] | ||
88 |+ # PERF401 | ||
90 89 | | ||
91 90 | | ||
92 91 | def f(): | ||
|
||
PERF401.py:95:9: PERF401 [*] Use a list comprehension to create a transformed list | ||
| | ||
93 | result, _ = [1,2,3,4], ... | ||
94 | for i in range(10): | ||
95 | result.append(i*2) # PERF401 | ||
| ^^^^^^^^^^^^^^^^^^ PERF401 | ||
96 | | ||
97 | def f(): | ||
| | ||
= help: Replace for loop with list.extend | ||
|
||
ℹ Unsafe fix | ||
91 91 | | ||
92 92 | def f(): | ||
93 93 | result, _ = [1,2,3,4], ... | ||
94 |- for i in range(10): | ||
95 |- result.append(i*2) # PERF401 | ||
94 |+ result.extend(i * 2 for i in range(10)) # PERF401 | ||
96 95 | | ||
97 96 | def f(): | ||
98 97 | result = [] | ||
|
||
PERF401.py:102:17: PERF401 Use a list comprehension to create a transformed list | ||
| | ||
100 | for i in range(10): | ||
101 | if i % 2: | ||
102 | result.append(i) # PERF401 | ||
| ^^^^^^^^^^^^^^^^ PERF401 | ||
| |
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.
Let's adjust the message to make use of the new
comprehension_type
as well to avoid inconsistency between the message and the fix title.