Skip to content
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

Merged
merged 8 commits into from
May 17, 2017
Merged

Add lint for unused macros #41907

merged 8 commits into from
May 17, 2017

Conversation

est31
Copy link
Member

@est31 est31 commented May 11, 2017

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
Collaborator

r? @pnkfelix

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

@est31
Copy link
Member Author

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.

@rust-highfive rust-highfive assigned jseyfried and unassigned pnkfelix May 11, 2017
@est31
Copy link
Member Author

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

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

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,

@est31 est31 force-pushed the macro_unused branch 5 times, most recently from 0c72cd2 to a97a1c5 Compare May 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
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.
@est31 est31 changed the title Add lint for unused macros [WIP] Add lint for unused macros May 13, 2017
@est31
Copy link
Member Author

est31 commented May 13, 2017

okay, ready for review. r? @jseyfried

Copy link
Contributor

@jseyfried jseyfried left a 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

let id_span = match *self.macro_map[did] {
SyntaxExtension::NormalTT(_, isp, _) => isp,
_ => None
};
Copy link
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
            };

@@ -12,6 +12,7 @@ use super::Wrapping;

use ops::*;

#[allow(unused_macros)]
Copy link
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
Member Author

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
Contributor

Choose a reason for hiding this comment

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

Makes sense.

exp,
Some((def.id, def.span)),
attr::contains_name(&def.attrs, "allow_internal_unstable")
)
Copy link
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.

// 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
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
Member Author

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.

// 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
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
Copy link
Contributor

bors commented May 16, 2017

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

@bors
Copy link
Contributor

bors commented May 16, 2017

💔 Test failed - status-travis

@Mark-Simulacrum
Copy link
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/git.luolix.top-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
Member

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

@arielb1 arielb1 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
Copy link
Contributor

bors commented May 16, 2017

⌛ 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
Copy link
Contributor

bors commented May 17, 2017

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

@bors bors merged commit 6dbd706 into rust-lang:master May 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
@brson brson added the relnotes Marks issues that should be documented in the release notes of the next release. label Jun 6, 2017
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.

Fixes rust-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.

Fixes rust-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.

Fixes rust-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.

Fixes rust-lang#73576
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: 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