rust-lang / rust-lang/rust

Confusing #warn(unused_comparisons)

Open
#149,490 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
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

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.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.