mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
[flake8-simplify
] Implement enumerate-for-loop
(SIM113
) (#7777)
Implements SIM113 from #998 Added tests Limitations - No fix yet - Only flag cases where index variable immediately precede `for` loop @charliermarsh please review and let me know any improvements --------- Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
This commit is contained in:
parent
b6ce4f5f3a
commit
0003c730e0
14 changed files with 464 additions and 11 deletions
|
@ -645,6 +645,24 @@ pub fn resolve_assignment<'a>(
|
|||
pub fn find_assigned_value<'a>(symbol: &str, semantic: &'a SemanticModel<'a>) -> Option<&'a Expr> {
|
||||
let binding_id = semantic.lookup_symbol(symbol)?;
|
||||
let binding = semantic.binding(binding_id);
|
||||
find_binding_value(symbol, binding, semantic)
|
||||
}
|
||||
|
||||
/// Find the assigned [`Expr`] for a given [`Binding`], if any.
|
||||
///
|
||||
/// For example given:
|
||||
/// ```python
|
||||
/// foo = 42
|
||||
/// (bar, bla) = 1, "str"
|
||||
/// ```
|
||||
///
|
||||
/// This function will return a `NumberLiteral` with value `Int(42)` when called with `foo` and a
|
||||
/// `StringLiteral` with value `"str"` when called with `bla`.
|
||||
pub fn find_binding_value<'a>(
|
||||
symbol: &str,
|
||||
binding: &Binding,
|
||||
semantic: &'a SemanticModel,
|
||||
) -> Option<&'a Expr> {
|
||||
match binding.kind {
|
||||
// Ex) `x := 1`
|
||||
BindingKind::NamedExprAssignment => {
|
||||
|
|
|
@ -129,6 +129,11 @@ impl<'a> Scope<'a> {
|
|||
self.shadowed_bindings.get(&id).copied()
|
||||
}
|
||||
|
||||
/// Returns an iterator over all bindings that the given binding shadows, including itself.
|
||||
pub fn shadowed_bindings(&self, id: BindingId) -> impl Iterator<Item = BindingId> + '_ {
|
||||
std::iter::successors(Some(id), |id| self.shadowed_bindings.get(id).copied())
|
||||
}
|
||||
|
||||
/// Adds a reference to a star import (e.g., `from sys import *`) to this scope.
|
||||
pub fn add_star_import(&mut self, import: StarImport<'a>) {
|
||||
self.star_imports.push(import);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue