From 777e136f4cd811bec0c5dabd47e1ae4285f25ca2 Mon Sep 17 00:00:00 2001 From: est31 Date: Thu, 9 Jun 2022 23:34:06 +0200 Subject: [PATCH] Suppress the unused_macro_rules lint if malformed rules are encountered Prior to this commit, if a macro had any malformed rules, all rules would be reported as unused, regardless of whether they were used or not. So we just turn off unused rule checking completely for macros with malformed rules. --- compiler/rustc_expand/src/mbe/macro_rules.rs | 10 ++++++---- .../lint/unused/unused-macro-rules-malformed-rule.rs | 11 +++++++++++ .../unused/unused-macro-rules-malformed-rule.stderr | 8 ++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/lint/unused/unused-macro-rules-malformed-rule.rs create mode 100644 src/test/ui/lint/unused/unused-macro-rules-malformed-rule.stderr diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs index 2a767646eafe..04af1eaf67b6 100644 --- a/compiler/rustc_expand/src/mbe/macro_rules.rs +++ b/compiler/rustc_expand/src/mbe/macro_rules.rs @@ -539,11 +539,11 @@ pub fn compile_declarative_macro( None => {} } - // Compute the spans of the macro rules - // We only take the span of the lhs here, - // so that the spans of created warnings are smaller. + // Compute the spans of the macro rules for unused rule linting. + // To avoid warning noise, only consider the rules of this + // macro for the lint, if all rules are valid. // Also, we are only interested in non-foreign macros. - let rule_spans = if def.id != DUMMY_NODE_ID { + let rule_spans = if valid && def.id != DUMMY_NODE_ID { lhses .iter() .zip(rhses.iter()) @@ -551,6 +551,8 @@ pub fn compile_declarative_macro( // If the rhs contains an invocation like compile_error!, // don't consider the rule for the unused rule lint. .filter(|(_idx, (_lhs, rhs))| !has_compile_error_macro(rhs)) + // We only take the span of the lhs here, + // so that the spans of created warnings are smaller. .map(|(idx, (lhs, _rhs))| (idx, lhs.span())) .collect::>() } else { diff --git a/src/test/ui/lint/unused/unused-macro-rules-malformed-rule.rs b/src/test/ui/lint/unused/unused-macro-rules-malformed-rule.rs new file mode 100644 index 000000000000..a826026ec405 --- /dev/null +++ b/src/test/ui/lint/unused/unused-macro-rules-malformed-rule.rs @@ -0,0 +1,11 @@ +#![deny(unused_macro_rules)] + +macro_rules! foo { + (v) => {}; + (w) => {}; + () => 0; //~ ERROR: macro rhs must be delimited +} + +fn main() { + foo!(v); +} diff --git a/src/test/ui/lint/unused/unused-macro-rules-malformed-rule.stderr b/src/test/ui/lint/unused/unused-macro-rules-malformed-rule.stderr new file mode 100644 index 000000000000..797c867103f0 --- /dev/null +++ b/src/test/ui/lint/unused/unused-macro-rules-malformed-rule.stderr @@ -0,0 +1,8 @@ +error: macro rhs must be delimited + --> $DIR/unused-macro-rules-malformed-rule.rs:6:11 + | +LL | () => 0; + | ^ + +error: aborting due to previous error +