Respect start index in unnecessary-list-index-lookup (#12603)

## Summary

Closes https://github.com/astral-sh/ruff/issues/12594.
This commit is contained in:
Charlie Marsh 2024-07-31 21:21:15 -04:00 committed by GitHub
parent 3f49ab126f
commit 8e383b9587
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 65 additions and 1 deletions

View file

@ -62,3 +62,17 @@ def value_intentionally_unused():
print(letters[index]) # OK
blah = letters[index] # OK
letters[index] = "d" # OK
def start():
# OK
for index, list_item in enumerate(some_list, start=1):
print(some_list[index])
# PLR1736
for index, list_item in enumerate(some_list, start=0):
print(some_list[index])
# PLR1736
for index, list_item in enumerate(some_list):
print(some_list[index])

View file

@ -1,7 +1,7 @@
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::{self as ast, Expr, StmtFor};
use ruff_python_ast::{self as ast, Expr, Int, Number, StmtFor};
use ruff_python_semantic::SemanticModel;
use ruff_text_size::Ranged;
@ -151,6 +151,19 @@ fn enumerate_items<'a>(
return None;
};
// If the `enumerate` call has a non-zero `start`, don't omit.
if !arguments.find_argument("start", 1).map_or(true, |expr| {
matches!(
expr,
Expr::NumberLiteral(ast::ExprNumberLiteral {
value: Number::Int(Int::ZERO),
..
})
)
}) {
return None;
}
// Check that the function is the `enumerate` builtin.
if !semantic.match_builtin_expr(func, "enumerate") {
return None;

View file

@ -181,3 +181,40 @@ unnecessary_list_index_lookup.py:19:16: PLR1736 [*] List index lookup in `enumer
20 20 |
21 21 |
22 22 | def dont_fix_these():
unnecessary_list_index_lookup.py:74:15: PLR1736 [*] List index lookup in `enumerate()` loop
|
72 | # PLR1736
73 | for index, list_item in enumerate(some_list, start=0):
74 | print(some_list[index])
| ^^^^^^^^^^^^^^^^ PLR1736
75 |
76 | # PLR1736
|
= help: Use the loop variable directly
Safe fix
71 71 |
72 72 | # PLR1736
73 73 | for index, list_item in enumerate(some_list, start=0):
74 |- print(some_list[index])
74 |+ print(list_item)
75 75 |
76 76 | # PLR1736
77 77 | for index, list_item in enumerate(some_list):
unnecessary_list_index_lookup.py:78:15: PLR1736 [*] List index lookup in `enumerate()` loop
|
76 | # PLR1736
77 | for index, list_item in enumerate(some_list):
78 | print(some_list[index])
| ^^^^^^^^^^^^^^^^ PLR1736
|
= help: Use the loop variable directly
Safe fix
75 75 |
76 76 | # PLR1736
77 77 | for index, list_item in enumerate(some_list):
78 |- print(some_list[index])
78 |+ print(list_item)