Confusing #warn(unused_comparisons)
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
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 emitted
Desired output
Not sure, but possibilities include:
- no warning
- a different warning
- more careful wording of this warning
Rationale 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.2
Anything else?
No response
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.
Research direction
Start by compiling the provided Rust example with rustc and compare the unused_comparisons diagnostic with the runtime assertions. Investigate how the warning handles the cast and literal, then determine an agreed warning behavior; done requires a diagnostic that accurately reflects this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100