From 21d9182ba2924cf9cc555a201e661d2ea474eed9 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 13 Feb 2022 21:43:45 +0800 Subject: [PATCH] nac3core: disallow methods/fields in Exception subclass Fixes #192 --- nac3core/src/toplevel/composer.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index c3adddd4..2357e9aa 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -588,8 +588,10 @@ impl TopLevelComposer { if class_ancestors.iter().any(|ann| matches!(ann, TypeAnnotation::CustomClass { id, .. } if id.0 == 7)) { // if inherited from Exception, the body should be a pass if let ast::StmtKind::ClassDef { body, .. } = &class_ast.as_ref().unwrap().node { - if body.len() != 1 || !matches!(body[0].node, ast::StmtKind::Pass { .. }) { - return Err("Classes inherited from exception should have `pass` as body".into()); + for stmt in body.iter() { + if matches!(stmt.node, ast::StmtKind::FunctionDef { .. } | ast::StmtKind::AnnAssign { .. }) { + return Err("Classes inherited from exception should have no custom fields/methods".into()); + } } } else { unreachable!()