9

I have something like:

#[macro_use] extern crate log;

pub fn do_nothing() { let _ = log::Level::Info; }

#[cfg(test)]
mod tests {
    #[test]
    fn test_a() { debug!("Message."); }
}

this compiles with a warning:

warning: unused `#[macro_use]` import

If I remove the macro import, and change the 1st line to:

extern crate log;

then I get the following error at compile time:

error: cannot find macro `debug!` in this scope

If I then try and import the macros only for the tests module, i.e.:

extern crate log;

pub fn do_nothing() { let _ = log::Level::Info; }

#[cfg(test)]

mod tests {
    #[macro_use] extern crate log;
    #[test]
    fn test_a() { debug!("Message."); }
}

then I get the compiler error:

error[E0468]: an `extern crate` loading macros must be at the crate root

Is there a solution which avoids all warnings without just suppressing them?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Dave Challis
  • 3,525
  • 2
  • 37
  • 65

1 Answers1

14

You can use cfg_attr:

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

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366