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 #[macro_use] import" false positive in libs and unit-tests #44342

Closed
hcpl opened this issue Sep 5, 2017 · 7 comments
Closed

"unused #[macro_use] import" false positive in libs and unit-tests #44342

hcpl opened this issue Sep 5, 2017 · 7 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@hcpl
Copy link

hcpl commented Sep 5, 2017

It doesn't recognize that assert_eq! from pretty_assertions has shadowed the standard one.

Code

src/lib.rs:

#[macro_use]
extern crate pretty_assertions;

pub fn has_bug() {
    assert_eq!(0, 42);
}

#[test]
fn test_has_bug() {
    assert_eq!(0, 42);
}

Command and output

$ touch src/lib.rs && cargo build
   Compiling pretty v0.1.0 (file:///tmp/rust/pretty)
warning: unused `#[macro_use]` import
 --> src/main.rs:2:1
  |
2 | #[macro_use]
  | ^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 2.28 secs
$ touch src/lib.rs && cargo test
   Compiling pretty v0.1.0 (file:///tmp/rust/pretty)
warning: unused `#[macro_use]` import
 --> src/lib.rs:1:1
  |
1 | #[macro_use]
  | ^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 2.73 secs
     Running target/debug/deps/pretty-be6cb182e74a998d

...

Configuration

$ rustc --version --verbose
rustc 1.20.0 (f3d6973f4 2017-08-27)
binary: rustc
commit-hash: f3d6973f41a7d1fb83029c9c0ceaf0f5d4fd7208
commit-date: 2017-08-27
host: x86_64-unknown-linux-gnu
release: 1.20.0
LLVM version: 4.0
$
$ cargo --version --verbose
cargo 0.21.0 (5b4b8b2ae 2017-08-12)
release: 0.21.0
commit-hash: 5b4b8b2ae3f6a884099544ce66dbb41626110ece
commit-date: 2017-08-12

Note that cargo run with equivalent src/main.rs:

#[macro_use]
extern crate pretty_assertions;

fn main() {
    assert_eq!(0, 42);
}

works fine:

$ touch src/main.rs && cargo run
   Compiling pretty v0.1.0 (file:///tmp/rust/pretty)
    Finished dev [unoptimized + debuginfo] target(s) in 1.77 secs
     Running `target/debug/pretty`

But this src/main.rs:

extern crate pretty;
#[macro_use]
extern crate pretty_assertions;

use pretty::has_bug;

fn main() {
    has_bug();
}

will trigger the warning too:

$ touch src/main.rs && cargo run
   Compiling pretty v0.1.0 (file:///tmp/rust/pretty)
warning: unused `#[macro_use]` import
 --> src/main.rs:2:1
  |
2 | #[macro_use]
  | ^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 1.68 secs
     Running `target/debug/pretty`
...
@hcpl
Copy link
Author

hcpl commented Sep 6, 2017

cc @est31 as the one worked on Add lint for unused macros PR

@alexcrichton alexcrichton added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 7, 2017
@RReverser
Copy link
Contributor

RReverser commented Jan 4, 2018

It doesn't recognize that assert_eq! from pretty_assertions has shadowed the standard one.

In fact, problem is somewhat different and more generic: unused lint doesn't see anything that is used inside of #[test] functions.

I've run into this with a local macro as well (which initially seemed different, but now I think is the same issue): https://play.rust-lang.org/?gist=0c4a2ca1e118674b9d6666e58a37da97&version=stable

// Produces warning: unused macro definition
macro_rules! assert_ok {
    ($s:expr, $res:expr, $rest:expr) => {
        assert_eq!($s, Ok(($res, $rest)))
    };
}

#[test]
fn test() {
    assert_ok!(Ok(1, ""), 1, "");
}

fn main() {}

It could be understandable if it would work at least if I add #[cfg(test)] to the macro, so that macro would be in scope only when test is, that is, it would be always used, but even that doesn't help to suppress the warning.

UPD: Actually it does help to suppress the warning https://play.rust-lang.org/?gist=0bdf3930b49f0835db08ed351a405b7e&version=stable, for some reason that didn't work locally.

@hcpl Try adding #[cfg(test)] to your extern crate item and see if it helps.

@rcoh
Copy link
Contributor

rcoh commented Mar 5, 2018

Adding #[cfg(test)] to the extern crate fixed the issue for me

@andrewdavidmackenzie
Copy link

I have the issue, I need the crate always (for test and normal config), but I only need the #[macro_use] in the test config.

If I use:

#[macro_use]
extern crate serde_json;

then I get the warning during compile (non-test config compile I guess).

I tried to use #[cfg(test)] but that then removes the extern crate for the normal config

so, I had to use this:

#[cfg(test)]
#[macro_use]
extern crate serde_json;
#[cfg(not(test))]
extern crate serde_json;

@RReverser
Copy link
Contributor

@andrewdavidmackenzie Doesn't seem same as this issue, but have you tried cfg_attr for that instead?

@kaiba696
Copy link

#[cfg_attr(test, macro_use)] 
extern crate serde_json;

should do it

@nikomatsakis
Copy link
Contributor

It seems like this is not a bug -- it is unfortunate that "unused" lints and so forth fire even when other configurations may use those things, but it is not specific to this lint and would be rather difficult to truly fix. Closing for now.

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. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants