mirror of https://github.com/astral-sh/ruff
Use copied
This commit is contained in:
parent
fcdc7bdd33
commit
524eda8a4b
|
|
@ -298,7 +298,7 @@ where
|
||||||
|
|
||||||
if let Some(binding_id) = binding_id {
|
if let Some(binding_id) = binding_id {
|
||||||
self.semantic_model.add_local_reference(
|
self.semantic_model.add_local_reference(
|
||||||
*binding_id,
|
binding_id,
|
||||||
stmt.range(),
|
stmt.range(),
|
||||||
ExecutionContext::Runtime,
|
ExecutionContext::Runtime,
|
||||||
);
|
);
|
||||||
|
|
@ -1898,7 +1898,7 @@ where
|
||||||
.global_scope()
|
.global_scope()
|
||||||
.get(name)
|
.get(name)
|
||||||
.map_or(true, |binding_id| {
|
.map_or(true, |binding_id| {
|
||||||
self.semantic_model.bindings[*binding_id]
|
self.semantic_model.bindings[binding_id]
|
||||||
.kind
|
.kind
|
||||||
.is_annotation()
|
.is_annotation()
|
||||||
})
|
})
|
||||||
|
|
@ -1962,7 +1962,7 @@ where
|
||||||
.global_scope()
|
.global_scope()
|
||||||
.get(name)
|
.get(name)
|
||||||
.map_or(true, |binding_id| {
|
.map_or(true, |binding_id| {
|
||||||
self.semantic_model.bindings[*binding_id]
|
self.semantic_model.bindings[binding_id]
|
||||||
.kind
|
.kind
|
||||||
.is_annotation()
|
.is_annotation()
|
||||||
})
|
})
|
||||||
|
|
@ -4007,7 +4007,7 @@ where
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let definition = self.semantic_model.scope().get(name).copied();
|
let definition = self.semantic_model.scope().get(name);
|
||||||
self.handle_node_store(
|
self.handle_node_store(
|
||||||
name,
|
name,
|
||||||
&Expr::Name(ast::ExprName {
|
&Expr::Name(ast::ExprName {
|
||||||
|
|
@ -4021,9 +4021,9 @@ where
|
||||||
|
|
||||||
if let Some(binding_id) = {
|
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.is_used(*binding_id) {
|
if !self.semantic_model.is_used(binding_id) {
|
||||||
if self.enabled(Rule::UnusedVariable) {
|
if self.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() },
|
||||||
|
|
@ -4296,7 +4296,7 @@ impl<'a> Checker<'a> {
|
||||||
.ancestors(self.semantic_model.scope_id)
|
.ancestors(self.semantic_model.scope_id)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find_map(|(stack_index, scope)| {
|
.find_map(|(stack_index, scope)| {
|
||||||
scope.get(name).map(|binding_id| (stack_index, *binding_id))
|
scope.get(name).map(|binding_id| (stack_index, binding_id))
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
let existing = &self.semantic_model.bindings[existing_binding_id];
|
let existing = &self.semantic_model.bindings[existing_binding_id];
|
||||||
|
|
@ -4391,7 +4391,7 @@ 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(binding_id) = scope.get(name) {
|
let binding = if let Some(binding_id) = scope.get(name) {
|
||||||
let existing = &self.semantic_model.bindings[*binding_id];
|
let existing = &self.semantic_model.bindings[binding_id];
|
||||||
match &existing.kind {
|
match &existing.kind {
|
||||||
BindingKind::Builtin => {
|
BindingKind::Builtin => {
|
||||||
// Avoid overriding builtins.
|
// Avoid overriding builtins.
|
||||||
|
|
@ -4525,7 +4525,7 @@ impl<'a> Checker<'a> {
|
||||||
.scope()
|
.scope()
|
||||||
.get(id)
|
.get(id)
|
||||||
.map_or(false, |binding_id| {
|
.map_or(false, |binding_id| {
|
||||||
self.semantic_model.bindings[*binding_id].kind.is_global()
|
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);
|
||||||
|
|
@ -4643,7 +4643,7 @@ impl<'a> Checker<'a> {
|
||||||
if let Stmt::AugAssign(_) = parent {
|
if let Stmt::AugAssign(_) = parent {
|
||||||
if let Some(binding_id) = 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[*binding_id].kind
|
&self.semantic_model.bindings[binding_id].kind
|
||||||
{
|
{
|
||||||
names.extend_from_slice(existing);
|
names.extend_from_slice(existing);
|
||||||
}
|
}
|
||||||
|
|
@ -4930,7 +4930,6 @@ 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__")
|
||||||
.copied()
|
|
||||||
.map(|binding_id| &self.semantic_model.bindings[binding_id])
|
.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 }) => {
|
||||||
|
|
@ -4943,7 +4942,7 @@ impl<'a> Checker<'a> {
|
||||||
(
|
(
|
||||||
names
|
names
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|name| global_scope.get(name).copied())
|
.filter_map(|name| global_scope.get(name))
|
||||||
.collect(),
|
.collect(),
|
||||||
range,
|
range,
|
||||||
)
|
)
|
||||||
|
|
@ -4965,7 +4964,6 @@ impl<'a> Checker<'a> {
|
||||||
.semantic_model
|
.semantic_model
|
||||||
.global_scope()
|
.global_scope()
|
||||||
.get("__all__")
|
.get("__all__")
|
||||||
.copied()
|
|
||||||
.map(|binding_id| &self.semantic_model.bindings[binding_id])
|
.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)),
|
||||||
|
|
@ -4985,7 +4983,7 @@ impl<'a> Checker<'a> {
|
||||||
.map(|scope| {
|
.map(|scope| {
|
||||||
scope
|
scope
|
||||||
.binding_ids()
|
.binding_ids()
|
||||||
.map(|binding_id| &self.semantic_model.bindings[*binding_id])
|
.map(|binding_id| &self.semantic_model.bindings[binding_id])
|
||||||
.filter(|binding| {
|
.filter(|binding| {
|
||||||
flake8_type_checking::helpers::is_valid_runtime_import(
|
flake8_type_checking::helpers::is_valid_runtime_import(
|
||||||
&self.semantic_model,
|
&self.semantic_model,
|
||||||
|
|
@ -5046,7 +5044,7 @@ impl<'a> Checker<'a> {
|
||||||
// PLW0602
|
// PLW0602
|
||||||
if self.enabled(Rule::GlobalVariableNotAssigned) {
|
if self.enabled(Rule::GlobalVariableNotAssigned) {
|
||||||
for (name, binding_id) in scope.bindings() {
|
for (name, binding_id) in scope.bindings() {
|
||||||
let binding = &self.semantic_model.bindings[*binding_id];
|
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];
|
||||||
|
|
@ -5073,7 +5071,7 @@ impl<'a> Checker<'a> {
|
||||||
// the bindings are in different scopes.
|
// the bindings are in different scopes.
|
||||||
if self.enabled(Rule::RedefinedWhileUnused) {
|
if self.enabled(Rule::RedefinedWhileUnused) {
|
||||||
for (name, binding_id) in scope.bindings() {
|
for (name, binding_id) in scope.bindings() {
|
||||||
let binding = &self.semantic_model.bindings[*binding_id];
|
let binding = &self.semantic_model.bindings[binding_id];
|
||||||
|
|
||||||
if matches!(
|
if matches!(
|
||||||
binding.kind,
|
binding.kind,
|
||||||
|
|
@ -5086,10 +5084,10 @@ impl<'a> Checker<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(shadowed_ids) =
|
if let Some(shadowed_ids) =
|
||||||
self.semantic_model.shadowed_bindings.get(binding_id)
|
self.semantic_model.shadowed_bindings.get(&binding_id)
|
||||||
{
|
{
|
||||||
for binding_id in shadowed_ids {
|
for binding_id in shadowed_ids.iter().copied() {
|
||||||
let rebound = &self.semantic_model.bindings[*binding_id];
|
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
|
||||||
|
|
@ -5131,7 +5129,7 @@ impl<'a> Checker<'a> {
|
||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
for binding_id in scope.binding_ids() {
|
for binding_id in scope.binding_ids() {
|
||||||
let binding = &self.semantic_model.bindings[*binding_id];
|
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(
|
flake8_type_checking::rules::runtime_import_in_type_checking_block(
|
||||||
|
|
@ -5170,7 +5168,7 @@ impl<'a> Checker<'a> {
|
||||||
FxHashMap::default();
|
FxHashMap::default();
|
||||||
|
|
||||||
for binding_id in scope.binding_ids() {
|
for binding_id in scope.binding_ids() {
|
||||||
let binding = &self.semantic_model.bindings[*binding_id];
|
let binding = &self.semantic_model.bindings[binding_id];
|
||||||
|
|
||||||
if binding.is_used() || binding.is_explicit_export() {
|
if binding.is_used() || binding.is_explicit_export() {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -5392,7 +5390,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(|binding_id| &self.semantic_model.bindings[*binding_id])
|
.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,
|
||||||
|
|
|
||||||
|
|
@ -157,7 +157,7 @@ pub(crate) fn unused_loop_control_variable(checker: &mut Checker, target: &Expr,
|
||||||
let scope = checker.semantic_model().scope();
|
let scope = checker.semantic_model().scope();
|
||||||
if scope
|
if scope
|
||||||
.bindings_for_name(name)
|
.bindings_for_name(name)
|
||||||
.map(|binding_id| &checker.semantic_model().bindings[*binding_id])
|
.map(|binding_id| &checker.semantic_model().bindings[binding_id])
|
||||||
.all(|binding| !binding.is_used())
|
.all(|binding| !binding.is_used())
|
||||||
{
|
{
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
|
|
|
||||||
|
|
@ -260,7 +260,7 @@ fn call<'a>(
|
||||||
for arg in args {
|
for arg in args {
|
||||||
if let Some(binding) = values
|
if let Some(binding) = values
|
||||||
.get(arg.arg.as_str())
|
.get(arg.arg.as_str())
|
||||||
.map(|binding_id| &bindings[*binding_id])
|
.map(|binding_id| &bindings[binding_id])
|
||||||
{
|
{
|
||||||
if binding.kind.is_argument()
|
if binding.kind.is_argument()
|
||||||
&& !binding.is_used()
|
&& !binding.is_used()
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ 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(|binding_id| &checker.semantic_model().bindings[*binding_id])
|
.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(range) = binding.references().find_map(|reference_id| {
|
if let Some(range) = binding.references().find_map(|reference_id| {
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,7 @@ pub(crate) fn unused_annotation(checker: &mut Checker, scope: ScopeId) {
|
||||||
let bindings: Vec<_> = scope
|
let bindings: Vec<_> = scope
|
||||||
.bindings()
|
.bindings()
|
||||||
.filter_map(|(name, binding_id)| {
|
.filter_map(|(name, binding_id)| {
|
||||||
let name = *name;
|
let binding = &checker.semantic_model().bindings[binding_id];
|
||||||
let binding = &checker.semantic_model().bindings[*binding_id];
|
|
||||||
|
|
||||||
if binding.kind.is_annotation()
|
if binding.kind.is_annotation()
|
||||||
&& !binding.is_used()
|
&& !binding.is_used()
|
||||||
&& !checker.settings.dummy_variable_rgx.is_match(name)
|
&& !checker.settings.dummy_variable_rgx.is_match(name)
|
||||||
|
|
@ -39,11 +37,8 @@ pub(crate) fn unused_annotation(checker: &mut Checker, scope: ScopeId) {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for (name, range) in bindings {
|
for (name, range) in bindings {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker
|
||||||
UnusedAnnotation {
|
.diagnostics
|
||||||
name: (*name).to_string(),
|
.push(Diagnostic::new(UnusedAnnotation { name }, range));
|
||||||
},
|
|
||||||
range,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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, binding_id)| (*name, &checker.semantic_model().bindings[*binding_id]))
|
.map(|(name, binding_id)| (name, &checker.semantic_model().bindings[binding_id]))
|
||||||
.filter_map(|(name, binding)| {
|
.filter_map(|(name, binding)| {
|
||||||
if (binding.kind.is_assignment() || binding.kind.is_named_expr_assignment())
|
if (binding.kind.is_assignment() || binding.kind.is_named_expr_assignment())
|
||||||
&& !binding.is_used()
|
&& !binding.is_used()
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ impl Violation for GlobalStatement {
|
||||||
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(binding_id) = scope.get(name) {
|
if let Some(binding_id) = scope.get(name) {
|
||||||
let binding = &checker.semantic_model().bindings[*binding_id];
|
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
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ fn rule(name: &str, bases: &[Expr], scope: &Scope, bindings: &Bindings) -> Optio
|
||||||
if !matches!(
|
if !matches!(
|
||||||
scope
|
scope
|
||||||
.get(id.as_str())
|
.get(id.as_str())
|
||||||
.map(|binding_id| &bindings[*binding_id]),
|
.map(|binding_id| &bindings[binding_id]),
|
||||||
None | Some(Binding {
|
None | Some(Binding {
|
||||||
kind: BindingKind::Builtin,
|
kind: BindingKind::Builtin,
|
||||||
..
|
..
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,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(|binding_id| &self.bindings[*binding_id])
|
.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.
|
||||||
|
|
@ -124,7 +124,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
// PEP 563 indicates that if a forward reference can be resolved in the module scope, we
|
// PEP 563 indicates that if a forward reference can be resolved in the module scope, we
|
||||||
// should prefer it over local resolutions.
|
// should prefer it over local resolutions.
|
||||||
if self.in_deferred_type_definition() {
|
if self.in_deferred_type_definition() {
|
||||||
if let Some(binding_id) = self.scopes.global().get(symbol).copied() {
|
if let Some(binding_id) = self.scopes.global().get(symbol) {
|
||||||
// Mark the binding as used.
|
// Mark the binding as used.
|
||||||
let context = self.execution_context();
|
let context = self.execution_context();
|
||||||
let reference_id = self.references.push(ScopeId::global(), range, context);
|
let reference_id = self.references.push(ScopeId::global(), range, context);
|
||||||
|
|
@ -160,7 +160,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(binding_id) = scope.get(symbol).copied() {
|
if let Some(binding_id) = scope.get(symbol) {
|
||||||
// Mark the binding as used.
|
// Mark the binding as used.
|
||||||
let context = self.execution_context();
|
let context = self.execution_context();
|
||||||
let reference_id = self.references.push(self.scope_id, range, context);
|
let reference_id = self.references.push(self.scope_id, range, context);
|
||||||
|
|
@ -255,7 +255,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.scopes[scope_id].get(full_name).copied()
|
self.scopes[scope_id].get(full_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves the [`Expr`] to a fully-qualified symbol-name, if `value` resolves to an imported
|
/// Resolves the [`Expr`] to a fully-qualified symbol-name, if `value` resolves to an imported
|
||||||
|
|
@ -352,7 +352,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
member: &str,
|
member: &str,
|
||||||
) -> Option<(&Stmt, String)> {
|
) -> Option<(&Stmt, String)> {
|
||||||
self.scopes().enumerate().find_map(|(scope_index, scope)| {
|
self.scopes().enumerate().find_map(|(scope_index, scope)| {
|
||||||
scope.binding_ids().copied().find_map(|binding_id| {
|
scope.binding_ids().find_map(|binding_id| {
|
||||||
let binding = &self.bindings[binding_id];
|
let binding = &self.bindings[binding_id];
|
||||||
match &binding.kind {
|
match &binding.kind {
|
||||||
// Ex) Given `module="sys"` and `object="exit"`:
|
// Ex) Given `module="sys"` and `object="exit"`:
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@ impl Reference {
|
||||||
self.range
|
self.range
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn context(&self) -> &ExecutionContext {
|
pub const fn context(&self) -> ExecutionContext {
|
||||||
&self.context
|
self.context
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,8 +45,8 @@ impl<'a> Scope<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the [id](BindingId) of the binding bound to the given name.
|
/// Returns the [id](BindingId) of the binding bound to the given name.
|
||||||
pub fn get(&self, name: &str) -> Option<&BindingId> {
|
pub fn get(&self, name: &str) -> Option<BindingId> {
|
||||||
self.bindings.get(name)
|
self.bindings.get(name).copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a new binding with the given name to this scope.
|
/// Adds a new binding with the given name to this scope.
|
||||||
|
|
@ -70,22 +70,23 @@ impl<'a> Scope<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the ids of all bindings defined in this scope.
|
/// Returns the ids of all bindings defined in this scope.
|
||||||
pub fn binding_ids(&self) -> std::collections::hash_map::Values<&str, BindingId> {
|
pub fn binding_ids(&self) -> impl Iterator<Item = BindingId> + '_ {
|
||||||
self.bindings.values()
|
self.bindings.values().copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a tuple of the name and id of all bindings defined in this scope.
|
/// Returns a tuple of the name and id of all bindings defined in this scope.
|
||||||
pub fn bindings(&self) -> std::collections::hash_map::Iter<&'a str, BindingId> {
|
pub fn bindings(&self) -> impl Iterator<Item = (&str, BindingId)> + '_ {
|
||||||
self.bindings.iter()
|
self.bindings.iter().map(|(&name, &id)| (name, id))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator over all [bindings](BindingId) bound to the given name, including
|
/// Returns an iterator over all [bindings](BindingId) bound to the given name, including
|
||||||
/// those that were shadowed by later bindings.
|
/// those that were shadowed by later bindings.
|
||||||
pub fn bindings_for_name(&self, name: &str) -> impl Iterator<Item = &BindingId> {
|
pub fn bindings_for_name(&self, name: &str) -> impl Iterator<Item = BindingId> + '_ {
|
||||||
self.bindings
|
self.bindings
|
||||||
.get(name)
|
.get(name)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(self.shadowed_bindings.get(name).into_iter().flatten().rev())
|
.chain(self.shadowed_bindings.get(name).into_iter().flatten().rev())
|
||||||
|
.copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a reference to a star import (e.g., `from sys import *`) to this scope.
|
/// Adds a reference to a star import (e.g., `from sys import *`) to this scope.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue