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:
@@ -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.
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user