- Notifications
You must be signed in to change notification settings - Fork 14.1k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Code
#[allow(overflowing_literals)] fn bad(c: u8) -> bool { if (c as i8) >= 0xFF { return true; } else { return false; } } fn main() { assert!(bad(0)); assert!(!bad(0xFE)); }Current output
warning: comparison is useless due to type limits --> a.rs:3:8 | 3 | if (c as i8) >= 0xFF { | ^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_comparisons)]` on by default warning: 1 warning emittedDesired output
Not sure, but possibilities include: - no warning - a different warning - more careful wording of this warningRationale and extra context
While the current warning makes sense on the human level (i8 value is always less than 255), it is contrary to the way the code is compiled - the resulting program does not fail any of the assertions, which proves that both branches of the comparison are executed depending on the input argument. So, 0xFF literal is interpreted as -1, in which case it is well within bounds of i8 and the comparison is not useless.
Other cases
Rust Version
$ rustc --version --verbose rustc 1.91.1 (ed61e7d7e 2025-11-07) (built from a source tarball) binary: rustc commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb commit-date: 2025-11-07 host: x86_64-unknown-linux-gnu release: 1.91.1 LLVM version: 21.1.2Anything else?
No response
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.