-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn about unused macro arms. #73576
Comments
I personally think that we should not warn for unused macro arms by default, as I tend to fairly often write the following: macro_rules! used_macro {
($($e:expr),*) => {};
($($e:expr),*,) => {}; // <-- This is unused.
}
fn main() {
used_macro!(7, 13);
} To not have to worry about this in case I ever change a macro invocation to use_macro!(
7,
13,
); |
@lcnr This is convenient indeed :) But I don't think it should be blocking, for several reasons:
macro_rules! used_macro {
($($e:expr),*$(,)?) => {}; // Only one used arm.
}
fn main() {
used_macro!(7, 13); // Without the comma.
used_macro!(
7,
13, // With the comma.
);
}
In any case, I think that this is a good point, but also that it does not hinder the need for a warning about unused macro arms. |
One concern I have is that it's not possible to cfg-gate single macro arms, which would be needed in situations I've outlined in this comment: #34938 (comment) |
@est31 #[allow(unused_macro_arms)]
macro foo {
used_arm => {},
conditionally_used_arm => {}
} Not perfect, but still a pretty good approximation. |
Good point. Concern resolved! Would love to see this implemented. Begone, unused stuff! |
@petrochenkov Is this finer-grained alternate macro foo {
used_arm => {},
#[allow(unused_macro_arm)]
conditionally_used_arm => {}
} |
@iago-lito |
You'd likely require ids to implement the lint in the first place, so at least that cost has to be paid already. At least that's how I did it in #41907 (with a set of ids of unused macros), idk how much it has changed since then. |
For uniquely identifying the arm having only the macro's NodeId + the arm's index should be enough, then this pair can be used to track which arms are used and which are not. For applying |
The lint might also be useful from a performance point of view, as each unused arm causes additional compile time. According to this blog post, there is sometimes a lot of compile time spent in expansion, it also reports about PRs that have reordered match arms and removed unused ones to increase whole-crate compile times by double digit percentages. |
I'm wondering about the name for the lint. I couldn't find the term "arm" used anywhere in official documentation to refer to macro matchers, like the reference. Instead I found the term "rule", while Per that, the name of the lint should be I'm thinking about |
I've filed a PR here: #96150 . It uses the |
Implement a lint to warn about unused macro rules This implements a new lint to warn about unused macro rules (arms/matchers), similar to the `unused_macros` lint added by rust-lang#41907 that warns about entire macros. ```rust macro_rules! unused_empty { (hello) => { println!("Hello, world!") }; () => { println!("empty") }; //~ ERROR: 1st rule of macro `unused_empty` is never used } fn main() { unused_empty!(hello); } ``` Builds upon ~~(and currently integrates)~~ rust-lang#96149 and rust-lang#96156. Fixes rust-lang#73576
…enkov Implement a lint to warn about unused macro rules This implements a new lint to warn about unused macro rules (arms/matchers), similar to the `unused_macros` lint added by rust-lang#41907 that warns about entire macros. ```rust macro_rules! unused_empty { (hello) => { println!("Hello, world!") }; () => { println!("empty") }; //~ ERROR: 1st rule of macro `unused_empty` is never used } fn main() { unused_empty!(hello); } ``` Builds upon rust-lang#96149 and rust-lang#96156. Fixes rust-lang#73576
…enkov Implement a lint to warn about unused macro rules This implements a new lint to warn about unused macro rules (arms/matchers), similar to the `unused_macros` lint added by rust-lang#41907 that warns about entire macros. ```rust macro_rules! unused_empty { (hello) => { println!("Hello, world!") }; () => { println!("empty") }; //~ ERROR: 1st rule of macro `unused_empty` is never used } fn main() { unused_empty!(hello); } ``` Builds upon rust-lang#96149 and rust-lang#96156. Fixes rust-lang#73576
827: Remove unused macro rules r=taiki-e a=alygin The [new `unused_macro_rules` lint](rust-lang/rust#73576) that recently landed in nightly [breaks](https://github.com/crossbeam-rs/crossbeam/actions/runs/2316949414) CI builds. This fix removes unused macro arms in tests. Co-authored-by: Andrew Lygin <[email protected]>
It accepts single comma as an input. There is better way: |
rustc
now warns about unused macros. With the following code:we get the warning:
This is really helpful while cleaning up a large meta-programming mess.
However, it does not warn about unused macro arms, so the following:
yields no warning yet.
Following #34938, I open this issue for discussing this feature. Is this something desirable to have
rustc
warn about unused macro arms?The text was updated successfully, but these errors were encountered: