rust-lang / rust-lang/rust-clippy

nonminimal_bool and is_x86_feature_detected

Open
#14,153 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error T-macros
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

nonminimal_bool fires when chaining is_x86_feature_detected!(), suggesting invalid code.

Lint Name

nonminimal_bool

Reproducer

I tried this code:

pub fn check() -> bool {
    std::is_x86_feature_detected!("sse4.2") || std::is_x86_feature_detected!("avx2")
}

(playground)

I saw this happen:

warning: this boolean expression can be simplified
 --> src/lib.rs:2:5
  |
2 |     std::is_x86_feature_detected!("sse4.2") || std::is_x86_feature_detected!("avx2")
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool
  = note: `#[warn(clippy::nonminimal_bool)]` on by default
help: try
  |
2 |     !(!cfg!(target_feature = $target_feature_lit) && !$crate::detect::__is_feature_detected::$feature() && !$crate::detect::__is_feature_detected::$feature())
  |
2 |     std::is_x86_feature_detected!("sse4.2") || std::is_x86_feature_detected!("sse4.2") || std::is_x86_feature_detected!("avx2")
  |

I expected to see this happen:

I think clippy tries to tell me that avx2 implies sse4.2, so there’s no need to specify both. The output is extremely confusing though, and the suggestion is not valid Rust. I think either it should suggest correcting to just std::is_x86_feature_detected!("avx2") (although I’m not sure this could be reasonably implemented), or just skip this warning.

This reproduces on the latest nightly in Playground. It may be related to #12627, but I’m not sure if it’s a duplicate.

Version
rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.84.0
LLVM version: 19.1.5
Additional Labels

@rustbot label +I-suggestion-causes-error +T-macros

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the nonminimal_bool lint and reproduce the warning using the Rust Playground example that chains is_x86_feature_detected!(). Inspect how the macro is handled and how the suggestion is generated; done means the lint no longer emits invalid Rust for this case, either by producing a valid suggestion or skipping the warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.