Skip to content

Spurious "useless lint attribute" warning #1938

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

Closed
joshlf opened this issue Aug 8, 2017 · 18 comments
Closed

Spurious "useless lint attribute" warning #1938

joshlf opened this issue Aug 8, 2017 · 18 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@joshlf
Copy link

joshlf commented Aug 8, 2017

Given the following code:

#[macro_use]
extern crate lazy_static;

cargo build produces the following warning:

warning: unused `#[macro_use]` import
 --> src/lib.rs:1:1
  |
1 | #[macro_use]
  | ^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

If we then add #[allow(unused_imports)]:

#[allow(unused_imports)]
#[macro_use]
extern crate lazy_static;

then cargo build produces no warnings. However, if we run cargo clippy on this code, we get a "useless lint attribute" warning:

warning: useless lint attribute
 --> src/lib.rs:1:1
  |
1 | #[allow(unused_imports)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(unused_imports)]`
  |
  = note: #[warn(useless_attribute)] on by default
  = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#useless_attribute
@oli-obk oli-obk added the C-bug Category: Clippy is not doing the correct thing label Aug 9, 2017
@oli-obk
Copy link
Contributor

oli-obk commented Aug 9, 2017

Question: why do you want to ignore an unused #[macro_use]?

This is a known limitation of the lint (see https://rust-lang-nursery.github.io/rust-clippy/master/index.html#useless_attribute ), but as the limitation states, it's easier to remove the unused import than to add an allow.

@joshlf
Copy link
Author

joshlf commented Aug 9, 2017

Question: why do you want to ignore an unused #[macro_use]?

My crate exports macros that, internally, use lazy_static. Thus, when compiling the crate itself, nobody has called those macros, and thus lazy_static isn't actually used, so an unused import warning is generated.

as the limitation states, it's easier to remove the unused import than to add an allow.

In my case, I actually need the import.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 9, 2017

That sounds more like a rustc bug to me. Have you reported this upstream?

@joshlf
Copy link
Author

joshlf commented Aug 9, 2017

I don't think it's a rustc bug. Maybe I'm missing something though. Here's the full file if you're curious.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 10, 2017

Ah... your macro is unusable within crates that don't have

#[macro_use]
extern crate lazy_static;

So I suggest to move the #[macro_use] extern crate lazy_static; at the beginning of your macro: https://gist.github.com/joshlf/87783078dad1d888a321db50da5d4097#file-lib-rs-L484

The trick mentioned in https://gist.github.com/joshlf/87783078dad1d888a321db50da5d4097#file-lib-rs-L474 kinda works but is hacky imo. The solution of moving the crate import into the macro itself is also used by serde and others, so I consider that solution idiomatic.

@joshlf
Copy link
Author

joshlf commented Aug 11, 2017

I'm not sure that it would work. My concern is that since #[macro_use] is only allowed on extern crate declarations at the crate root, it would bar the macro from being used anywhere other than a crate's root. I'll have to try it out to see, though.

@joshlf
Copy link
Author

joshlf commented Aug 11, 2017

Oh, and more problematically, putting #[macro_use] extern crate lazy_static; in the macro requires a lazy_static dependency in the consuming crate's Cargo.toml, which I definitely want to avoid.

@lilith
Copy link

lilith commented Oct 3, 2017

This is a major pain with #![feature(alloc_system)] see

rustc gives a spurious unused crate error for extern crate alloc_system.

When we disable that lint, #[allow(unused_extern_crates)], clippy complains (incorrectly) about that attribute.

Which brings us to this mess:

#![feature(alloc_system)]
#[cfg_attr(feature = "cargo-clippy", allow(useless_attribute))]
#[allow(unused_extern_crates)]
extern crate alloc_system;

@llogiq
Copy link
Contributor

llogiq commented Oct 4, 2017

The unused_extern_crates lint was made to warn by default recently, but this lead to many false positives. The newest (or next?) nightly should have a PR that disables the lint again until the false positives are sorted out.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 15, 2017

Oh, and more problematically, putting #[macro_use] extern crate lazy_static; in the macro requires a lazy_static dependency in the consuming crate's Cargo.toml, which I definitely want to avoid.

You have to do that anyway as far as I can see.

When we disable that lint, #[allow(unused_extern_crates)], clippy complains (incorrectly) about that attribute.

That has been fixed in rustc

@joshlf
Copy link
Author

joshlf commented Nov 15, 2017

Oh, and more problematically, putting #[macro_use] extern crate lazy_static; in the macro requires a lazy_static dependency in the consuming crate's Cargo.toml, which I definitely want to avoid.

You have to do that anyway as far as I can see.

Nah, I'm able to do:

#[doc(hidden)]
pub use lazy_static::*;

When we disable that lint, #[allow(unused_extern_crates)], clippy complains (incorrectly) about that attribute.

That has been fixed in rustc

Good to know!

@oli-obk
Copy link
Contributor

oli-obk commented Nov 16, 2017

In that case the lint unused_imports won't be triggered, right?

@brandur
Copy link

brandur commented Mar 20, 2018

Another example of a false positive case: I use serde_json in my project, but I only need the macros it provides in my test suite. If I annotate it with #[macro_use], Rustc correctly complains that I'm not using those macros (outside of tests).

The solution is to annotate the extern crate with #[allow(unused_imports)], but unfortunately, this leads to Clippy complaining.

Whether you consider this a Rustc problem or not, it's pretty unambiguous that the "why this is bad" description is wrong isn't it?

Lint attributes have no effect on crate imports. Most likely a ! was forgotten

Having an allow there fixes a compiler warning, so it certainly has an effect.

@sinkuu
Copy link
Contributor

sinkuu commented Mar 21, 2018

BTW you can simply annotate it with #[cfg_attr(test, macro_use)].

@brandur
Copy link

brandur commented Mar 21, 2018

@sinkuu Thanks! I'd basically solved it with:

#[cfg_attr(feature = "cargo-clippy", allow(useless_attribute))]
#[allow(unused_imports)]
#[macro_use]
extern create serde_json;

But that seems better.

@dtolnay
Copy link
Member

dtolnay commented May 14, 2018

As reported in #1995 and #2607, this false positive is triggered when re-exporting procedural macros.

#[allow(unused_imports)]
#[macro_use]
extern crate repro_derive;

pub use repro_derive::*;

@gnzlbg
Copy link
Contributor

gnzlbg commented Jul 19, 2018

I have a lot of code using cfg_if in which #[allow(unused_import)] simplifies many branches. Otherwise, I would actually have to code all potential target_arch, target_feature, target_os, ... combinations that would actually need the import, which would be a mess compared to just:

cfg_if! {
    if #[cfg(all(target_arch = "arm", target_feature = "v7", target_feature = "neon"))] {
        // Do something differently
        extern crate coresimd;
        #[allow(unused_import)]
        use coresimd::arch;
    } else {
        // Add the import because some other target_arch/target_feature combinations
        // need it, but not all:
        #[allow(unused_import)]
        use core::arch;
    }
}

@flip1995
Copy link
Member

@gnzlbg #[allow(unused_imports)] is whitelisted for use items. The unused_import (without s) lint doesn't exist, see rustc book.

SamuelMarks added a commit to Fantom-foundation/old-vm-transport-consensus-rs that referenced this issue Oct 29, 2019
danwilliams added a commit to danwilliams/rubedo that referenced this issue May 3, 2024
  - Disabled the clippy::std_instead_of_core lint for the std module due
    to a false positive on std::env, which is due to the following bug:

    rust-lang/rust-clippy#12438

    The fix had to be applied globally otherwise it triggered a "useless
    lint attribute" warning, which may be another bug!

    rust-lang/rust-clippy#1938
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

9 participants