Skip to content
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 20 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ignore rule when underscore is used as a variable
  • Loading branch information
diceroll123 committed Nov 24, 2023
commit f24c92d8a04c36687093bad90cf4fa1dfab69331
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
letters = ["a", "b", "c"]

for index, letter in enumerate(letters):
print(letters[index]) # PLR1736
blah = letters[index] # PLR1736
letters[index] = letters[index] # PLR1736 (on the right hand)
letters[index] = "d" # Ok

def fix_these():
for index, letter in enumerate(letters):
print(letters[index]) # PLR1736
blah = letters[index] # PLR1736
letters[index] = letters[index] # PLR1736 (on the right hand)


def dont_fix_these():
for index, letter in enumerate(letters):
letters[index] = "d" # Ok


def value_intentionally_unused():
for index, _ in enumerate(letters):
print(letters[index]) # Ok
blah = letters[index] # Ok
letters[index] = letters[index] # Ok
letters[index] = "d" # Ok
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ pub(crate) fn unnecessary_list_index_lookup(checker: &mut Checker, stmt_for: &St
return;
};

// If either of the variable names are intentionally ignored by naming them `_`, then don't emit
if index_name == "_" || value_name == "_" {
return;
}

// Get the first argument of the enumerate call
let Some(Expr::Name(ast::ExprName { id: sequence, .. })) = arguments.args.first() else {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
unnecessary_list_index_lookup.py:4:11: PLR1736 [*] Unnecessary list index lookup
unnecessary_list_index_lookup.py:6:15: PLR1736 [*] Unnecessary list index lookup
|
3 | for index, letter in enumerate(letters):
4 | print(letters[index]) # PLR1736
| ^^^^^^^^^^^^^^ PLR1736
5 | blah = letters[index] # PLR1736
6 | letters[index] = letters[index] # PLR1736 (on the right hand)
4 | def fix_these():
5 | for index, letter in enumerate(letters):
6 | print(letters[index]) # PLR1736
| ^^^^^^^^^^^^^^ PLR1736
7 | blah = letters[index] # PLR1736
8 | letters[index] = letters[index] # PLR1736 (on the right hand)
|
= help: Remove unnecessary list index lookup

ℹ Fix
1 1 | letters = ["a", "b", "c"]
2 2 |
3 3 | for index, letter in enumerate(letters):
4 |- print(letters[index]) # PLR1736
4 |+ print(letter) # PLR1736
5 5 | blah = letters[index] # PLR1736
6 6 | letters[index] = letters[index] # PLR1736 (on the right hand)
7 7 | letters[index] = "d" # Ok
3 3 |
4 4 | def fix_these():
5 5 | for index, letter in enumerate(letters):
6 |- print(letters[index]) # PLR1736
6 |+ print(letter) # PLR1736
7 7 | blah = letters[index] # PLR1736
8 8 | letters[index] = letters[index] # PLR1736 (on the right hand)
9 9 |

unnecessary_list_index_lookup.py:5:12: PLR1736 [*] Unnecessary list index lookup
unnecessary_list_index_lookup.py:7:16: PLR1736 [*] Unnecessary list index lookup
|
3 | for index, letter in enumerate(letters):
4 | print(letters[index]) # PLR1736
5 | blah = letters[index] # PLR1736
| ^^^^^^^^^^^^^^ PLR1736
6 | letters[index] = letters[index] # PLR1736 (on the right hand)
7 | letters[index] = "d" # Ok
5 | for index, letter in enumerate(letters):
6 | print(letters[index]) # PLR1736
7 | blah = letters[index] # PLR1736
| ^^^^^^^^^^^^^^ PLR1736
8 | letters[index] = letters[index] # PLR1736 (on the right hand)
|
= help: Remove unnecessary list index lookup

ℹ Fix
2 2 |
3 3 | for index, letter in enumerate(letters):
4 4 | print(letters[index]) # PLR1736
5 |- blah = letters[index] # PLR1736
5 |+ blah = letter # PLR1736
6 6 | letters[index] = letters[index] # PLR1736 (on the right hand)
7 7 | letters[index] = "d" # Ok
4 4 | def fix_these():
5 5 | for index, letter in enumerate(letters):
6 6 | print(letters[index]) # PLR1736
7 |- blah = letters[index] # PLR1736
7 |+ blah = letter # PLR1736
8 8 | letters[index] = letters[index] # PLR1736 (on the right hand)
9 9 |
10 10 |

unnecessary_list_index_lookup.py:6:22: PLR1736 [*] Unnecessary list index lookup
unnecessary_list_index_lookup.py:8:26: PLR1736 [*] Unnecessary list index lookup
|
4 | print(letters[index]) # PLR1736
5 | blah = letters[index] # PLR1736
6 | letters[index] = letters[index] # PLR1736 (on the right hand)
| ^^^^^^^^^^^^^^ PLR1736
7 | letters[index] = "d" # Ok
6 | print(letters[index]) # PLR1736
7 | blah = letters[index] # PLR1736
8 | letters[index] = letters[index] # PLR1736 (on the right hand)
| ^^^^^^^^^^^^^^ PLR1736
|
= help: Remove unnecessary list index lookup

ℹ Fix
3 3 | for index, letter in enumerate(letters):
4 4 | print(letters[index]) # PLR1736
5 5 | blah = letters[index] # PLR1736
6 |- letters[index] = letters[index] # PLR1736 (on the right hand)
6 |+ letters[index] = letter # PLR1736 (on the right hand)
7 7 | letters[index] = "d" # Ok
5 5 | for index, letter in enumerate(letters):
6 6 | print(letters[index]) # PLR1736
7 7 | blah = letters[index] # PLR1736
8 |- letters[index] = letters[index] # PLR1736 (on the right hand)
8 |+ letters[index] = letter # PLR1736 (on the right hand)
9 9 |
10 10 |
11 11 | def dont_fix_these():