It doesn't recognize that assert_eq!
from pretty_assertions
has shadowed the standard one.
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);
}
$ 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
...
$ 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`
...
cc @est31 as the one worked on Add lint for unused macros PR
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.
Adding #[cfg(test)]
to the extern crate fixed the issue for me
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;
@andrewdavidmackenzie Doesn't seem same as this issue, but have you tried cfg_attr
for that instead?
#[cfg_attr(test, macro_use)]
extern crate serde_json;
should do it
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.
Most helpful comment
should do it