mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:50:38 +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
|
.semantic_model
|
||||||
.global_scope()
|
.global_scope()
|
||||||
.get(name)
|
.get(name)
|
||||||
.map_or(true, |index| {
|
.map_or(true, |binding_id| {
|
||||||
self.semantic_model.bindings[*index].kind.is_annotation()
|
self.semantic_model.bindings[*binding_id]
|
||||||
|
.kind
|
||||||
|
.is_annotation()
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
let id = self.semantic_model.bindings.push(Binding {
|
let id = self.semantic_model.bindings.push(Binding {
|
||||||
|
@ -2128,8 +2130,10 @@ where
|
||||||
.semantic_model
|
.semantic_model
|
||||||
.global_scope()
|
.global_scope()
|
||||||
.get(name)
|
.get(name)
|
||||||
.map_or(true, |index| {
|
.map_or(true, |binding_id| {
|
||||||
self.semantic_model.bindings[*index].kind.is_annotation()
|
self.semantic_model.bindings[*binding_id]
|
||||||
|
.kind
|
||||||
|
.is_annotation()
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
let id = self.semantic_model.bindings.push(Binding {
|
let id = self.semantic_model.bindings.push(Binding {
|
||||||
|
@ -4378,11 +4382,11 @@ where
|
||||||
|
|
||||||
walk_excepthandler(self, excepthandler);
|
walk_excepthandler(self, excepthandler);
|
||||||
|
|
||||||
if let Some(index) = {
|
if let Some(binding_id) = {
|
||||||
let scope = self.semantic_model.scope_mut();
|
let scope = self.semantic_model.scope_mut();
|
||||||
&scope.remove(name)
|
&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) {
|
if self.settings.rules.enabled(Rule::UnusedVariable) {
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
pyflakes::rules::UnusedVariable { name: name.into() },
|
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();
|
let scope = self.semantic_model.scope_mut();
|
||||||
scope.add(name, index);
|
scope.add(name, binding_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => walk_excepthandler(self, excepthandler),
|
None => walk_excepthandler(self, excepthandler),
|
||||||
|
@ -4759,8 +4763,8 @@ impl<'a> Checker<'a> {
|
||||||
};
|
};
|
||||||
let scope = &mut self.semantic_model.scopes[scope_id];
|
let scope = &mut self.semantic_model.scopes[scope_id];
|
||||||
|
|
||||||
let binding = if let Some(index) = scope.get(name) {
|
let binding = if let Some(binding_id) = scope.get(name) {
|
||||||
let existing = &self.semantic_model.bindings[*index];
|
let existing = &self.semantic_model.bindings[*binding_id];
|
||||||
match &existing.kind {
|
match &existing.kind {
|
||||||
BindingKind::Builtin => {
|
BindingKind::Builtin => {
|
||||||
// Avoid overriding builtins.
|
// Avoid overriding builtins.
|
||||||
|
@ -4903,9 +4907,14 @@ impl<'a> Checker<'a> {
|
||||||
{
|
{
|
||||||
if matches!(self.semantic_model.scope().kind, ScopeKind::Function(..)) {
|
if matches!(self.semantic_model.scope().kind, ScopeKind::Function(..)) {
|
||||||
// Ignore globals.
|
// Ignore globals.
|
||||||
if !self.semantic_model.scope().get(id).map_or(false, |index| {
|
if !self
|
||||||
self.semantic_model.bindings[*index].kind.is_global()
|
.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);
|
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.
|
// Grab the existing bound __all__ values.
|
||||||
if let Stmt::AugAssign(_) = parent {
|
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 }) =
|
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);
|
names.extend_from_slice(existing);
|
||||||
}
|
}
|
||||||
|
@ -5324,7 +5333,7 @@ impl<'a> Checker<'a> {
|
||||||
let global_scope = self.semantic_model.global_scope();
|
let global_scope = self.semantic_model.global_scope();
|
||||||
let all_names: Option<(&[&str], TextRange)> = global_scope
|
let all_names: Option<(&[&str], TextRange)> = global_scope
|
||||||
.get("__all__")
|
.get("__all__")
|
||||||
.map(|index| &self.semantic_model.bindings[*index])
|
.map(|binding_id| &self.semantic_model.bindings[*binding_id])
|
||||||
.and_then(|binding| match &binding.kind {
|
.and_then(|binding| match &binding.kind {
|
||||||
BindingKind::Export(Export { names }) => {
|
BindingKind::Export(Export { names }) => {
|
||||||
Some((names.as_slice(), binding.range))
|
Some((names.as_slice(), binding.range))
|
||||||
|
@ -5344,8 +5353,8 @@ impl<'a> Checker<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some((bindings, range)) = all_bindings {
|
if let Some((bindings, range)) = all_bindings {
|
||||||
for index in bindings {
|
for binding_id in bindings {
|
||||||
self.semantic_model.bindings[index].mark_used(
|
self.semantic_model.bindings[binding_id].mark_used(
|
||||||
ScopeId::global(),
|
ScopeId::global(),
|
||||||
range,
|
range,
|
||||||
ExecutionContext::Runtime,
|
ExecutionContext::Runtime,
|
||||||
|
@ -5358,7 +5367,7 @@ impl<'a> Checker<'a> {
|
||||||
.semantic_model
|
.semantic_model
|
||||||
.global_scope()
|
.global_scope()
|
||||||
.get("__all__")
|
.get("__all__")
|
||||||
.map(|index| &self.semantic_model.bindings[*index])
|
.map(|binding_id| &self.semantic_model.bindings[*binding_id])
|
||||||
.and_then(|binding| match &binding.kind {
|
.and_then(|binding| match &binding.kind {
|
||||||
BindingKind::Export(Export { names }) => Some((names.as_slice(), binding.range)),
|
BindingKind::Export(Export { names }) => Some((names.as_slice(), binding.range)),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
@ -5377,7 +5386,7 @@ impl<'a> Checker<'a> {
|
||||||
.map(|scope| {
|
.map(|scope| {
|
||||||
scope
|
scope
|
||||||
.binding_ids()
|
.binding_ids()
|
||||||
.map(|index| &self.semantic_model.bindings[*index])
|
.map(|binding_id| &self.semantic_model.bindings[*binding_id])
|
||||||
.filter(|binding| {
|
.filter(|binding| {
|
||||||
flake8_type_checking::helpers::is_valid_runtime_import(binding)
|
flake8_type_checking::helpers::is_valid_runtime_import(binding)
|
||||||
})
|
})
|
||||||
|
@ -5438,8 +5447,8 @@ impl<'a> Checker<'a> {
|
||||||
|
|
||||||
// PLW0602
|
// PLW0602
|
||||||
if self.settings.rules.enabled(Rule::GlobalVariableNotAssigned) {
|
if self.settings.rules.enabled(Rule::GlobalVariableNotAssigned) {
|
||||||
for (name, index) in scope.bindings() {
|
for (name, binding_id) in scope.bindings() {
|
||||||
let binding = &self.semantic_model.bindings[*index];
|
let binding = &self.semantic_model.bindings[*binding_id];
|
||||||
if binding.kind.is_global() {
|
if binding.kind.is_global() {
|
||||||
if let Some(source) = binding.source {
|
if let Some(source) = binding.source {
|
||||||
let stmt = &self.semantic_model.stmts[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
|
// unused. Note that we only store references in `redefinitions` if
|
||||||
// the bindings are in different scopes.
|
// the bindings are in different scopes.
|
||||||
if self.settings.rules.enabled(Rule::RedefinedWhileUnused) {
|
if self.settings.rules.enabled(Rule::RedefinedWhileUnused) {
|
||||||
for (name, index) in scope.bindings() {
|
for (name, binding_id) in scope.bindings() {
|
||||||
let binding = &self.semantic_model.bindings[*index];
|
let binding = &self.semantic_model.bindings[*binding_id];
|
||||||
|
|
||||||
if matches!(
|
if matches!(
|
||||||
binding.kind,
|
binding.kind,
|
||||||
|
@ -5479,9 +5488,11 @@ impl<'a> Checker<'a> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(indices) = self.semantic_model.shadowed_bindings.get(index) {
|
if let Some(shadowed_ids) =
|
||||||
for index in indices {
|
self.semantic_model.shadowed_bindings.get(binding_id)
|
||||||
let rebound = &self.semantic_model.bindings[*index];
|
{
|
||||||
|
for binding_id in shadowed_ids {
|
||||||
|
let rebound = &self.semantic_model.bindings[*binding_id];
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
let line = self.locator.compute_line_index(
|
let line = self.locator.compute_line_index(
|
||||||
binding
|
binding
|
||||||
|
@ -5522,8 +5533,8 @@ impl<'a> Checker<'a> {
|
||||||
.copied()
|
.copied()
|
||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
for index in scope.binding_ids() {
|
for binding_id in scope.binding_ids() {
|
||||||
let binding = &self.semantic_model.bindings[*index];
|
let binding = &self.semantic_model.bindings[*binding_id];
|
||||||
|
|
||||||
if let Some(diagnostic) =
|
if let Some(diagnostic) =
|
||||||
flake8_type_checking::rules::runtime_import_in_type_checking_block(binding)
|
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>> =
|
let mut ignored: FxHashMap<BindingContext, Vec<UnusedImport>> =
|
||||||
FxHashMap::default();
|
FxHashMap::default();
|
||||||
|
|
||||||
for index in scope.binding_ids() {
|
for binding_id in scope.binding_ids() {
|
||||||
let binding = &self.semantic_model.bindings[*index];
|
let binding = &self.semantic_model.bindings[*binding_id];
|
||||||
|
|
||||||
let full_name = match &binding.kind {
|
let full_name = match &binding.kind {
|
||||||
BindingKind::Importation(Importation { full_name, .. }) => full_name,
|
BindingKind::Importation(Importation { full_name, .. }) => full_name,
|
||||||
|
@ -5781,7 +5792,7 @@ impl<'a> Checker<'a> {
|
||||||
let global_scope = self.semantic_model.global_scope();
|
let global_scope = self.semantic_model.global_scope();
|
||||||
let exports: Option<&[&str]> = global_scope
|
let exports: Option<&[&str]> = global_scope
|
||||||
.get("__all__")
|
.get("__all__")
|
||||||
.map(|index| &self.semantic_model.bindings[*index])
|
.map(|binding_id| &self.semantic_model.bindings[*binding_id])
|
||||||
.and_then(|binding| match &binding.kind {
|
.and_then(|binding| match &binding.kind {
|
||||||
BindingKind::Export(Export { names }) => Some(names.as_slice()),
|
BindingKind::Export(Export { names }) => Some(names.as_slice()),
|
||||||
_ => None,
|
_ => 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()) {
|
if certainty.into() && checker.patch(diagnostic.kind.rule()) {
|
||||||
// Find the `BindingKind::LoopVar` corresponding to the name.
|
// Find the `BindingKind::LoopVar` corresponding to the name.
|
||||||
let scope = checker.semantic_model().scope();
|
let scope = checker.semantic_model().scope();
|
||||||
let binding = scope.bindings_for_name(name).find_map(|index| {
|
let binding = scope.bindings_for_name(name).find_map(|binding_id| {
|
||||||
let binding = &checker.semantic_model().bindings[*index];
|
let binding = &checker.semantic_model().bindings[*binding_id];
|
||||||
binding.source.and_then(|source| {
|
binding.source.and_then(|source| {
|
||||||
(Some(source) == checker.semantic_model().stmt_id).then_some(binding)
|
(Some(source) == checker.semantic_model().stmt_id).then_some(binding)
|
||||||
})
|
})
|
||||||
|
|
|
@ -258,7 +258,10 @@ fn call<'a>(
|
||||||
) -> Vec<Diagnostic> {
|
) -> Vec<Diagnostic> {
|
||||||
let mut diagnostics: Vec<Diagnostic> = vec![];
|
let mut diagnostics: Vec<Diagnostic> = vec![];
|
||||||
for arg in args {
|
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()
|
if !binding.used()
|
||||||
&& binding.kind.is_argument()
|
&& binding.kind.is_argument()
|
||||||
&& !dummy_variable_rgx.is_match(arg.arg.as_str())
|
&& !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 the name was defined in that scope...
|
||||||
if let Some(binding) = scope
|
if let Some(binding) = scope
|
||||||
.get(name)
|
.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...
|
// And has already been accessed in the current scope...
|
||||||
if let Some((scope_id, location)) = binding.runtime_usage {
|
if let Some((scope_id, location)) = binding.runtime_usage {
|
||||||
if scope_id == checker.semantic_model().scope_id {
|
if scope_id == checker.semantic_model().scope_id {
|
||||||
// Then it's probably an error.
|
// Then it's probably an error.
|
||||||
|
|
||||||
return Some(location);
|
return Some(location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,9 +23,9 @@ pub(crate) fn unused_annotation(checker: &mut Checker, scope: ScopeId) {
|
||||||
|
|
||||||
let bindings: Vec<_> = scope
|
let bindings: Vec<_> = scope
|
||||||
.bindings()
|
.bindings()
|
||||||
.filter_map(|(name, index)| {
|
.filter_map(|(name, binding_id)| {
|
||||||
let name = *name;
|
let name = *name;
|
||||||
let binding = &checker.semantic_model().bindings[*index];
|
let binding = &checker.semantic_model().bindings[*binding_id];
|
||||||
|
|
||||||
if !binding.used()
|
if !binding.used()
|
||||||
&& binding.kind.is_annotation()
|
&& binding.kind.is_annotation()
|
||||||
|
|
|
@ -320,7 +320,7 @@ pub(crate) fn unused_variable(checker: &mut Checker, scope: ScopeId) {
|
||||||
|
|
||||||
let bindings: Vec<_> = scope
|
let bindings: Vec<_> = scope
|
||||||
.bindings()
|
.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)| {
|
.filter_map(|(name, binding)| {
|
||||||
if !binding.used()
|
if !binding.used()
|
||||||
&& (binding.kind.is_assignment() || binding.kind.is_named_expr_assignment())
|
&& (binding.kind.is_assignment() || binding.kind.is_named_expr_assignment())
|
||||||
|
|
|
@ -56,8 +56,8 @@ impl Violation for GlobalStatement {
|
||||||
/// PLW0603
|
/// PLW0603
|
||||||
pub(crate) fn global_statement(checker: &mut Checker, name: &str) {
|
pub(crate) fn global_statement(checker: &mut Checker, name: &str) {
|
||||||
let scope = checker.semantic_model().scope();
|
let scope = checker.semantic_model().scope();
|
||||||
if let Some(index) = scope.get(name) {
|
if let Some(binding_id) = scope.get(name) {
|
||||||
let binding = &checker.semantic_model().bindings[*index];
|
let binding = &checker.semantic_model().bindings[*binding_id];
|
||||||
if binding.kind.is_global() {
|
if binding.kind.is_global() {
|
||||||
let source = checker.semantic_model().stmts[binding
|
let source = checker.semantic_model().stmts[binding
|
||||||
.source
|
.source
|
||||||
|
|
|
@ -35,7 +35,9 @@ fn rule(name: &str, bases: &[Expr], scope: &Scope, bindings: &Bindings) -> Optio
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if !matches!(
|
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 {
|
None | Some(Binding {
|
||||||
kind: BindingKind::Builtin,
|
kind: BindingKind::Builtin,
|
||||||
..
|
..
|
||||||
|
|
|
@ -105,7 +105,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
pub fn find_binding(&self, member: &str) -> Option<&Binding> {
|
pub fn find_binding(&self, member: &str) -> Option<&Binding> {
|
||||||
self.scopes()
|
self.scopes()
|
||||||
.find_map(|scope| scope.get(member))
|
.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.
|
/// Return `true` if `member` is bound as a builtin.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue