Clippy lints / Correctness
Clippy lint · Correctness · checked with Clippy 0.1.99
clippy::absurd_extreme_comparisons: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
What it meansChecks for comparisons where one side of the relation is either the minimum or maximum value for its type and warns if it involves a case that is always true or always false. Only integer and boolean types are checked. It is in the correctness group and denied by default (the build fails).
What you see
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false --> main.rs:4:4 | 4 | if vec.len() <= 0 {} | ^^^^^^^^^^^^^^ | = help: because `0` is the minimum value for this type, the case where the two sides are not equal never occurs, consider using `vec.len() == 0` instead = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#absurd_extreme_comparisons = note: `#[deny(clippy::absurd_extreme_comparisons)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
An expression like min <= x may misleadingly imply that it is possible for x to be less than the minimum. Expressions like max < x are probably mistakes.
Code that is almost certainly wrong. Denied by default: the build fails until you fix it or allow it.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
let vec: Vec<isize> = Vec::new();
if vec.len() <= 0 {}
if 100 > i32::MAX {}
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::absurd_extreme_comparisons)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
absurd_extreme_comparisons = "allow"One run
cargo clippy -- -A clippy::absurd-extreme-comparisonsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for comparisons where one side of the relation is either the minimum or maximum value for its type and warns if it involves a case that is always true or always false. Only integer and boolean types are checked.
Why is this bad?
An expression like min <= x may misleadingly imply
that it is possible for x to be less than the minimum. Expressions like
max < x are probably mistakes.
Known problems
For usize the size of the current compile target will
be assumed (e.g., 64 bits on 64 bit systems). This means code that uses such
a comparison to detect target pointer width will trigger this lint. One can
use mem::sizeof and compare its value or conditional compilation
attributes
like #[cfg(target_pointer_width = "64")] .. instead.
Example
let vec: Vec<isize> = Vec::new();
if vec.len() <= 0 {}
if 100 > i32::MAX {}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- almost_swappedChecks for foo = bar; bar = foo sequences
- approx_constantapproximate value of `f{32, 64}::consts::PI` found
- async_yields_asyncan async construct yields a type which is itself awaitable
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::almost_swappedChecks for foo = bar; bar = foo sequencesRead it next →Did this answer your question?