mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 17:40:37 +00:00
Rename index
to binding_id
in a few iterators (#4594)
This commit is contained in:
parent
6c3724ab98
commit
74effb40b9
9 changed files with 59 additions and 44 deletions
|
@ -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<BindingContext, Vec<UnusedImport>> =
|
||||
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,
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -258,7 +258,10 @@ fn call<'a>(
|
|||
) -> Vec<Diagnostic> {
|
||||
let mut diagnostics: Vec<Diagnostic> = 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())
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
..
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue