Skip to content

innvalid invalid_nan_comparisons breaks: no method named is_nan found for reference &{float} #164143

Closed

Description

@matthiaskrgr

Using the following flags

--force-warn invalid_nan_comparisons

this code:

fn main() {
    let b = &3.3;
    if b != &f64::NAN {}
}

caused the following diagnostics:

    Checking crate-main v0.1.0 (/tmp/icemaker_global_tempdir.J6giLa0jxU8j/icemaker_clippyfix_tempdir.CpGHsJu3V6ae/crate-main)
warning: incorrect NaN comparison, NaN cannot be directly compared to itself
 --> src/main.rs:3:8
  |
3 |     if b != &f64::NAN {}
  |        ^^^^^^^^^^^^^^
  |
  = note: requested on the command line with `--force-warn invalid-nan-comparisons`
help: use `f32::is_nan()` or `f64::is_nan()` instead
  |
3 -     if b != &f64::NAN {}
3 +     if !b.is_nan() {}
  |

warning: `crate-main` (bin "crate-main") generated 1 warning (run `cargo clippy --fix --bin "crate-main" -p crate-main -- -Aclippy::all -Awarnings --force-warn invalid_nan_comparisons --cap-lints warn` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s

However after applying these diagnostics, the resulting code:

fn main() {
    let b = &3.3;
    if !b.is_nan() {}
}

no longer compiled:

    Checking crate-main v0.1.0 (/tmp/icemaker_global_tempdir.J6giLa0jxU8j/icemaker_clippyfix_tempdir.CpGHsJu3V6ae/crate-main)
error[E0599]: no method named `is_nan` found for reference `&{float}` in the current scope
 --> src/main.rs:3:11
  |
3 |     if !b.is_nan() {}
  |           ^^^^^^
  |
help: there is a method `is_qnan` with a similar name
  |
3 |     if !b.is_qnan() {}
  |              +

For more information about this error, try `rustc --explain E0599`.
error: could not compile `crate-main` (bin "crate-main" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `crate-main` (bin "crate-main") due to 1 previous error

Version:

rustc 1.101.0-nightly (7211ea594 2026-10-11)
binary: rustc
commit-hash: 7211ea594cbf137ef6118b1752b959dea7cb9088
commit-date: 2026-10-11
host: x86_64-unknown-linux-gnu
release: 1.101.0-nightly
LLVM version: 23.1.3

Activity

  1. added
    A-diagnostics
    C-bug
    A-suggestion-diagnostics
    D-invalid-suggestion
    A-floating-point
    L-invalid_nan_comparisons
    on Oct 11, 2026
  2. added
    T-compiler
    on Oct 11, 2026
  3. caelyreth commented on Oct 13, 2026

    @caelyreth
    Contributor

    I'd like to work on it.

    The compiler suggestion turns b != &f64::NAN into !b.is_nan(), but this only edits the spans; it does not look at the type of b.

    In let b = &3.3, that comparison is what makes {float} become f64. After the patch, b is still &{float}, and .is_nan() is not found there. Btw, (*b).is_nan() has the same problem.

    tests/ui/lint/invalid-nan-comparison-suggestion.rs#L31-L37 already has a reference case, but it uses &2.3f32, so b.is_nan() works through autoderef. I suspected that an unsuffixed float is not covered.

    So the suggestion should depend on the type.

    That said, if it is already f32 or f64 (or a reference), keep !b.is_nan(). If it is still an unsuffixed float, suggest !f64::is_nan(*b), instead, use the NaN's type, and add one * for each &, so the float still gets a concrete type. If it is some other type, only keep the help text, and don't offer an edit that rustfix would apply.

    @rustbot claim

  4. added 6 commits that reference this issue on Oct 20, 2026
    157c2cb
    3712698
    2930b12
    e0beadb
    40cb792
    20c2948
  5. added a commit that references this issue on Oct 21, 2026
    ddb54cc
  6. added a commit that references this issue on Oct 21, 2026
    8d67a75
  7. added a commit that references this issue on Oct 22, 2026
    06736f1
  8. added a commit that references this issue on Oct 22, 2026
    e886a33
  9. added a commit that references this issue on Oct 25, 2026
    26bc6ac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsA-floating-pointA-suggestion-diagnosticsC-bugD-invalid-suggestionL-invalid_nan_comparisonsT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions