assertions_on_constants is falsely reported on is_x86_feature_detected depending on target-cpu configuration
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rust-lang/rust-clippy
-
C-bug L-suggestion
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
rust-lang/rust-clippy#17674 · 5 comments ·
-
C-bug L-suggestion
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
rust-lang/rust-clippy#17673 · 3 comments ·
-
C-bug I-false-positive
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
rust-lang/rust-clippy#17566 ·
-
A-documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
rust-lang/rust-clippy#17259 · 3 comments ·
-
A-documentation A-website C-bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
rust-lang/rust-clippy#16981 · 1 reaction ·
All issues in rust-lang/rust-clippy
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
kwakseongjae/auto-hwp#319 ·
-
area:cli bug filter-quality good first issue priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
bevyengine/bevy#25861 ·
-
comp-datalake
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/ClickHouse#121222 ·
-
enhancement remote
Difficulty 2/5 1-3 hours Newbie friendliness 68/100