-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[pylint] - add unnecessary-list-index-lookup
(PLR1736
) + autofix
#7999
Merged
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
76e178a
add PLR1736 and autofix
diceroll123 a0c3a57
fix assignment case
diceroll123 f24c92d
ignore rule when underscore is used as a variable
diceroll123 87ac940
add the other assignments
diceroll123 26e9968
add generator/compehrension expressions
diceroll123 6a041fa
tweak logic for assignments
diceroll123 85f4c44
fix lint code order
diceroll123 801aca5
update snapshots
diceroll123 1aa0024
Update crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_ind…
diceroll123 0dc64eb
Update crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_ind…
diceroll123 6f7f90d
Update crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_ind…
diceroll123 33bdba8
Update crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_ind…
diceroll123 502f543
Update ruff_linter__rules__pylint__tests__PLR1736_unnecessary_list_in…
diceroll123 d0e2f19
tweak modification detection
diceroll123 fc43d1b
update fixture
diceroll123 bcae782
use `checker.semantic().resolve_call_path`
diceroll123 267a10b
add case for deletions
diceroll123 a6502b4
fix comments
diceroll123 59fec72
suggested tweaks
diceroll123 a0a3688
Merge branch 'main' into add-R1736
diceroll123 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
59 changes: 59 additions & 0 deletions
59
crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_list_index_lookup.py
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,59 @@ | ||
import builtins | ||
|
||
letters = ["a", "b", "c"] | ||
|
||
|
||
def fix_these(): | ||
[letters[index] for index, letter in enumerate(letters)] # PLR1736 | ||
{letters[index] for index, letter in enumerate(letters)} # PLR1736 | ||
{letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 | ||
|
||
for index, letter in enumerate(letters): | ||
print(letters[index]) # PLR1736 | ||
blah = letters[index] # PLR1736 | ||
assert letters[index] == "d" # PLR1736 | ||
|
||
for index, letter in builtins.enumerate(letters): | ||
print(letters[index]) # PLR1736 | ||
blah = letters[index] # PLR1736 | ||
assert letters[index] == "d" # PLR1736 | ||
|
||
|
||
def dont_fix_these(): | ||
# once there is an assignment to the sequence[index], we stop emitting diagnostics | ||
for index, letter in enumerate(letters): | ||
letters[index] = "d" # Ok | ||
letters[index] += "e" # Ok | ||
assert letters[index] == "de" # Ok | ||
|
||
# once there is an assignment to the index, we stop emitting diagnostics | ||
for index, letter in enumerate(letters): | ||
index += 1 # Ok | ||
print(letters[index]) # Ok | ||
|
||
# once there is an assignment to the sequence, we stop emitting diagnostics | ||
for index, letter in enumerate(letters): | ||
letters = ["d", "e", "f"] # Ok | ||
print(letters[index]) # Ok | ||
|
||
# once there is an deletion from or of the sequence or index, we stop emitting diagnostics | ||
for index, letter in enumerate(letters): | ||
del letters[index] # Ok | ||
print(letters[index]) # Ok | ||
for index, letter in enumerate(letters): | ||
del letters # Ok | ||
print(letters[index]) # Ok | ||
for index, letter in enumerate(letters): | ||
del index # Ok | ||
print(letters[index]) # Ok | ||
|
||
|
||
def value_intentionally_unused(): | ||
[letters[index] for index, _ in enumerate(letters)] # Ok | ||
{letters[index] for index, _ in enumerate(letters)} # Ok | ||
{index: letters[index] for index, _ in enumerate(letters)} # Ok | ||
|
||
for index, _ in enumerate(letters): | ||
print(letters[index]) # Ok | ||
blah = letters[index] # Ok | ||
letters[index] = "d" # Ok |
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
Oops, something went wrong.
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.
These tests are really thorough! Thanks for thinking through them.