Rust-clippy: `needless_bool` triggers on `cfg!` comparisons

Created on 16 Apr 2019  路  3Comments  路  Source: rust-lang/rust-clippy

Clippy gives wrong suggestions if there's a bool comparison with a result of cfg!(...) macro, e.g. testing for a feature flag:

error: inequality checks against false are unnecessary
  --> safe_core/src/ipc/mod.rs:86:8
   |
86 |     if mock != cfg!(feature = "use-mock-routing") {
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `mock`
   |

If I try to compile with the future switched on (cargo build --features use-mock-routing), it gives an opposite suggestion:

error: inequality checks against true can be replaced by a negation
  --> safe_core/src/ipc/mod.rs:86:8
   |
86 |     if mock != cfg!(feature = "use-mock-routing") {
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!mock`
   |

Version:

$ cargo clippy -V
clippy 0.0.212 (1fac380 2019-02-20)
A-suggestion L-bug T-macros good-first-issue hacktoberfest

All 3 comments

To fix this, the span of the bool literal needs to be checked if it comes from a macro expansion.

I can give it a try. Thanks! :)

Let me know if you need any help.

Was this page helpful?
0 / 5 - 0 ratings