Move some accesses behind a shared function

This commit is contained in:
Charlie Marsh 2022-10-05 14:54:31 -04:00
parent 3d5bc1f51f
commit b307afc00c
1 changed files with 12 additions and 20 deletions

View File

@ -219,8 +219,7 @@ where
StmtKind::Global { names } | StmtKind::Nonlocal { names } => { StmtKind::Global { names } | StmtKind::Nonlocal { names } => {
let global_scope_id = self.scopes[GLOBAL_SCOPE_INDEX].id; let global_scope_id = self.scopes[GLOBAL_SCOPE_INDEX].id;
let current_scope = let current_scope = self.current_scope();
&self.scopes[*(self.scope_stack.last().expect("No current scope found."))];
let current_scope_id = current_scope.id; let current_scope_id = current_scope.id;
if current_scope_id != global_scope_id { if current_scope_id != global_scope_id {
for name in names { for name in names {
@ -724,9 +723,7 @@ where
self.check_builtin_shadowing(id, Range::from_located(expr), true); self.check_builtin_shadowing(id, Range::from_located(expr), true);
let parent = self.handle_node_store(expr, self.current_parent());
self.parents[*(self.parent_stack.last().expect("No parent found."))];
self.handle_node_store(expr, parent);
} }
ExprContext::Del => self.handle_node_delete(expr), ExprContext::Del => self.handle_node_delete(expr),
}, },
@ -775,8 +772,7 @@ where
} }
} }
ExprKind::Yield { .. } | ExprKind::YieldFrom { .. } | ExprKind::Await { .. } => { ExprKind::Yield { .. } | ExprKind::YieldFrom { .. } | ExprKind::Await { .. } => {
let scope = let scope = self.current_scope();
&self.scopes[*(self.scope_stack.last().expect("No current scope found."))];
if self if self
.settings .settings
.enabled .enabled
@ -1072,11 +1068,7 @@ where
false, false,
); );
let scope = &self.scopes if self.current_scope().values.contains_key(name) {
[*(self.scope_stack.last().expect("No current scope found."))];
if scope.values.contains_key(name) {
let parent = self.parents
[*(self.parent_stack.last().expect("No parent found."))];
self.handle_node_store( self.handle_node_store(
&Expr::new( &Expr::new(
excepthandler.location, excepthandler.location,
@ -1086,15 +1078,11 @@ where
ctx: ExprContext::Store, ctx: ExprContext::Store,
}, },
), ),
parent, self.current_parent(),
); );
} }
let parent = let definition = self.current_scope().values.get(name).cloned();
self.parents[*(self.parent_stack.last().expect("No parent found."))];
let scope = &self.scopes
[*(self.scope_stack.last().expect("No current scope found."))];
let definition = scope.values.get(name).cloned();
self.handle_node_store( self.handle_node_store(
&Expr::new( &Expr::new(
excepthandler.location, excepthandler.location,
@ -1104,7 +1092,7 @@ where
ctx: ExprContext::Store, ctx: ExprContext::Store,
}, },
), ),
parent, self.current_parent(),
); );
walk_excepthandler(self, excepthandler); walk_excepthandler(self, excepthandler);
@ -1244,6 +1232,10 @@ impl<'a> Checker<'a> {
&self.scopes[*(self.scope_stack.last().expect("No current scope found."))] &self.scopes[*(self.scope_stack.last().expect("No current scope found."))]
} }
pub fn current_parent(&self) -> &'a Stmt {
self.parents[*(self.parent_stack.last().expect("No parent found."))]
}
pub fn binding_context(&self) -> BindingContext { pub fn binding_context(&self) -> BindingContext {
let mut rev = self.parent_stack.iter().rev().fuse(); let mut rev = self.parent_stack.iter().rev().fuse();
let defined_by = *rev.next().expect("Expected to bind within a statement."); let defined_by = *rev.next().expect("Expected to bind within a statement.");
@ -1682,7 +1674,7 @@ impl<'a> Checker<'a> {
} }
fn check_builtin_shadowing(&mut self, name: &str, location: Range, is_attribute: bool) { fn check_builtin_shadowing(&mut self, name: &str, location: Range, is_attribute: bool) {
let scope = &self.scopes[*(self.scope_stack.last().expect("No current scope found."))]; let scope = self.current_scope();
// flake8-builtins // flake8-builtins
if is_attribute && matches!(scope.kind, ScopeKind::Class) { if is_attribute && matches!(scope.kind, ScopeKind::Class) {