Skip to content

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

Closed
@hcpl

Description

@hcpl

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`
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions