forked from pylint-dev/pylint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New checker
unnecessary-list-index-lookup
(pylint-dev#4525)
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 deletions.
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
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
44 changes: 44 additions & 0 deletions
44
tests/functional/u/unnecessary/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,44 @@ | ||
# pylint: disable=missing-docstring, too-few-public-methods, expression-not-assigned, line-too-long, unused-variable | ||
|
||
my_list = ['a', 'b'] | ||
|
||
for idx, val in enumerate(my_list): | ||
print(my_list[idx]) # [unnecessary-list-index-lookup] | ||
|
||
for idx, _ in enumerate(my_list): | ||
print(my_list[0]) | ||
if idx > 0: | ||
print(my_list[idx - 1]) | ||
|
||
for idx, val in enumerate(my_list): | ||
del my_list[idx] | ||
|
||
for idx, val in enumerate(my_list): | ||
my_list[idx] = 42 | ||
|
||
def process_list(data): | ||
for index, value in enumerate(data): | ||
index = 1 | ||
print(data[index]) | ||
|
||
def process_list_again(data): | ||
for index, value in enumerate(data): | ||
value = 1 | ||
print(data[index]) # Can't use value here, it's been redefined | ||
|
||
for idx, val in enumerate(my_list): | ||
if (val := 42) and my_list[idx] == 'b': | ||
print(1) | ||
|
||
other_list = [1, 2] | ||
for idx, val in enumerate(my_list): | ||
print(other_list[idx]) | ||
|
||
OTHER_INDEX = 0 | ||
for idx, val in enumerate(my_list): | ||
print(my_list[OTHER_INDEX]) | ||
|
||
result = [val for idx, val in enumerate(my_list) if my_list[idx] == 'a'] # [unnecessary-list-index-lookup] | ||
result = [val for idx, val in enumerate(my_list) if idx > 0 and my_list[idx - 1] == 'a'] | ||
result = [val for idx, val in enumerate(my_list) if other_list[idx] == 'a'] | ||
result = [my_list[idx] for idx, val in enumerate(my_list)] # [unnecessary-list-index-lookup] |
3 changes: 3 additions & 0 deletions
3
tests/functional/u/unnecessary/unnecessary_list_index_lookup.txt
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,3 @@ | ||
unnecessary-list-index-lookup:6:10:6:22::Unnecessary list index lookup, use 'val' instead:UNDEFINED | ||
unnecessary-list-index-lookup:41:52:41:64::Unnecessary list index lookup, use 'val' instead:UNDEFINED | ||
unnecessary-list-index-lookup:44:10:44:22::Unnecessary list index lookup, use 'val' instead:UNDEFINED |