From 74effb40b965ea99f92d5096a49a17bbfaf34add Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 22 May 2023 23:56:00 -0400 Subject: [PATCH] Rename `index` to `binding_id` in a few iterators (#4594) --- crates/ruff/src/checkers/ast/mod.rs | 75 +++++++++++-------- .../rules/unused_loop_control_variable.rs | 4 +- .../rules/flake8_unused_arguments/rules.rs | 5 +- .../rules/pyflakes/rules/undefined_local.rs | 3 +- .../rules/pyflakes/rules/unused_annotation.rs | 4 +- .../rules/pyflakes/rules/unused_variable.rs | 2 +- .../rules/pylint/rules/global_statement.rs | 4 +- .../rules/useless_object_inheritance.rs | 4 +- crates/ruff_python_semantic/src/model.rs | 2 +- 9 files changed, 59 insertions(+), 44 deletions(-) diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index ab204814d5..7fbc9f4d37 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -2065,8 +2065,10 @@ where .semantic_model .global_scope() .get(name) - .map_or(true, |index| { - self.semantic_model.bindings[*index].kind.is_annotation() + .map_or(true, |binding_id| { + self.semantic_model.bindings[*binding_id] + .kind + .is_annotation() }) { let id = self.semantic_model.bindings.push(Binding { @@ -2128,8 +2130,10 @@ where .semantic_model .global_scope() .get(name) - .map_or(true, |index| { - self.semantic_model.bindings[*index].kind.is_annotation() + .map_or(true, |binding_id| { + self.semantic_model.bindings[*binding_id] + .kind + .is_annotation() }) { let id = self.semantic_model.bindings.push(Binding { @@ -4378,11 +4382,11 @@ where walk_excepthandler(self, excepthandler); - if let Some(index) = { + if let Some(binding_id) = { let scope = self.semantic_model.scope_mut(); &scope.remove(name) } { - if !self.semantic_model.bindings[*index].used() { + if !self.semantic_model.bindings[*binding_id].used() { if self.settings.rules.enabled(Rule::UnusedVariable) { let mut diagnostic = Diagnostic::new( pyflakes::rules::UnusedVariable { name: name.into() }, @@ -4402,9 +4406,9 @@ where } } - if let Some(index) = definition { + if let Some(binding_id) = definition { let scope = self.semantic_model.scope_mut(); - scope.add(name, index); + scope.add(name, binding_id); } } None => walk_excepthandler(self, excepthandler), @@ -4759,8 +4763,8 @@ impl<'a> Checker<'a> { }; let scope = &mut self.semantic_model.scopes[scope_id]; - let binding = if let Some(index) = scope.get(name) { - let existing = &self.semantic_model.bindings[*index]; + let binding = if let Some(binding_id) = scope.get(name) { + let existing = &self.semantic_model.bindings[*binding_id]; match &existing.kind { BindingKind::Builtin => { // Avoid overriding builtins. @@ -4903,9 +4907,14 @@ impl<'a> Checker<'a> { { if matches!(self.semantic_model.scope().kind, ScopeKind::Function(..)) { // Ignore globals. - if !self.semantic_model.scope().get(id).map_or(false, |index| { - self.semantic_model.bindings[*index].kind.is_global() - }) { + if !self + .semantic_model + .scope() + .get(id) + .map_or(false, |binding_id| { + self.semantic_model.bindings[*binding_id].kind.is_global() + }) + { pep8_naming::rules::non_lowercase_variable_in_function(self, expr, parent, id); } } @@ -5030,9 +5039,9 @@ impl<'a> Checker<'a> { // Grab the existing bound __all__ values. if let Stmt::AugAssign(_) = parent { - if let Some(index) = scope.get("__all__") { + if let Some(binding_id) = scope.get("__all__") { if let BindingKind::Export(Export { names: existing }) = - &self.semantic_model.bindings[*index].kind + &self.semantic_model.bindings[*binding_id].kind { names.extend_from_slice(existing); } @@ -5324,7 +5333,7 @@ impl<'a> Checker<'a> { let global_scope = self.semantic_model.global_scope(); let all_names: Option<(&[&str], TextRange)> = global_scope .get("__all__") - .map(|index| &self.semantic_model.bindings[*index]) + .map(|binding_id| &self.semantic_model.bindings[*binding_id]) .and_then(|binding| match &binding.kind { BindingKind::Export(Export { names }) => { Some((names.as_slice(), binding.range)) @@ -5344,8 +5353,8 @@ impl<'a> Checker<'a> { }; if let Some((bindings, range)) = all_bindings { - for index in bindings { - self.semantic_model.bindings[index].mark_used( + for binding_id in bindings { + self.semantic_model.bindings[binding_id].mark_used( ScopeId::global(), range, ExecutionContext::Runtime, @@ -5358,7 +5367,7 @@ impl<'a> Checker<'a> { .semantic_model .global_scope() .get("__all__") - .map(|index| &self.semantic_model.bindings[*index]) + .map(|binding_id| &self.semantic_model.bindings[*binding_id]) .and_then(|binding| match &binding.kind { BindingKind::Export(Export { names }) => Some((names.as_slice(), binding.range)), _ => None, @@ -5377,7 +5386,7 @@ impl<'a> Checker<'a> { .map(|scope| { scope .binding_ids() - .map(|index| &self.semantic_model.bindings[*index]) + .map(|binding_id| &self.semantic_model.bindings[*binding_id]) .filter(|binding| { flake8_type_checking::helpers::is_valid_runtime_import(binding) }) @@ -5438,8 +5447,8 @@ impl<'a> Checker<'a> { // PLW0602 if self.settings.rules.enabled(Rule::GlobalVariableNotAssigned) { - for (name, index) in scope.bindings() { - let binding = &self.semantic_model.bindings[*index]; + for (name, binding_id) in scope.bindings() { + let binding = &self.semantic_model.bindings[*binding_id]; if binding.kind.is_global() { if let Some(source) = binding.source { let stmt = &self.semantic_model.stmts[source]; @@ -5465,8 +5474,8 @@ impl<'a> Checker<'a> { // unused. Note that we only store references in `redefinitions` if // the bindings are in different scopes. if self.settings.rules.enabled(Rule::RedefinedWhileUnused) { - for (name, index) in scope.bindings() { - let binding = &self.semantic_model.bindings[*index]; + for (name, binding_id) in scope.bindings() { + let binding = &self.semantic_model.bindings[*binding_id]; if matches!( binding.kind, @@ -5479,9 +5488,11 @@ impl<'a> Checker<'a> { continue; } - if let Some(indices) = self.semantic_model.shadowed_bindings.get(index) { - for index in indices { - let rebound = &self.semantic_model.bindings[*index]; + if let Some(shadowed_ids) = + self.semantic_model.shadowed_bindings.get(binding_id) + { + for binding_id in shadowed_ids { + let rebound = &self.semantic_model.bindings[*binding_id]; #[allow(deprecated)] let line = self.locator.compute_line_index( binding @@ -5522,8 +5533,8 @@ impl<'a> Checker<'a> { .copied() .collect() }; - for index in scope.binding_ids() { - let binding = &self.semantic_model.bindings[*index]; + for binding_id in scope.binding_ids() { + let binding = &self.semantic_model.bindings[*binding_id]; if let Some(diagnostic) = flake8_type_checking::rules::runtime_import_in_type_checking_block(binding) @@ -5557,8 +5568,8 @@ impl<'a> Checker<'a> { let mut ignored: FxHashMap> = FxHashMap::default(); - for index in scope.binding_ids() { - let binding = &self.semantic_model.bindings[*index]; + for binding_id in scope.binding_ids() { + let binding = &self.semantic_model.bindings[*binding_id]; let full_name = match &binding.kind { BindingKind::Importation(Importation { full_name, .. }) => full_name, @@ -5781,7 +5792,7 @@ impl<'a> Checker<'a> { let global_scope = self.semantic_model.global_scope(); let exports: Option<&[&str]> = global_scope .get("__all__") - .map(|index| &self.semantic_model.bindings[*index]) + .map(|binding_id| &self.semantic_model.bindings[*binding_id]) .and_then(|binding| match &binding.kind { BindingKind::Export(Export { names }) => Some(names.as_slice()), _ => None, diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs b/crates/ruff/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs index cdb4de65f5..58df55f2f7 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs @@ -154,8 +154,8 @@ pub(crate) fn unused_loop_control_variable(checker: &mut Checker, target: &Expr, if certainty.into() && checker.patch(diagnostic.kind.rule()) { // Find the `BindingKind::LoopVar` corresponding to the name. let scope = checker.semantic_model().scope(); - let binding = scope.bindings_for_name(name).find_map(|index| { - let binding = &checker.semantic_model().bindings[*index]; + let binding = scope.bindings_for_name(name).find_map(|binding_id| { + let binding = &checker.semantic_model().bindings[*binding_id]; binding.source.and_then(|source| { (Some(source) == checker.semantic_model().stmt_id).then_some(binding) }) diff --git a/crates/ruff/src/rules/flake8_unused_arguments/rules.rs b/crates/ruff/src/rules/flake8_unused_arguments/rules.rs index 344cc8f342..e9d5d08d23 100644 --- a/crates/ruff/src/rules/flake8_unused_arguments/rules.rs +++ b/crates/ruff/src/rules/flake8_unused_arguments/rules.rs @@ -258,7 +258,10 @@ fn call<'a>( ) -> Vec { let mut diagnostics: Vec = vec![]; for arg in args { - if let Some(binding) = values.get(arg.arg.as_str()).map(|index| &bindings[*index]) { + if let Some(binding) = values + .get(arg.arg.as_str()) + .map(|binding_id| &bindings[*binding_id]) + { if !binding.used() && binding.kind.is_argument() && !dummy_variable_rgx.is_match(arg.arg.as_str()) diff --git a/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs b/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs index 61aa84640f..2df6f58f6a 100644 --- a/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs +++ b/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs @@ -43,13 +43,12 @@ pub(crate) fn undefined_local(checker: &mut Checker, name: &str) { // If the name was defined in that scope... if let Some(binding) = scope .get(name) - .map(|index| &checker.semantic_model().bindings[*index]) + .map(|binding_id| &checker.semantic_model().bindings[*binding_id]) { // And has already been accessed in the current scope... if let Some((scope_id, location)) = binding.runtime_usage { if scope_id == checker.semantic_model().scope_id { // Then it's probably an error. - return Some(location); } } diff --git a/crates/ruff/src/rules/pyflakes/rules/unused_annotation.rs b/crates/ruff/src/rules/pyflakes/rules/unused_annotation.rs index 64d446bff8..62b2093bc8 100644 --- a/crates/ruff/src/rules/pyflakes/rules/unused_annotation.rs +++ b/crates/ruff/src/rules/pyflakes/rules/unused_annotation.rs @@ -23,9 +23,9 @@ pub(crate) fn unused_annotation(checker: &mut Checker, scope: ScopeId) { let bindings: Vec<_> = scope .bindings() - .filter_map(|(name, index)| { + .filter_map(|(name, binding_id)| { let name = *name; - let binding = &checker.semantic_model().bindings[*index]; + let binding = &checker.semantic_model().bindings[*binding_id]; if !binding.used() && binding.kind.is_annotation() diff --git a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs index 5e8922793e..932c318ebc 100644 --- a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs +++ b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs @@ -320,7 +320,7 @@ pub(crate) fn unused_variable(checker: &mut Checker, scope: ScopeId) { let bindings: Vec<_> = scope .bindings() - .map(|(name, index)| (*name, &checker.semantic_model().bindings[*index])) + .map(|(name, binding_id)| (*name, &checker.semantic_model().bindings[*binding_id])) .filter_map(|(name, binding)| { if !binding.used() && (binding.kind.is_assignment() || binding.kind.is_named_expr_assignment()) diff --git a/crates/ruff/src/rules/pylint/rules/global_statement.rs b/crates/ruff/src/rules/pylint/rules/global_statement.rs index 976b52a61e..105afdd345 100644 --- a/crates/ruff/src/rules/pylint/rules/global_statement.rs +++ b/crates/ruff/src/rules/pylint/rules/global_statement.rs @@ -56,8 +56,8 @@ impl Violation for GlobalStatement { /// PLW0603 pub(crate) fn global_statement(checker: &mut Checker, name: &str) { let scope = checker.semantic_model().scope(); - if let Some(index) = scope.get(name) { - let binding = &checker.semantic_model().bindings[*index]; + if let Some(binding_id) = scope.get(name) { + let binding = &checker.semantic_model().bindings[*binding_id]; if binding.kind.is_global() { let source = checker.semantic_model().stmts[binding .source diff --git a/crates/ruff/src/rules/pyupgrade/rules/useless_object_inheritance.rs b/crates/ruff/src/rules/pyupgrade/rules/useless_object_inheritance.rs index 4ae6d1f3dd..a1e3a4068d 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/useless_object_inheritance.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/useless_object_inheritance.rs @@ -35,7 +35,9 @@ fn rule(name: &str, bases: &[Expr], scope: &Scope, bindings: &Bindings) -> Optio continue; } if !matches!( - scope.get(id.as_str()).map(|index| &bindings[*index]), + scope + .get(id.as_str()) + .map(|binding_id| &bindings[*binding_id]), None | Some(Binding { kind: BindingKind::Builtin, .. diff --git a/crates/ruff_python_semantic/src/model.rs b/crates/ruff_python_semantic/src/model.rs index 99bdc070eb..20635893ce 100644 --- a/crates/ruff_python_semantic/src/model.rs +++ b/crates/ruff_python_semantic/src/model.rs @@ -105,7 +105,7 @@ impl<'a> SemanticModel<'a> { pub fn find_binding(&self, member: &str) -> Option<&Binding> { self.scopes() .find_map(|scope| scope.get(member)) - .map(|index| &self.bindings[*index]) + .map(|binding_id| &self.bindings[*binding_id]) } /// Return `true` if `member` is bound as a builtin.