Skip to content

Commit 777e136

Browse files
committed
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.
1 parent eb3c611 commit 777e136

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -539,18 +539,20 @@ pub fn compile_declarative_macro(
539539
None => {}
540540
}
541541

542-
// Compute the spans of the macro rules
543-
// We only take the span of the lhs here,
544-
// so that the spans of created warnings are smaller.
542+
// Compute the spans of the macro rules for unused rule linting.
543+
// To avoid warning noise, only consider the rules of this
544+
// macro for the lint, if all rules are valid.
545545
// Also, we are only interested in non-foreign macros.
546-
let rule_spans = if def.id != DUMMY_NODE_ID {
546+
let rule_spans = if valid && def.id != DUMMY_NODE_ID {
547547
lhses
548548
.iter()
549549
.zip(rhses.iter())
550550
.enumerate()
551551
// If the rhs contains an invocation like compile_error!,
552552
// don't consider the rule for the unused rule lint.
553553
.filter(|(_idx, (_lhs, rhs))| !has_compile_error_macro(rhs))
554+
// We only take the span of the lhs here,
555+
// so that the spans of created warnings are smaller.
554556
.map(|(idx, (lhs, _rhs))| (idx, lhs.span()))
555557
.collect::<Vec<_>>()
556558
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![deny(unused_macro_rules)]
2+
3+
macro_rules! foo {
4+
(v) => {};
5+
(w) => {};
6+
() => 0; //~ ERROR: macro rhs must be delimited
7+
}
8+
9+
fn main() {
10+
foo!(v);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: macro rhs must be delimited
2+
--> $DIR/unused-macro-rules-malformed-rule.rs:6:11
3+
|
4+
LL | () => 0;
5+
| ^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)