assertions_on_constants is falsely reported on is_x86_feature_detected depending on target-cpu configuration

Open
#16,434 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
devtools

Research direction

Start with the assertions_on_constants lint implementation and its tests, then reproduce the report using src/main.rs and the .cargo/config.toml target-cpu=x86-64-v2 configuration. Done means configuration-dependent is_x86_feature_detected! assertions are not falsely warned about, while ordinary constant assertions retain their existing behavior.

Written by the indexing model from the issue text.

Description

C-bug I-false-positive
Summary

When a -Ctarget-cpu option is passed to rustc, certain is_x86_feature_detected! invocations become compile-time constants. Clippy flags this code with clippy::assertions_on_constants, despite the constantness of the expression being dependent on the build configuration.

Lint Name

assertions_on_constants

Reproducer

I tried this code:

fn main() {
    assert!(std::arch::is_x86_feature_detected!("sse4.2"));
}

By default, no clippy assertion is issued (which is correct). If you then add the following to .cargo/config.toml:

[target.'cfg(target_arch = "x86_64")']
rustflags = ["-Ctarget-cpu=x86-64-v2"]

This now occurs

warning: this assertion has a constant value
 --> src/main.rs:2:5
  |
2 |     assert!(std::arch::is_x86_feature_detected!("sse4.2"));
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: consider moving this into a const block: `const { assert!(..) }`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#assertions_on_constants
  = note: `#[warn(clippy::assertions_on_constants)]` on by default

Further note that following the suggestion fails:

error[E0015]: cannot call non-const function `std_detect::detect::arch::x86::__is_feature_detected::sse4_2` in constants
 --> src/main.rs:2:21
  |
2 |     const { assert!(std::arch::is_x86_feature_detected!("sse4.2")) };
  |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: calls in constants are limited to constant functions, tuple structs and tuple variants
  = note: this error originates in the macro `std::arch::is_x86_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0015`.
error: could not compile `tmp-warning` (bin "tmp-warning") due to 1 previous error
Version
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3
Additional Labels

No response

Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

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.

More from rust-lang/rust-clippy

All issues in rust-lang/rust-clippy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.