21

The unused imports and dead code warnings are the most common that I've found while learning Rust, and they get annoying after awhile (a very short while, like a few seconds). Especially when they are mixed with compiler errors, because it makes the console difficult to read.

I was able to turn off these warnings:

#![allow(unused_imports)]
#![allow(dead_code)]

This will disable the warnings for all builds, but I want the warnings enabled for release builds.

I tried disabling them like this:

#![cfg(dev)]
#![allow(unused_imports)]
#![allow(dead_code)]

But, this removed the entire Rust file from release builds (not what I want).

I tried to configure using cfg_attr but it had no effect for either builds.

#![cfg_attr(dev, allow(unused_imports))]
#![cfg_attr(dev, allow(dead_code))]

I have Googled and read all the related questions on StackOverflow but can't figure this out.

Reactgular
  • 52,335
  • 19
  • 158
  • 208

1 Answers1

15

dev isn't a supported predicate for conditional compilation, so your examples will never include the affected code. As far as I know, the best way to detect debug mode is instead with #[cfg(debug_assertions)]. With my testing, #![cfg_attr(debug_assertions, allow(dead_code, unused_imports))] seems to work to disable the lints for debug builds but enable them in release builds.

You can see a list of supported predicates in the Rust reference.

apetranzilla
  • 5,331
  • 27
  • 34