-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Summary
I have some constants in my code and trying to make sure that some relation between them holds. Using an assert
seem like an obvious choice and indeed - when I make a mistake - program fails to compile. Clippy complains that the check will be optimized away. While technically that's correct, compiler first performs the check so assert
totally works as expected or even better - a compile time error instead of a runtime one.
Is this a useful lint/behavior? It also comes with some cosmetic issues - I didn't write assert!(false)
, I wrote something else:
warning: `assert!(false)` should probably be replaced
--> src/main.rs:191:9
|
191 | assert!(1 == (1 + 1));
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: use `panic!()` or `unreachable!()`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
= note: `#[warn(clippy::assertions_on_constants)]` on by default
Even assert!(false, "message")
can be useful if you want to prevent a certain combination of feature flags from compiling.
A related ticket: #8159
Lint Name
clippy::assertions_on_constants
Reproducer
I tried this code:
const: u32 FOO = 4;
const: u32 BAR = 16;
assert!(BAR % FOO == 0, "make sure BAR is a multiple of FOO");
I saw this happen:
warning: `assert!(true)` will be optimized out by the compiler
I expected to see this happen:
No warning?
Version
cargo clippy --version clippy 0.1.88 (6b00bc3880 2025-06-23)
% rustc -Vv
rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5
Additional Labels
No response