about summary refs log tree commit diff
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2022-06-09 23:34:06 +0200
committerest31 <MTest31@outlook.com>2022-06-09 23:34:06 +0200
commit777e136f4cd811bec0c5dabd47e1ae4285f25ca2 (patch)
treea1b8b12186959d8ae8a472bd8c9826219d31530d
parenteb3c611e1d3829e05aec8f9887efa164ea2c6da5 (diff)
downloadrust-777e136f4cd811bec0c5dabd47e1ae4285f25ca2.tar.gz
rust-777e136f4cd811bec0c5dabd47e1ae4285f25ca2.zip
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.
-rw-r--r--compiler/rustc_expand/src/mbe/macro_rules.rs10
-rw-r--r--src/test/ui/lint/unused/unused-macro-rules-malformed-rule.rs11
-rw-r--r--src/test/ui/lint/unused/unused-macro-rules-malformed-rule.stderr8
3 files changed, 25 insertions, 4 deletions
diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs
index 2a767646eaf..04af1eaf67b 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::<Vec<_>>()
     } 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 00000000000..a826026ec40
--- /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 00000000000..797c867103f
--- /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
+