From 62e8ee9403f6e34d1c21afef91c0cfd06c655e87 Mon Sep 17 00:00:00 2001 From: sisantaChhatoi Date: Fri, 24 Jul 2026 02:05:50 +0530 Subject: [PATCH] feat(rules): add macro correctness checks to Rust review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rust.md covered ownership, unsafe, async and atomics but had no coverage of macros — a high-bug-density area of Rust. Add a scoped "Macros and Metaprogramming" section for the well-known macro_rules!/ proc-macro footguns: $expr double-evaluation, missing $crate::, unparenthesized expansion precedence, and proc-macros panicking instead of emitting compile_error!/syn::Error. Guarded to fire only when a macro is defined, preserving the doc's precision-over-recall bias so it does not flag ordinary macro invocations. No wiring changes: **/*.rs already resolves to rust.md and .rs is already in the extension allowlist. --- internal/config/rules/rule_docs/rust.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/config/rules/rule_docs/rust.md b/internal/config/rules/rule_docs/rust.md index e27d8710..022b9b60 100644 --- a/internal/config/rules/rule_docs/rust.md +++ b/internal/config/rules/rule_docs/rust.md @@ -46,6 +46,14 @@ - Public structs, enums, traits, and errors should have useful names, visibility, trait derives, and documentation appropriate to the crate boundary - Avoid exposing concrete collection or synchronization types in public APIs when a slice, iterator, trait, or narrower abstraction would preserve flexibility +#### Macros and Metaprogramming +Only flag when the diff actually defines a `macro_rules!` or procedural macro; do not report on ordinary macro invocations. +- An `$x:expr` fragment interpolated more than once in the expansion, so the caller's expression — and any side effects — runs multiple times; bind it to a `let` once inside the expansion +- Exported or publicly used macros that reference items without `$crate::`, so name resolution breaks or binds the wrong item when the macro is invoked from another crate +- Token-tree (`$t:tt`) fragments re-emitted without parentheses, where operator precedence can silently change the intended meaning; this applies only to token-level (`tt`) interpolation, since an `:expr` fragment and a whole expansion are each parsed as one complete expression +- Procedural macros that `unwrap()`, `expect()`, or `panic!` on malformed input instead of emitting a `syn::Error` / `compile_error!` with a useful span +- Macro-hygiene assumptions that break: generated identifiers relying on names from the call-site scope, or items that collide when the macro is invoked more than once in the same module + #### Security-Sensitive Code - Validate path, URL, command, SQL, and serialized input before use; do not build shell commands or SQL with unchecked string concatenation - Do not log secrets, tokens, credentials, private keys, or personally identifiable information