Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow dead_code and unused_imports for dev builds only?

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.

like image 631
Reactgular Avatar asked Nov 02 '19 13:11

Reactgular


1 Answers

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.

like image 140
apetranzilla Avatar answered Oct 30 '22 00:10

apetranzilla