macros-code-review
Original:🇺🇸 English
Translated
Reviews Rust macro code for hygiene issues, fragment misuse, compile-time impact, and procedural macro patterns. Use when reviewing macro_rules! definitions, procedural macros, derive macros, or attribute macros.
7installs
Sourceexistential-birds/beagle
Added on
NPX Install
npx skill4agent add existential-birds/beagle macros-code-reviewTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Macros Code Review
Review Workflow
- Check -- Note Rust edition (2024 reserves
Cargo.tomlkeyword, affecting macro output), proc-macro crate dependencies (gen,syn,quote), and feature flags (e.g.,proc-macro2with minimal features)syn - Check macro type -- Determine if reviewing declarative (), function-like proc macro, attribute macro, or derive macro
macro_rules! - Check if a macro is needed -- If the transformation is type-based, generics are better. Macros are for structural/repetitive code generation that generics cannot express
- Scan macro definitions -- Read full macro bodies including all match arms, not just the invocation site
- Check each category -- Work through the checklist below, loading references as needed
- Verify before reporting -- Load before submitting findings
beagle-rust:review-verification-protocol
Output Format
Report findings as:
text
[FILE:LINE] ISSUE_TITLE
Severity: Critical | Major | Minor | Informational
Description of the issue and why it matters.Quick Reference
| Issue Type | Reference |
|---|---|
| Fragment types, repetition, hygiene, declarative patterns | references/declarative-macros.md |
| Proc macro types, syn/quote, spans, testing | references/procedural-macros.md |
Review Checklist
Declarative Macros (macro_rules!
)
macro_rules!- Correct fragment types used (vs
:exprvs:tt-- wrong choice causes unexpected parsing):ident - Repetition separators match intended syntax (vs
,vs none,;vs*)+ - Trailing comma/semicolon handled (add or
$(,)?at end of repetition)$(;)? - Matchers ordered from most specific to least specific (first match wins)
- No ambiguous expansions -- each metavariable appears in the correct repetition depth in the transcriber
- Variables defined in the macro use macro-internal names (hygiene protects variables, not types/modules/functions)
- Exported macros () use
#[macro_export]for crate-internal paths, never$crate::orcrate::self:: - Standard library paths use and
::core::(not::alloc::) for::std::compatibilityno_std - used for meaningful error messages on invalid input patterns
compile_error! - Macro placement respects textual scoping (defined before use) unless
#[macro_export]
Procedural Macros
- features minimized (don't enable
synwhenfullsuffices -- reduces compile time)derive - Spans propagated from input tokens to output tokens (errors point to user code, not macro internals)
- used for identifiers that should be visible to the caller
Span::call_site() - used for identifiers private to the macro (matches
Span::mixed_site()hygiene)macro_rules! - Error reporting uses with proper spans, not
syn::Errorpanic! - Multiple errors collected and reported together via
syn::Error::combine - used for testing (testable outside of compiler context)
proc-macro2 - Generated code volume is proportionate -- proc macros that emit large amounts of code bloat compile times
Derive Macros
- Derivation is obvious -- a developer could guess what it does from the trait name alone
- Helper attributes (style) are documented
#[serde(skip)] - Trait implementation is correct for all variant shapes (unit, tuple, struct variants)
- Generated blocks use fully qualified paths (
impl,::core::)$crate::
Attribute Macros
- Input item is preserved or intentionally transformed (not accidentally dropped)
- Attribute arguments are validated with clear error messages
- Test generation patterns (style) produce unique test names
#[test_case] - Framework annotations document what code they generate
Edition 2024 Awareness
- Macro output does not use as an identifier (reserved keyword -- use
genor rename)r#gen - Generated bodies use explicit
unsafe fnblocks around unsafe opsunsafe {} - Generated blocks use
externunsafe extern
Generics vs Macros
Flag a macro when the same result is achievable with generics or trait bounds. Macros are appropriate when:
- The generated code varies structurally (not just by type)
- Repetitive trait impls for many concrete types
- Test batteries with configuration variants
- Compile-time computation that cannot express
const fn
Severity Calibration
Critical (Block Merge)
- Macro generates unsound code
unsafe - Hygiene violation in macro that outputs blocks (caller's variables leak into unsafe context)
unsafe - Proc macro panics instead of returning (crashes the compiler)
compile_error! - Derive macro generates incorrect trait implementation (violates trait contract)
Major (Should Fix)
- Exported macro uses or
crate::instead ofself::(breaks for downstream users)$crate:: - Exported macro uses instead of
::std::/::core::(breaks::alloc::users)no_std - Wrong fragment type causing unexpected parsing (where
:exprneeded, or vice versa):tt - Proc macro enables full features unnecessarily (compile time cost)
syn - Missing span propagation (errors point to macro definition, not invocation)
- No error handling in proc macro (panics on bad input instead of )
compile_error!
Minor (Consider Fixing)
- Missing trailing comma/semicolon tolerance in repetition patterns
- Matcher arms not ordered most-specific-first
- Macro used where generics would be clearer and equally expressive
- Missing fallback arm for invalid patterns
compile_error! - Helper attributes undocumented
Informational (Note Only)
- Suggestions to split complex into a proc macro
macro_rules! - Suggestions to reduce generated code volume
- TT munching or push-down accumulation patterns that could be simplified
Valid Patterns (Do NOT Flag)
- for test batteries -- Generating repetitive test modules from a list of types/configs
macro_rules! - for trait impls -- Implementing a trait for many concrete types with identical bodies
macro_rules! - TT munching -- Valid advanced pattern for recursive token processing
- Push-down accumulation -- Valid pattern for building output incrementally across recursive calls
- with
#[macro_export]-- Correct way to make macros usable outside the defining crate$crate - for generated functions -- Intentionally making generated items visible to callers
Span::call_site() - -- Correct error reporting pattern in proc macros
syn::Error::to_compile_error() - tests for proc macros -- Standard compile-fail testing approach
trybuild - Attribute macros on test functions -- Common pattern for test setup/teardown
- in impossible match arms -- Good practice for catching invalid macro input
compile_error!
Before Submitting Findings
Load and follow before reporting any issue.
beagle-rust:review-verification-protocol