From c6fe3ac04bcf0ec60887e333075272fbc5eae171 Mon Sep 17 00:00:00 2001 From: FredApps Date: Thu, 19 Nov 2026 17:13:36 +0100 Subject: [PATCH] Detect suite registrations in macro invocations --- .../src/contract/registry_expansion.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/design-lint/src/contract/registry_expansion.rs b/crates/design-lint/src/contract/registry_expansion.rs index 16c40b7f9..ee423f44d 100644 --- a/crates/design-lint/src/contract/registry_expansion.rs +++ b/crates/design-lint/src/contract/registry_expansion.rs @@ -33,6 +33,10 @@ fn has_registration(tokens: TokenStream) -> bool { attrs.push(attr); i += 2; } + if attrs.is_empty() { + i += 1; + continue; + } if attrs.iter().any(|attr| words(attr).first().is_some_and(|word| *word == "story" || *word == "trial")) { return true; } @@ -85,9 +89,7 @@ struct Walk<'a, 'b> { impl<'ast> Visit<'ast> for Walk<'_, '_> { fn visit_item_macro(&mut self, item: &'ast syn::ItemMacro) { - let hidden = if item.mac.path.is_ident("macro_rules") { - has_registration(item.mac.tokens.clone()) - } else if item.mac.path.is_ident("include") { + let hidden = if item.mac.path.is_ident("include") { syn::parse2::(item.mac.tokens.clone()) .ok() .and_then(|path| self.cx.path.parent().map(|parent| parent.join(path.value()))) @@ -95,7 +97,7 @@ impl<'ast> Visit<'ast> for Walk<'_, '_> { .and_then(|source| TokenStream::from_str(&source).ok()) .is_some_and(has_registration) } else { - false + has_registration(item.mac.tokens.clone()) }; if hidden { self.cx.flag_span(self.out, ID, ID, item.span(), "suite registration inside a macro or include is invisible to registry!".to_owned()); @@ -120,6 +122,14 @@ mod tests { assert!(has_registration(item.mac.tokens.clone())); } + #[test] + fn detects_trial_inside_a_proptest_invocation() { + let source = "proptest! { #![proptest_config(ProptestConfig::with_cases(48))] #[trial] fn roundtrip(value in 0u8..4) {} }"; + let file = syn::parse_file(source).expect("valid macro invocation"); + let syn::Item::Macro(item) = &file.items[0] else { panic!("expected macro invocation") }; + assert!(has_registration(item.mac.tokens.clone())); + } + #[test] fn detects_hidden_registrations_and_allows_unregistered_derive() { let hidden = TokenStream::from_str("#[derive(Component)] struct A; #[trial] fn t() {} ").unwrap_or_default();