Uh oh!
There was an error while loading. Please reload this page.
Add lint for unused macros - #41907
Conversation
rust-highfive
commented
May 11, 2017
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
est31
commented
May 11, 2017
r? @jseyfried because you seem to have done many things with macros in the past, so might know the code best. |
est31
commented
May 11, 2017
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 |
est31
commented
May 12, 2017
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
a97a1c5CompareRemove 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.
petrochenkov
commented
May 13, 2017
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.
est31
commented
May 13, 2017
okay, ready for review. r? @jseyfried |
jseyfried
left a comment
There was a problem hiding this comment.
Looks good! r=me modulo comments
| let id_span = match *self.macro_map[did] { | ||
| SyntaxExtension::NormalTT(_, isp, _) => isp, | ||
| _ => None | ||
| }; |
There was a problem hiding this comment.
nit: this is usually formatted
let id_span = match*self.macro_map[did]{SyntaxExtension::NormalTT(_, isp, _) => isp,
_ => None};| use ops::*; | ||
| #[allow(unused_macros)] |
There was a problem hiding this comment.
Is there a reason we can't just remove the unused macro here?
There was a problem hiding this comment.
There is a FIXME below to get the remaining impls uncommented, including invocations of the macro.
| exp, | ||
| Some((def.id, def.span)), | ||
| attr::contains_name(&def.attrs, "allow_internal_unstable") | ||
| ) |
There was a problem hiding this comment.
nit: nonstandard formatting -- should be three lines with "visual indenting" or indented one block with four/five lines.
| // 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.
Could you clarify that this is only for crate-local macro_rules!?
There was a problem hiding this comment.
Okay, but hopefully the upcoming macros 2.0 macros could be included as well.
| // 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.
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*
bors
commented
May 16, 2017
⌛ Testing commit 6dbd706 with merge 9ccc84d... |
bors
commented
May 16, 2017
💔 Test failed - status-travis |
Mark-Simulacrum
commented
May 16, 2017
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 |
alexcrichton
commented
May 16, 2017
FWIW that was fixed by rust-lang/cargo#4051 and we'll get the update in #42039 |
bors
commented
May 16, 2017
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*
bors
commented
May 17, 2017
☀️ 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. Fixesrust-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. Fixesrust-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. Fixesrust-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. Fixesrust-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