Skip to content

Confusing #warn(unused_comparisons) #149490

@skvadrik

Description

@skvadrik

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions