From 2475a2b7254274cf75444d0cf4587b023d31bfb4 Mon Sep 17 00:00:00 2001 From: mulhern Date: Mon, 9 Sep 2024 10:38:50 -0400 Subject: [PATCH] Panic if more than one attribute found In stratisd, we always expect exactly one. If that changes, we'll probably have to enrich this method somehow. Signed-off-by: mulhern --- stratisd_proc_macros/src/lib.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/stratisd_proc_macros/src/lib.rs b/stratisd_proc_macros/src/lib.rs index 31c831a5f3..700605f5c6 100644 --- a/stratisd_proc_macros/src/lib.rs +++ b/stratisd_proc_macros/src/lib.rs @@ -228,16 +228,23 @@ fn get_attr_level(attrs: &mut Vec) -> Option { /// Determine whether a method has the given attribute. fn has_attribute(attrs: &mut Vec, attribute: &str) -> bool { - if let Some((index, _)) = attrs + let matching_attrs = attrs .iter() .enumerate() .filter(|(_, a)| a.path().is_ident(attribute)) - .last() - { - attrs.remove(index); - true - } else { - false + .collect::>(); + + match matching_attrs.len() { + 0 => false, + 1 => { + let (index, _) = matching_attrs[0]; + attrs.remove(index); + true + } + _ => panic!( + "More than 1 {} attribute specified for the same syntactic entity.", + attribute + ), } }