-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
76e178a
add PLR1736 and autofix
diceroll123 a0c3a57
fix assignment case
diceroll123 f24c92d
ignore rule when underscore is used as a variable
diceroll123 87ac940
add the other assignments
diceroll123 26e9968
add generator/compehrension expressions
diceroll123 6a041fa
tweak logic for assignments
diceroll123 85f4c44
fix lint code order
diceroll123 801aca5
update snapshots
diceroll123 1aa0024
Update crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_ind…
diceroll123 0dc64eb
Update crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_ind…
diceroll123 6f7f90d
Update crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_ind…
diceroll123 33bdba8
Update crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_ind…
diceroll123 502f543
Update ruff_linter__rules__pylint__tests__PLR1736_unnecessary_list_in…
diceroll123 d0e2f19
tweak modification detection
diceroll123 fc43d1b
update fixture
diceroll123 bcae782
use `checker.semantic().resolve_call_path`
diceroll123 267a10b
add case for deletions
diceroll123 a6502b4
fix comments
diceroll123 59fec72
suggested tweaks
diceroll123 a0a3688
Merge branch 'main' into add-R1736
diceroll123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
crates/ruff_linter/resources/test/fixtures/pylint/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,30 @@ | ||
letters = ["a", "b", "c"] | ||
|
||
|
||
def fix_these(): | ||
[letters[index] for index, letter in enumerate(letters)] # PLR1736 | ||
{letters[index] for index, letter in enumerate(letters)} # PLR1736 | ||
{letter: letters[index] for index, letter in enumerate(letters)} # PLR1736 | ||
|
||
for index, letter in enumerate(letters): | ||
print(letters[index]) # PLR1736 | ||
blah = letters[index] # PLR1736 | ||
assert letters[index] == "d" # PLR1736 | ||
|
||
|
||
def dont_fix_these(): | ||
# once there is an assignment to the sequence[index], we stop emitting diagnostics | ||
for index, letter in enumerate(letters): | ||
letters[index] = "d" # Ok | ||
assert letters[index] == "d" # Ok | ||
|
||
|
||
def value_intentionally_unused(): | ||
[letters[index] for index, _ in enumerate(letters)] # PLR1736 | ||
{letters[index] for index, _ in enumerate(letters)} # PLR1736 | ||
{index: letters[index] for index, _ in enumerate(letters)} # PLR1736 | ||
|
||
for index, _ in enumerate(letters): | ||
print(letters[index]) # Ok | ||
blah = letters[index] # Ok | ||
letters[index] = "d" # Ok |
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
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
254 changes: 254 additions & 0 deletions
254
crates/ruff_linter/src/rules/pylint/rules/unnecessary_list_index_lookup.rs
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,254 @@ | ||
use ruff_python_ast::{self as ast, Expr, Stmt, StmtFor}; | ||
|
||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::visitor; | ||
use ruff_python_ast::visitor::Visitor; | ||
use ruff_text_size::TextRange; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of enumeration and accessing the value by index lookup. | ||
diceroll123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// ## Why is this bad? | ||
/// The value is already accessible by the 2nd variable from the enumeration. | ||
diceroll123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// ## Example | ||
/// ```python | ||
/// letters = ["a", "b", "c"] | ||
/// | ||
/// for index, letter in enumerate(letters): | ||
/// print(letters[index]) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// letters = ["a", "b", "c"] | ||
/// | ||
/// for index, letter in enumerate(letters): | ||
/// print(letter) | ||
/// ``` | ||
#[violation] | ||
pub struct UnnecessaryListIndexLookup; | ||
|
||
impl AlwaysFixableViolation for UnnecessaryListIndexLookup { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Unnecessary list index lookup") | ||
diceroll123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
fn fix_title(&self) -> String { | ||
format!("Remove unnecessary list index lookup") | ||
diceroll123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
struct SubscriptVisitor<'a> { | ||
sequence_name: &'a str, | ||
index_name: &'a str, | ||
diagnostic_ranges: Vec<TextRange>, | ||
is_subcript_modified: bool, | ||
} | ||
|
||
impl<'a> SubscriptVisitor<'a> { | ||
fn new(sequence_name: &'a str, index_name: &'a str) -> Self { | ||
Self { | ||
sequence_name, | ||
index_name, | ||
diagnostic_ranges: Vec::new(), | ||
is_subcript_modified: false, | ||
} | ||
} | ||
} | ||
|
||
fn check_target_for_assignment(expr: &Expr, sequence_name: &str, index_name: &str) -> bool { | ||
// if we see the sequence subscript being modified, we'll stop emitting diagnostics | ||
match expr { | ||
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => { | ||
if let Expr::Name(ast::ExprName { id, .. }) = value.as_ref() { | ||
if id == sequence_name { | ||
if let Expr::Name(ast::ExprName { id, .. }) = slice.as_ref() { | ||
if id == index_name { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
false | ||
} | ||
_ => false, | ||
} | ||
} | ||
|
||
impl<'a> Visitor<'_> for SubscriptVisitor<'a> { | ||
fn visit_expr(&mut self, expr: &Expr) { | ||
if self.is_subcript_modified { | ||
return; | ||
} | ||
match expr { | ||
Expr::Subscript(ast::ExprSubscript { | ||
value, | ||
slice, | ||
range, | ||
.. | ||
}) => { | ||
if let Expr::Name(ast::ExprName { id, .. }) = value.as_ref() { | ||
if id == self.sequence_name { | ||
if let Expr::Name(ast::ExprName { id, .. }) = slice.as_ref() { | ||
if id == self.index_name { | ||
self.diagnostic_ranges.push(*range); | ||
} | ||
} | ||
} | ||
} | ||
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
_ => visitor::walk_expr(self, expr), | ||
} | ||
} | ||
|
||
fn visit_stmt(&mut self, stmt: &Stmt) { | ||
if self.is_subcript_modified { | ||
return; | ||
} | ||
match stmt { | ||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => { | ||
self.is_subcript_modified = targets.iter().any(|target| { | ||
check_target_for_assignment(target, self.sequence_name, self.index_name) | ||
}); | ||
self.visit_expr(value); | ||
} | ||
Stmt::AnnAssign(ast::StmtAnnAssign { target, value, .. }) => { | ||
if let Some(value) = value { | ||
self.is_subcript_modified = | ||
check_target_for_assignment(target, self.sequence_name, self.index_name); | ||
self.visit_expr(value); | ||
} | ||
} | ||
Stmt::AugAssign(ast::StmtAugAssign { target, value, .. }) => { | ||
self.is_subcript_modified = | ||
check_target_for_assignment(target, self.sequence_name, self.index_name); | ||
self.visit_expr(value); | ||
} | ||
_ => visitor::walk_stmt(self, stmt), | ||
} | ||
} | ||
} | ||
|
||
/// PLR1736 | ||
pub(crate) fn unnecessary_list_index_lookup(checker: &mut Checker, stmt_for: &StmtFor) { | ||
let Some((sequence, index_name, value_name)) = | ||
enumerate_items(checker, &stmt_for.iter, &stmt_for.target) | ||
else { | ||
return; | ||
}; | ||
|
||
let mut visitor = SubscriptVisitor::new(&sequence, &index_name); | ||
|
||
visitor.visit_body(&stmt_for.body); | ||
visitor.visit_body(&stmt_for.orelse); | ||
|
||
for range in visitor.diagnostic_ranges { | ||
let mut diagnostic = Diagnostic::new(UnnecessaryListIndexLookup, range); | ||
|
||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( | ||
value_name.clone(), | ||
range, | ||
))); | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} | ||
} | ||
|
||
/// PLR1736 | ||
pub(crate) fn unnecessary_list_index_lookup_comprehension(checker: &mut Checker, expr: &Expr) { | ||
match expr { | ||
Expr::GeneratorExp(ast::ExprGeneratorExp { | ||
elt, generators, .. | ||
}) | ||
| Expr::DictComp(ast::ExprDictComp { | ||
value: elt, | ||
generators, | ||
.. | ||
}) | ||
| Expr::SetComp(ast::ExprSetComp { | ||
elt, generators, .. | ||
}) | ||
| Expr::ListComp(ast::ExprListComp { | ||
elt, generators, .. | ||
}) => { | ||
for comp in generators { | ||
if let Some((sequence, index_name, value_name)) = | ||
enumerate_items(checker, &comp.iter, &comp.target) | ||
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
let mut visitor = SubscriptVisitor::new(&sequence, &index_name); | ||
|
||
visitor.visit_expr(elt.as_ref()); | ||
|
||
for range in visitor.diagnostic_ranges { | ||
let mut diagnostic = Diagnostic::new(UnnecessaryListIndexLookup, range); | ||
|
||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( | ||
value_name.clone(), | ||
range, | ||
))); | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} | ||
} | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
fn enumerate_items( | ||
checker: &mut Checker, | ||
call_expr: &Expr, | ||
tuple_expr: &Expr, | ||
) -> Option<(String, String, String)> { | ||
let Expr::Call(ast::ExprCall { | ||
func, arguments, .. | ||
}) = call_expr | ||
else { | ||
return None; | ||
}; | ||
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good change, I've also added detection for |
||
if !checker.semantic().is_builtin("enumerate") { | ||
return None; | ||
}; | ||
|
||
let Expr::Tuple(ast::ExprTuple { elts, .. }) = tuple_expr else { | ||
return None; | ||
}; | ||
let [index, value] = elts.as_slice() else { | ||
return None; | ||
}; | ||
|
||
// Grab the variable names | ||
let Expr::Name(ast::ExprName { id: index_name, .. }) = index else { | ||
return None; | ||
}; | ||
|
||
let Expr::Name(ast::ExprName { id: value_name, .. }) = value else { | ||
return None; | ||
}; | ||
|
||
// If either of the variable names are intentionally ignored by naming them `_`, then don't emit | ||
if index_name == "_" || value_name == "_" { | ||
return None; | ||
} | ||
|
||
// Get the first argument of the enumerate call | ||
let Some(Expr::Name(ast::ExprName { id: sequence, .. })) = arguments.args.first() else { | ||
return None; | ||
}; | ||
|
||
Some((sequence.clone(), index_name.clone(), value_name.clone())) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about a case where
index
is mutated? e.g.index += 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a case for that, as well as the sequence itself being mutated! Thanks!