From a8f4faa6e41b227d36d5428246706fb68e8b0ec5 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 7 Sep 2022 22:40:25 -0400 Subject: [PATCH] Fix crash on missing parent --- src/check_ast.rs | 24 +++++++++++++++--------- src/linter.rs | 1 + 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/check_ast.rs b/src/check_ast.rs index 868966d739f2c..4bc0457ce4f69 100644 --- a/src/check_ast.rs +++ b/src/check_ast.rs @@ -500,9 +500,11 @@ where ExprKind::Name { ctx, .. } => match ctx { ExprContext::Load => self.handle_node_load(expr), ExprContext::Store => { - let parent = self.parents.pop().expect("No parnet statement found."); - self.handle_node_store(expr, Some(parent)); - self.parents.push(parent); + let parent = self.parents.pop(); + self.handle_node_store(expr, parent); + if let Some(parent) = parent { + self.parents.push(parent); + } } ExprContext::Del => self.handle_node_delete(expr), }, @@ -772,7 +774,7 @@ where let scope = &self.scopes[*(self.scope_stack.last().expect("No current scope found."))]; if scope.values.contains_key(name) { - let parent = self.parents.pop().expect("No parnet statement found."); + let parent = self.parents.pop(); self.handle_node_store( &Expr::new( excepthandler.location, @@ -781,12 +783,14 @@ where ctx: ExprContext::Store, }, ), - Some(parent), + parent, ); - self.parents.push(parent); + if let Some(parent) = parent { + self.parents.push(parent); + } } - let parent = self.parents.pop().expect("No parnet statement found."); + let parent = self.parents.pop(); let scope = &self.scopes[*(self.scope_stack.last().expect("No current scope found."))]; let definition = scope.values.get(name).cloned(); @@ -798,9 +802,11 @@ where ctx: ExprContext::Store, }, ), - Some(parent), + parent, ); - self.parents.push(parent); + if let Some(parent) = parent { + self.parents.push(parent); + } walk_excepthandler(self, excepthandler); diff --git a/src/linter.rs b/src/linter.rs index eb2070a879605..d8f4906040168 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -13,6 +13,7 @@ use crate::settings::Settings; use crate::{autofix, cache, fs}; fn check_path(path: &Path, settings: &Settings, autofix: &autofix::Mode) -> Result> { + println!("{:?}", path); // Read the file from disk. let contents = fs::read_file(path)?;