-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4113d65
commit 7cff2fb
Showing
8 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
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,5 @@ | ||
letters = ["a", "b", "c"] | ||
|
||
for index, letter in enumerate(letters): | ||
print(letters[index]) # PLR1736 | ||
blah = letters[index] # PLR1736 |
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
142 changes: 142 additions & 0 deletions
142
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,142 @@ | ||
use ruff_python_ast::{self as ast, Expr, 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. | ||
/// | ||
/// ## Why is this bad? | ||
/// The value is already accessible by the 2nd variable from the enumeration. | ||
/// | ||
/// ## 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") | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
format!("Remove unnecessary list index lookup") | ||
} | ||
} | ||
|
||
struct SubscriptVisitor<'a> { | ||
sequence_name: &'a str, | ||
index_name: &'a str, | ||
diagnostic_ranges: Vec<TextRange>, | ||
} | ||
|
||
impl<'a> SubscriptVisitor<'a> { | ||
fn new(sequence_name: &'a str, index_name: &'a str) -> Self { | ||
Self { | ||
sequence_name, | ||
index_name, | ||
diagnostic_ranges: Vec::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Visitor<'_> for SubscriptVisitor<'a> { | ||
fn visit_expr(&mut self, expr: &Expr) { | ||
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
_ => visitor::walk_expr(self, expr), | ||
} | ||
} | ||
} | ||
|
||
/// PLR1736 | ||
pub(crate) fn unnecessary_list_index_lookup(checker: &mut Checker, stmt_for: &StmtFor) { | ||
let Expr::Call(ast::ExprCall { | ||
func, arguments, .. | ||
}) = stmt_for.iter.as_ref() | ||
else { | ||
return; | ||
}; | ||
|
||
// Check that the function is the `enumerate` builtin. | ||
let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() else { | ||
return; | ||
}; | ||
if id != "enumerate" { | ||
return; | ||
}; | ||
if !checker.semantic().is_builtin("enumerate") { | ||
return; | ||
}; | ||
|
||
let Expr::Tuple(ast::ExprTuple { elts, .. }) = stmt_for.target.as_ref() else { | ||
return; | ||
}; | ||
let [index, value] = elts.as_slice() else { | ||
return; | ||
}; | ||
|
||
// Grab the variable names | ||
let Expr::Name(ast::ExprName { id: index_name, .. }) = index else { | ||
return; | ||
}; | ||
|
||
let Expr::Name(ast::ExprName { id: value_name, .. }) = value else { | ||
return; | ||
}; | ||
|
||
// Get the first argument of the enumerate call | ||
let Some(Expr::Name(ast::ExprName { id: sequence, .. })) = arguments.args.first() 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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...napshots/ruff_linter__rules__pylint__tests__PLR1736_unnecessary_list_index_lookup.py.snap
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,37 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
unnecessary_list_index_lookup.py:4:11: PLR1736 [*] Unnecessary list index lookup | ||
| | ||
3 | for index, letter in enumerate(letters): | ||
4 | print(letters[index]) # PLR1736 | ||
| ^^^^^^^^^^^^^^ PLR1736 | ||
5 | blah = letters[index] # PLR1736 | ||
| | ||
= help: Remove unnecessary list index lookup | ||
|
||
ℹ Fix | ||
1 1 | letters = ["a", "b", "c"] | ||
2 2 | | ||
3 3 | for index, letter in enumerate(letters): | ||
4 |- print(letters[index]) # PLR1736 | ||
4 |+ print(letter) # PLR1736 | ||
5 5 | blah = letters[index] # PLR1736 | ||
|
||
unnecessary_list_index_lookup.py:5:12: PLR1736 [*] Unnecessary list index lookup | ||
| | ||
3 | for index, letter in enumerate(letters): | ||
4 | print(letters[index]) # PLR1736 | ||
5 | blah = letters[index] # PLR1736 | ||
| ^^^^^^^^^^^^^^ PLR1736 | ||
| | ||
= help: Remove unnecessary list index lookup | ||
|
||
ℹ Fix | ||
2 2 | | ||
3 3 | for index, letter in enumerate(letters): | ||
4 4 | print(letters[index]) # PLR1736 | ||
5 |- blah = letters[index] # PLR1736 | ||
5 |+ blah = letter # PLR1736 | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.