Skip to content

Commit

Permalink
use checker.semantic().resolve_call_path
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Nov 28, 2023
1 parent fc43d1b commit bcae782
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import builtins

letters = ["a", "b", "c"]


Expand All @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,16 @@ fn enumerate_items(
};

// Check that the function is the `enumerate` builtin.
let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() else {
return None;
};
if id != "enumerate" {
return None;
};
if !checker.semantic().is_builtin("enumerate") {
let Some(call_path) = checker.semantic().resolve_call_path(func.as_ref()) else {
return None;
};

match call_path.as_slice() {
["", "enumerate"] => (),
["builtins", "enumerate"] => (),
_ => return None,
}

let Expr::Tuple(ast::ExprTuple { elts, .. }) = tuple_expr else {
return None;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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():


0 comments on commit bcae782

Please sign in to comment.