forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#97903 - est31:unused_macro_rules_compile_erro…
…r, r=petrochenkov Never regard macro rules with compile_error! invocations as unused The very point of compile_error! is to never be reached, and one of the use cases of the macro, currently also listed as examples in the documentation of compile_error, is to create nicer errors for wrong macro invocations. Thus, we should never warn about unused macro arms that contain invocations of compile_error. See also rust-lang#96150 (comment) and the discussion after that. Furthermore, the PR also contains two commits to silence `unused_macro_rules` when a macro has an invalid rule, and to add a test that `unused_macros` does not behave badly in the same situation. r? `@petrochenkov` as I've talked to them about this
- Loading branch information
Showing
10 changed files
with
158 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/ui/lint/unused/unused-macro-rules-compile-error.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#![deny(unused_macro_rules)] | ||
// To make sure we are not hitting this | ||
#![deny(unused_macros)] | ||
|
||
macro_rules! num { | ||
(one) => { 1 }; | ||
// Most simple (and common) case | ||
(two) => { compile_error!("foo"); }; | ||
// Some nested use | ||
(two_) => { foo(compile_error!("foo")); }; | ||
(three) => { 3 }; | ||
(four) => { 4 }; //~ ERROR: rule of macro | ||
} | ||
const _NUM: u8 = num!(one) + num!(three); | ||
|
||
// compile_error not used as a macro invocation | ||
macro_rules! num2 { | ||
(one) => { 1 }; | ||
// Only identifier present | ||
(two) => { fn compile_error() {} }; //~ ERROR: rule of macro | ||
// Only identifier and bang present | ||
(two_) => { compile_error! }; //~ ERROR: rule of macro | ||
(three) => { 3 }; | ||
} | ||
const _NUM2: u8 = num2!(one) + num2!(three); | ||
|
||
fn main() {} |
26 changes: 26 additions & 0 deletions
26
src/test/ui/lint/unused/unused-macro-rules-compile-error.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error: 5th rule of macro `num` is never used | ||
--> $DIR/unused-macro-rules-compile-error.rs:12:5 | ||
| | ||
LL | (four) => { 4 }; | ||
| ^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/unused-macro-rules-compile-error.rs:1:9 | ||
| | ||
LL | #![deny(unused_macro_rules)] | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
||
error: 3rd rule of macro `num2` is never used | ||
--> $DIR/unused-macro-rules-compile-error.rs:22:5 | ||
| | ||
LL | (two_) => { compile_error! }; | ||
| ^^^^^^ | ||
|
||
error: 2nd rule of macro `num2` is never used | ||
--> $DIR/unused-macro-rules-compile-error.rs:20:5 | ||
| | ||
LL | (two) => { fn compile_error() {} }; | ||
| ^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
11 changes: 11 additions & 0 deletions
11
src/test/ui/lint/unused/unused-macro-rules-malformed-rule.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![deny(unused_macro_rules)] | ||
|
||
macro_rules! foo { | ||
(v) => {}; | ||
(w) => {}; | ||
() => 0; //~ ERROR: macro rhs must be delimited | ||
} | ||
|
||
fn main() { | ||
foo!(v); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/test/ui/lint/unused/unused-macro-rules-malformed-rule.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#![deny(unused_macros)] | ||
|
||
macro_rules! foo { //~ ERROR: unused macro definition | ||
(v) => {}; | ||
() => 0; //~ ERROR: macro rhs must be delimited | ||
} | ||
|
||
macro_rules! bar { | ||
(v) => {}; | ||
() => 0; //~ ERROR: macro rhs must be delimited | ||
} | ||
|
||
fn main() { | ||
bar!(v); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/test/ui/lint/unused/unused-macros-malformed-rule.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error: macro rhs must be delimited | ||
--> $DIR/unused-macros-malformed-rule.rs:5:11 | ||
| | ||
LL | () => 0; | ||
| ^ | ||
|
||
error: macro rhs must be delimited | ||
--> $DIR/unused-macros-malformed-rule.rs:10:11 | ||
| | ||
LL | () => 0; | ||
| ^ | ||
|
||
error: unused macro definition: `foo` | ||
--> $DIR/unused-macros-malformed-rule.rs:3:14 | ||
| | ||
LL | macro_rules! foo { | ||
| ^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/unused-macros-malformed-rule.rs:1:9 | ||
| | ||
LL | #![deny(unused_macros)] | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|