Skip to content

Add lint for unused macros - #41907

Merged
bors merged 8 commits into
rust-lang:masterfrom
est31:macro_unused
May 17, 2017
Merged

Add lint for unused macros#41907
bors merged 8 commits into
rust-lang:masterfrom
est31:macro_unused

Conversation

@est31

@est31est31 commented May 11, 2017

Copy link
Copy Markdown
Member

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:

  • fix the NodeId issue described above
  • 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. -> Remove unused macros from the codebase #41934
  • implement 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

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

@est31

Copy link
Copy Markdown
MemberAuthor

r? @jseyfried because you seem to have done many things with macros in the past, so might know the code best.

@est31

Copy link
Copy Markdown
MemberAuthor

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 ExprKind like UnusedMac or something that then lives on until the linter runs.

@alexcrichtonalexcrichton added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 11, 2017
@est31

Copy link
Copy Markdown
MemberAuthor

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,

@est31
est31force-pushed the macro_unused branch 5 times, most recently from 0c72cd2 to a97a1c5CompareMay 12, 2017 10:13
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 12, 2017
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.
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 12, 2017
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.
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 12, 2017
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.
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 13, 2017
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

Copy link
Copy Markdown
Contributor

If @jseyfried doesn't appear, then r=me after #41934 is merged.

est31 added 5 commits May 13, 2017 16:02
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.
@est31est31 changed the title Add lint for unused macros [WIP]Add lint for unused macrosMay 13, 2017
@est31

Copy link
Copy Markdown
MemberAuthor

okay, ready for review. r? @jseyfried

@jseyfriedjseyfried left a comment

Copy link
Copy Markdown
Contributor

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

Comment threadsrc/librustc_resolve/macros.rs Outdated
let id_span = match *self.macro_map[did] {
SyntaxExtension::NormalTT(_, isp, _) => isp,
_ => None
};

Copy link
Copy Markdown
Contributor

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};


use ops::*;

#[allow(unused_macros)]

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

Comment threadsrc/libsyntax/ext/tt/macro_rules.rs Outdated
exp,
Some((def.id, def.span)),
attr::contains_name(&def.attrs, "allow_internal_unstable")
)

Copy link
Copy Markdown
Contributor

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.

Comment threadsrc/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.

Copy link
Copy Markdown
Contributor

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!?

Copy link
Copy Markdown
MemberAuthor

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.

Comment threadsrc/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>,

Copy link
Copy Markdown
Contributor

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!).

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 16, 2017
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

bors commented May 16, 2017

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 6dbd706 with merge 9ccc84d...

@bors

bors commented May 16, 2017

Copy link
Copy Markdown
Collaborator

💔 Test failed - status-travis

@Mark-Simulacrum

Copy link
Copy Markdown
Member

[01:15:04] ---- concurrent_installs stdout ----
[01:15:04] thread 'concurrent_installs' panicked at '
[01:15:04] Expected: execs
[01:15:04] but: exited with exit code: 101
[01:15:04] --- stdout
[01:15:04] [01:15:04] --- stderr
[01:15:04] Updating registry `file:///checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/cit/t0/registry`
[01:15:04] Blocking waiting for file lock on the registry index
[01:15:04] error: could not find `foo` in `registry https://github.com/rust-lang/crates.io-index`
[01:15:04] ', /cargo/registry/src/github.com-1ecc6299db9ec823/hamcrest-0.1.1/src/core.rs:31
[01:15:04] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[01:15:04] [01:15:04] [01:15:04] failures:
[01:15:04] concurrent_installs

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

Copy link
Copy Markdown
Member

FWIW that was fixed by rust-lang/cargo#4051 and we'll get the update in #42039

@arielb1arielb1 added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 16, 2017
@bors

bors commented May 16, 2017

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 6dbd706 with merge 86319e4...

bors added a commit that referenced this pull request 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

bors commented May 17, 2017

Copy link
Copy Markdown
Collaborator

☀️ Test successful - status-appveyor, status-travis
Approved by: jseyfried
Pushing 86319e4 to master...

@bors
bors merged commit 6dbd706 into rust-lang:masterMay 17, 2017
bors added a commit that referenced this pull request Jun 3, 2017
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
@brsonbrson added the relnotes Marks issues that should be documented in the release notes of the next release. label Jun 6, 2017
@est31est31 mentioned this pull request Jun 22, 2020
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Apr 26, 2022
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
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 6, 2022
…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
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 6, 2022
…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
bors added a commit to rust-lang-ci/rust that referenced this pull request May 12, 2022
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
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

relnotesMarks issues that should be documented in the release notes of the next release.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@est31@rust-highfive@petrochenkov@jseyfried@bors@Mark-Simulacrum@alexcrichton@brson@pnkfelix@arielb1