-
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
Add lint for unused macros #41907
Add lint for unused macros #41907
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
r? @jseyfried because you seem to have done many things with macros in the past, so might know the code best. |
Hmmm, I suspect that the lint checker requires something to be present under the NodeId we give it, so we can't just bail out by creating some random one, but I guess we'll need to create our own special |
Okay, apparently macro definitions do survive inside the AST (but not in the hir), only have to add handling for them in the lint code. Update incoming. I've also found some occurences of unused macros inside the codebase, |
0c72cd2
to
a97a1c5
Compare
Remove unused macros from the codebase Thanks to the lint I've implemented in rust-lang#41907 I've found some unused macros inside the rustc codebase.
Remove unused macros from the codebase Thanks to the lint I've implemented in rust-lang#41907 I've found some unused macros inside the rustc codebase.
Remove unused macros from the codebase Thanks to the lint I've implemented in rust-lang#41907 I've found some unused macros inside the rustc codebase.
Remove unused macros from the codebase Thanks to the lint I've implemented in rust-lang#41907 I've found some unused macros inside the rustc codebase.
If @jseyfried doesn't appear, then r=me after #41934 is merged. |
This commit extends the current unused macro linter to support directives like #[allow(unused_macros)] or #[deny(unused_macros)] directly next to the macro definition, or in one of the modules the macro is inside. Before, we only supported such directives at a per crate level, due to the crate's NodeId being passed to session.add_lint. We also had to implement handling of the macro's NodeId in the lint visitor.
okay, ready for review. r? @jseyfried |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! r=me modulo comments
src/librustc_resolve/macros.rs
Outdated
let id_span = match *self.macro_map[did] { | ||
SyntaxExtension::NormalTT(_, isp, _) => isp, | ||
_ => None | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this is usually formatted
let id_span = match *self.macro_map[did] {
SyntaxExtension::NormalTT(_, isp, _) => isp,
_ => None
};
@@ -12,6 +12,7 @@ use super::Wrapping; | |||
|
|||
use ops::*; | |||
|
|||
#[allow(unused_macros)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we can't just remove the unused macro here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a FIXME below to get the remaining impls uncommented, including invocations of the macro.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
src/libsyntax/ext/tt/macro_rules.rs
Outdated
exp, | ||
Some((def.id, def.span)), | ||
attr::contains_name(&def.attrs, "allow_internal_unstable") | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: nonstandard formatting -- should be three lines with "visual indenting" or indented one block with four/five lines.
src/librustc_resolve/lib.rs
Outdated
// List of macros that we need to warn about as being unused. | ||
// The bool is true if the macro is unused, and false if its used. | ||
// Setting a bool to false should be much faster than removing a single | ||
// element from a FxHashSet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you clarify that this is only for crate-local macro_rules!
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, but hopefully the upcoming macros 2.0 macros could be included as well.
src/librustc_resolve/lib.rs
Outdated
// The bool is true if the macro is unused, and false if its used. | ||
// Setting a bool to false should be much faster than removing a single | ||
// element from a FxHashSet. | ||
unused_macros: FxHashMap<DefId, bool>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think this should be an FxHashSet<DefId>
-- I believe the perf difference is negligible here (removing a value from a hash set is already many orders of magnitude faster than the rest the processing we do per used crate-local macro_rules!
).
Add lint for unused macros Addresses parts of rust-lang#34938, to add a lint for unused macros. We now output warnings by default when we encounter a macro that we didn't use for expansion. Issues to be resolved before this PR is ready for merge: - [x] fix the NodeId issue described above - [x] remove all unused macros from rustc and the libraries or set `#[allow(unused_macros)]` next to them if they should be kept for some reason. This is needed for successful boostrap and bors to accept the PR. -> rust-lang#41934 - [x] ~~implement the full extent of rust-lang#34938, that means the macro match arm checking as well.~~ *let's not do this for now*
⌛ Testing commit 6dbd706 with merge 9ccc84d... |
💔 Test failed - status-travis |
I think this was recently fixed, but the update to the cargo submodule hasn't been pulled in yet (if the PR was even merged) @bors retry |
FWIW that was fixed by rust-lang/cargo#4051 and we'll get the update in #42039 |
Add lint for unused macros Addresses parts of #34938, to add a lint for unused macros. We now output warnings by default when we encounter a macro that we didn't use for expansion. Issues to be resolved before this PR is ready for merge: - [x] fix the NodeId issue described above - [x] remove all unused macros from rustc and the libraries or set `#[allow(unused_macros)]` next to them if they should be kept for some reason. This is needed for successful boostrap and bors to accept the PR. -> #41934 - [x] ~~implement the full extent of #34938, that means the macro match arm checking as well.~~ *let's not do this for now*
☀️ Test successful - status-appveyor, status-travis |
Extend the unused macro lint to macros 2.0 Extends the unused macro lint (added in PR #41907) to macros 2.0 (added in PR #40847). r? @jseyfried
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
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
Addresses parts of #34938, to add a lint for unused macros.
We now output warnings by default when we encounter a macro that we didn't use for expansion.
Issues to be resolved before this PR is ready for merge:
#[allow(unused_macros)]
next to them if they should be kept for some reason. This is needed for successful boostrap and bors to accept the PR. -> Remove unused macros from the codebase #41934implement the full extent of Unused lint warnings for macros #34938, that means the macro match arm checking as well.let's not do this for now