diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_list_index_lookup.py b/crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_list_index_lookup.py index 2e15d9bdb88f0b..3e00c7fd861805 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_list_index_lookup.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_list_index_lookup.py @@ -1,3 +1,5 @@ +import builtins + letters = ["a", "b", "c"] @@ -10,6 +12,11 @@ def fix_these(): 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(): diff --git a/crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_index_lookup.rs b/crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_index_lookup.rs index e585706d9b22b1..6774535db9d320 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_index_lookup.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_index_lookup.rs @@ -215,15 +215,15 @@ fn enumerate_items( }; // Check that the function is the `enumerate` builtin. - let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() else { + let Some(call_path) = checker.semantic().resolve_call_path(func.as_ref()) else { return None; }; - if id != "enumerate" { + if matches!( + call_path.as_slice(), + ["enumerate", "builtins" | "enumerate"] + ) { return None; - }; - if !checker.semantic().is_builtin("enumerate") { - return None; - }; + } let Expr::Tuple(ast::ExprTuple { elts, .. }) = tuple_expr else { return None; diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1736_unnecessary_list_index_lookup.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1736_unnecessary_list_index_lookup.py.snap index 772c4113b3afd0..aab105471a4c71 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1736_unnecessary_list_index_lookup.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1736_unnecessary_list_index_lookup.py.snap @@ -1,124 +1,185 @@ --- source: crates/ruff_linter/src/rules/pylint/mod.rs --- -unnecessary_list_index_lookup.py:5:6: PLR1736 [*] Unnecessary lookup of list item by index +unnecessary_list_index_lookup.py:7:6: PLR1736 [*] Unnecessary lookup of list item by index | -4 | def fix_these(): -5 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 +6 | def fix_these(): +7 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 | ^^^^^^^^^^^^^^ PLR1736 -6 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 -7 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 +8 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 +9 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 | = help: Use existing item variable instead ℹ Safe fix -2 2 | -3 3 | -4 4 | def fix_these(): -5 |- [letters[index] for index, letter in enumerate(letters)] # PLR1736 - 5 |+ [letter for index, letter in enumerate(letters)] # PLR1736 -6 6 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 -7 7 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 -8 8 | - -unnecessary_list_index_lookup.py:6:6: PLR1736 [*] Unnecessary lookup of list item by index +4 4 | +5 5 | +6 6 | def fix_these(): +7 |- [letters[index] for index, letter in enumerate(letters)] # PLR1736 + 7 |+ [letter for index, letter in enumerate(letters)] # PLR1736 +8 8 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 +9 9 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 +10 10 | + +unnecessary_list_index_lookup.py:8:6: PLR1736 [*] Unnecessary lookup of list item by index | -4 | def fix_these(): -5 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 -6 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 +6 | def fix_these(): +7 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 +8 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 | ^^^^^^^^^^^^^^ PLR1736 -7 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 +9 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 | = help: Use existing item variable instead ℹ Safe fix -3 3 | -4 4 | def fix_these(): -5 5 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 -6 |- {letters[index] for index, letter in enumerate(letters)} # PLR1736 - 6 |+ {letter for index, letter in enumerate(letters)} # PLR1736 -7 7 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 -8 8 | -9 9 | for index, letter in enumerate(letters): - -unnecessary_list_index_lookup.py:7:14: PLR1736 [*] Unnecessary lookup of list item by index - | -5 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 -6 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 -7 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 - | ^^^^^^^^^^^^^^ PLR1736 -8 | -9 | for index, letter in enumerate(letters): - | - = help: Use existing item variable instead +5 5 | +6 6 | def fix_these(): +7 7 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 +8 |- {letters[index] for index, letter in enumerate(letters)} # PLR1736 + 8 |+ {letter for index, letter in enumerate(letters)} # PLR1736 +9 9 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 +10 10 | +11 11 | for index, letter in enumerate(letters): + +unnecessary_list_index_lookup.py:9:14: PLR1736 [*] Unnecessary lookup of list item by index + | + 7 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 + 8 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 + 9 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 + | ^^^^^^^^^^^^^^ PLR1736 +10 | +11 | for index, letter in enumerate(letters): + | + = help: Use existing item variable instead + +ℹ Safe fix +6 6 | def fix_these(): +7 7 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 +8 8 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 +9 |- {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 + 9 |+ {letter: letter for index, letter in enumerate(letters)} # PLR1736 +10 10 | +11 11 | for index, letter in enumerate(letters): +12 12 | print(letters[index]) # PLR1736 + +unnecessary_list_index_lookup.py:12:15: PLR1736 [*] Unnecessary lookup of list item by index + | +11 | for index, letter in enumerate(letters): +12 | print(letters[index]) # PLR1736 + | ^^^^^^^^^^^^^^ PLR1736 +13 | blah = letters[index] # PLR1736 +14 | assert letters[index] == "d" # PLR1736 + | + = help: Use existing item variable instead + +ℹ Safe fix +9 9 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 +10 10 | +11 11 | for index, letter in enumerate(letters): +12 |- print(letters[index]) # PLR1736 + 12 |+ print(letter) # PLR1736 +13 13 | blah = letters[index] # PLR1736 +14 14 | assert letters[index] == "d" # PLR1736 +15 15 | + +unnecessary_list_index_lookup.py:13:16: PLR1736 [*] Unnecessary lookup of list item by index + | +11 | for index, letter in enumerate(letters): +12 | print(letters[index]) # PLR1736 +13 | blah = letters[index] # PLR1736 + | ^^^^^^^^^^^^^^ PLR1736 +14 | assert letters[index] == "d" # PLR1736 + | + = help: Use existing item variable instead + +ℹ Safe fix +10 10 | +11 11 | for index, letter in enumerate(letters): +12 12 | print(letters[index]) # PLR1736 +13 |- blah = letters[index] # PLR1736 + 13 |+ blah = letter # PLR1736 +14 14 | assert letters[index] == "d" # PLR1736 +15 15 | +16 16 | for index, letter in builtins.enumerate(letters): + +unnecessary_list_index_lookup.py:14:16: PLR1736 [*] Unnecessary lookup of list item by index + | +12 | print(letters[index]) # PLR1736 +13 | blah = letters[index] # PLR1736 +14 | assert letters[index] == "d" # PLR1736 + | ^^^^^^^^^^^^^^ PLR1736 +15 | +16 | for index, letter in builtins.enumerate(letters): + | + = help: Use existing item variable instead ℹ Safe fix -4 4 | def fix_these(): -5 5 | [letters[index] for index, letter in enumerate(letters)] # PLR1736 -6 6 | {letters[index] for index, letter in enumerate(letters)} # PLR1736 -7 |- {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 - 7 |+ {letter: letter for index, letter in enumerate(letters)} # PLR1736 -8 8 | -9 9 | for index, letter in enumerate(letters): -10 10 | print(letters[index]) # PLR1736 - -unnecessary_list_index_lookup.py:10:15: PLR1736 [*] Unnecessary lookup of list item by index +11 11 | for index, letter in enumerate(letters): +12 12 | print(letters[index]) # PLR1736 +13 13 | blah = letters[index] # PLR1736 +14 |- assert letters[index] == "d" # PLR1736 + 14 |+ assert letter == "d" # PLR1736 +15 15 | +16 16 | for index, letter in builtins.enumerate(letters): +17 17 | print(letters[index]) # PLR1736 + +unnecessary_list_index_lookup.py:17:15: PLR1736 [*] Unnecessary lookup of list item by index | - 9 | for index, letter in enumerate(letters): -10 | print(letters[index]) # PLR1736 +16 | for index, letter in builtins.enumerate(letters): +17 | print(letters[index]) # PLR1736 | ^^^^^^^^^^^^^^ PLR1736 -11 | blah = letters[index] # PLR1736 -12 | assert letters[index] == "d" # PLR1736 +18 | blah = letters[index] # PLR1736 +19 | assert letters[index] == "d" # PLR1736 | = help: Use existing item variable instead ℹ Safe fix -7 7 | {letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 -8 8 | -9 9 | for index, letter in enumerate(letters): -10 |- print(letters[index]) # PLR1736 - 10 |+ print(letter) # PLR1736 -11 11 | blah = letters[index] # PLR1736 -12 12 | assert letters[index] == "d" # PLR1736 -13 13 | - -unnecessary_list_index_lookup.py:11:16: PLR1736 [*] Unnecessary lookup of list item by index +14 14 | assert letters[index] == "d" # PLR1736 +15 15 | +16 16 | for index, letter in builtins.enumerate(letters): +17 |- print(letters[index]) # PLR1736 + 17 |+ print(letter) # PLR1736 +18 18 | blah = letters[index] # PLR1736 +19 19 | assert letters[index] == "d" # PLR1736 +20 20 | + +unnecessary_list_index_lookup.py:18:16: PLR1736 [*] Unnecessary lookup of list item by index | - 9 | for index, letter in enumerate(letters): -10 | print(letters[index]) # PLR1736 -11 | blah = letters[index] # PLR1736 +16 | for index, letter in builtins.enumerate(letters): +17 | print(letters[index]) # PLR1736 +18 | blah = letters[index] # PLR1736 | ^^^^^^^^^^^^^^ PLR1736 -12 | assert letters[index] == "d" # PLR1736 +19 | assert letters[index] == "d" # PLR1736 | = help: Use existing item variable instead ℹ Safe fix -8 8 | -9 9 | for index, letter in enumerate(letters): -10 10 | print(letters[index]) # PLR1736 -11 |- blah = letters[index] # PLR1736 - 11 |+ blah = letter # PLR1736 -12 12 | assert letters[index] == "d" # PLR1736 -13 13 | -14 14 | - -unnecessary_list_index_lookup.py:12:16: PLR1736 [*] Unnecessary lookup of list item by index +15 15 | +16 16 | for index, letter in builtins.enumerate(letters): +17 17 | print(letters[index]) # PLR1736 +18 |- blah = letters[index] # PLR1736 + 18 |+ blah = letter # PLR1736 +19 19 | assert letters[index] == "d" # PLR1736 +20 20 | +21 21 | + +unnecessary_list_index_lookup.py:19:16: PLR1736 [*] Unnecessary lookup of list item by index | -10 | print(letters[index]) # PLR1736 -11 | blah = letters[index] # PLR1736 -12 | assert letters[index] == "d" # PLR1736 +17 | print(letters[index]) # PLR1736 +18 | blah = letters[index] # PLR1736 +19 | assert letters[index] == "d" # PLR1736 | ^^^^^^^^^^^^^^ PLR1736 | = help: Use existing item variable instead ℹ Safe fix -9 9 | for index, letter in enumerate(letters): -10 10 | print(letters[index]) # PLR1736 -11 11 | blah = letters[index] # PLR1736 -12 |- assert letters[index] == "d" # PLR1736 - 12 |+ assert letter == "d" # PLR1736 -13 13 | -14 14 | -15 15 | def dont_fix_these(): +16 16 | for index, letter in builtins.enumerate(letters): +17 17 | print(letters[index]) # PLR1736 +18 18 | blah = letters[index] # PLR1736 +19 |- assert letters[index] == "d" # PLR1736 + 19 |+ assert letter == "d" # PLR1736 +20 20 | +21 21 | +22 22 | def dont_fix_these():