mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Add dedicated method to find typed binding (#8517)
## Summary We have this pattern in a bunch of places, where we find the _only_ binding to a name (and return `None`) if it's bound multiple times. This PR DRYs it up into a method on `SemanticModel`.
This commit is contained in:
parent
5b3e922050
commit
eab8ca4d7e
10 changed files with 82 additions and 79 deletions
|
@ -616,6 +616,16 @@ impl<'a> SemanticModel<'a> {
|
|||
self.resolved_names.get(&name.into()).copied()
|
||||
}
|
||||
|
||||
/// Resolves the [`ast::ExprName`] to the [`BindingId`] of the symbol it refers to, if it's the
|
||||
/// only binding to that name in its scope.
|
||||
pub fn only_binding(&self, name: &ast::ExprName) -> Option<BindingId> {
|
||||
self.resolve_name(name).filter(|id| {
|
||||
let binding = self.binding(*id);
|
||||
let scope = &self.scopes[binding.scope];
|
||||
scope.shadowed_binding(*id).is_none()
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolves the [`Expr`] to a fully-qualified symbol-name, if `value` resolves to an imported
|
||||
/// or builtin symbol.
|
||||
///
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue