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

Unused lint warnings for macros #34938

Closed
est31 opened this issue Jul 20, 2016 · 9 comments
Closed

Unused lint warnings for macros #34938

est31 opened this issue Jul 20, 2016 · 9 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@est31
Copy link
Member

est31 commented Jul 20, 2016

If you declare a variable, and not use it, you get an unused code warning.

The same should be true for macros (that don't have the #[macro_export] attribute). So if you have a private macro like:

fn main() {
    macro_rules! do_something {
        ($x:expr) => {$x + 20}
    }
}

It should give you a warning. Same goes if you use a macro, but not all of its match arms:

fn main() {
    macro_rules! do_something {
        ($x:expr) => {$x + 20};
        ($i:ident, $x:expr) => {$i = $x + 20};
    }
    println!("done: {}", do_something!(2));
}

Note that this is no dupe or not even against #24580. That issue is about unused code inside macro expansions, but my issue is about macro use.

By description, this falls under the definition of the dead-code lint.

@mcarton
Copy link
Member

mcarton commented Jul 20, 2016

That's hard. Macro expansion happens before Clippy is run (so if you write do_something!(2) clippy actually only sees 2 + 20). We can know that something happened because of a macro but I don't know whether we can even get macro definitions and I'm sure we have no way to know what branch of the macro the expansion that comes from.

@mcarton
Copy link
Member

mcarton commented Jul 20, 2016

(oups, I somehow though this was reported on clippy, but it still is hard I guess)

@sanxiyn sanxiyn added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Jul 28, 2016
@est31 est31 mentioned this issue May 11, 2017
3 tasks
frewsxcv added a commit to frewsxcv/rust that referenced this issue 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*
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue 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 added a commit that referenced this issue 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*
@est31
Copy link
Member Author

est31 commented May 21, 2017

So PR #41907 has implemented the first case: a lint for entirely not used macros. The second case (unused macro arms) is still unimplemented. I however am not sure whether it should be added, for the following reason:

When doing the PR mentioned above, I found some macros that got the unused warning but which were still used, just by cfg-out'd code. E.g. take this case:

macro_rules! do_something {
    ($x:expr) => {$x + 20}
}
#[cfg(windows)]
fn foo() {
    do_something!();
}

Here, the correct response was to add the cfg clause to the do_something clause as well:

#[cfg(windows)]
macro_rules! do_something {
    ($x:expr) => {$x + 20}
}
#[cfg(windows)]
fn foo() {
    do_something!();
}

Now, what about the following case:

macro_rules! do_something {
    ($x:expr) => {$x + 20};
    ($i:ident, $x:expr) => {$i = $x + 20};
}
fn main() {
    println!("done: {}", do_something!(2));
}
#[cfg(windows)]
fn foo() {
    let mut i = 4;
    println!("done: {}", do_something!(i, 2));
}

Here, we'll get a warning on non windows platforms that the second macro case is unused. Easy, to solve, right? you just add a cfg to the match arm:

macro_rules! do_something {
    ($x:expr) => {$x + 20};
    #[cfg(windows)]
    ($i:ident, $x:expr) => {$i = $x + 20};
}

That unfortunately doesn't work, the parser doesn't expect such cfg arms here.

So if such a lint were present, the only way to act here would be to do two macros, or to turn the lint off for the macro, both of which are not really satisfying answers.

Therefore I think that for now implementing the lint for macro arms as well is not really a thing we should do.

@est31
Copy link
Member Author

est31 commented Jun 15, 2017

PR #42334 has expanded the lint to macros 2.0. Closing.

@est31 est31 closed this as completed Jun 15, 2017
@iago-lito
Copy link
Contributor

Rustc now warns about unused macro, but not about unused macro arms. Would this be considered an improvement to #42334 or does it need be considered separately?

@est31
Copy link
Member Author

est31 commented Jun 19, 2020

@iago-lito see my comment about the impossibility to attach cfg to macro arms. The basic issue is that macro arms aren't assigned their own id's, at least back when I made the comment. It might be different now.

@est31
Copy link
Member Author

est31 commented Jun 19, 2020

I'd file a new issue for unused macro arms. It is possible to implement it but you'd need changes in the language so that you can cfg out branches.

@iago-lito
Copy link
Contributor

@est31 Okay, thanks, I'll open that at least :) Do you think this new issue rather belongs to this repo or to the RFCs repo?

@est31
Copy link
Member Author

est31 commented Jun 20, 2020

I'd file one here. In the best case, it attracts more feedback here than in the RFCs repo, in the worst case, you are told to move the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

4 participants