From 90d7243bf5ff12dd76796157f7659b57c2fbf490 Mon Sep 17 00:00:00 2001 From: z78078 Date: Fri, 8 Jul 2022 14:24:05 +0800 Subject: [PATCH] nac3artiq check if constructor __init__ use rpc decorator --- nac3artiq/src/lib.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index a7a07b75..80371702 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -393,21 +393,21 @@ impl Nac3 { let class_obj = module.getattr(py, name.to_string()).unwrap(); for stmt in body.iter() { if let StmtKind::FunctionDef { name, decorator_list, .. } = &stmt.node { - // checking: constructor __init__ must not be decorated with rpc - if name == &"__init__".into() { - for decorator in decorator_list.iter() { - if let ast::Located { location, node: ExprKind::Name { id, .. }, .. } = decorator { - if id == &"rpc".into() { - return Err(CompileError::new_err(format!( - "compilation failed\n----------\nConstructor __init__ function should not decorated with rpc decorator (at {})", - location - ))); - } + if let Some(location) = decorator_list + .iter() + .find_map(|ast::Located { location, node, .. } | { + match node { + ExprKind::Name { id, .. } if id == &"rpc".into() => Some(location), + _ => None, } + }) + { + if name == &"__init__".into() { + return Err(CompileError::new_err(format!( + "compilation failed\n----------\nConstructor __init__ function should not decorated with rpc decorator (at {})", + location + ))); } - } - - if decorator_list.iter().any(|decorator| matches!(decorator.node, ExprKind::Name { id, .. } if id == "rpc".into())) { rpc_ids.push((Some((class_obj.clone(), *name)), def_id)); } }