rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`manual_range_contains` suggestion changes NaN handling

Open moritzmucha opened this issue 8 months ago • 1 comments

Summary

The current implementation of manual_range_contains changes NaN handling.

Manually negative-checking a float against a lower and upper bound, like q < 0.0 || q > 1.0, evaluates NaN as false. Clippy suggests that !(0.0..=1.0).contains(&q) is a better equivalent, but it's not equivalent - it evaluates NaN as true.

A linter should never change control flow.

Lint Name

manual_range_contains

Reproducer

I tried this code:

if q < 0.0 || q > 1.0 {
    () // raise an error
}

I saw this happen:

cargo clippy output:

warning: manual `!RangeInclusive::contains` implementation
   --> src/lib.rs:377:12
    |
377 |         if q < 0.0 || q > 1.0 {
    |            ^^^^^^^^^^^^^^^^^^ help: use: `!(0.0..=1.0).contains(&q)`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.91.0/index.html#manual_range_contains

I expected to see this happen:

Clippy should either accept the code as-is, or suggest the actual equivalent:

if !(0.0..=1.0).contains(&q) && !q.is_nan() {
    () // raise an error
}

Version

rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: aarch64-apple-darwin
release: 1.91.1
LLVM version: 21.1.2

Additional Labels

No response

moritzmucha avatar Mar 11 '26 18:03 moritzmucha

@rustbot claim

moses7054 avatar Mar 11 '26 19:03 moses7054