mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Optimise SemanticModel::match_builtin_expr
(#11001)
This commit is contained in:
parent
2971655b28
commit
caae8d2c68
1 changed files with 13 additions and 2 deletions
|
@ -294,8 +294,19 @@ impl<'a> SemanticModel<'a> {
|
|||
/// or `builtins.object` (where `builtins` is imported as a module at the top level)
|
||||
pub fn match_builtin_expr(&self, expr: &Expr, symbol: &str) -> bool {
|
||||
debug_assert!(!symbol.contains('.'));
|
||||
self.resolve_builtin_symbol(expr)
|
||||
.is_some_and(|name| name == symbol)
|
||||
// fast path with more short-circuiting
|
||||
if !self.seen_module(Modules::BUILTINS) {
|
||||
let Expr::Name(ast::ExprName { id, .. }) = expr else {
|
||||
return false;
|
||||
};
|
||||
return id == symbol && self.is_builtin(symbol);
|
||||
}
|
||||
|
||||
// slow path: we need to consider attribute accesses and aliased imports
|
||||
let Some(qualified_name) = self.resolve_qualified_name(expr) else {
|
||||
return false;
|
||||
};
|
||||
matches!(qualified_name.segments(), ["" | "builtins", name] if *name == symbol)
|
||||
}
|
||||
|
||||
/// Return `true` if `member` is an "available" symbol, i.e., a symbol that has not been bound
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue