Repository navigation
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsA-floating-pointArea: Floating point numbers and arithmeticA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-invalid_nan_comparisonsLint: invalid_nan_comparisonsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
Description
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
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsA-floating-pointArea: Floating point numbers and arithmeticA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-invalid_nan_comparisonsLint: invalid_nan_comparisonsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
Activity
caelyreth commentedon Oct 13, 2026
I'd like to work on it.
The compiler suggestion turns
b != &f64::NANinto!b.is_nan(), but this only edits the spans; it does not look at the type ofb.In
let b = &3.3, that comparison is what makes{float}becomef64. After the patch,bis 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, sob.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
f32orf64(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
Rollup merge of rust-lang#164264 - caelyreth:fix-invalid-nan-float, r…
Rollup merge of rust-lang#164264 - caelyreth:fix-invalid-nan-float, r…
Rollup merge of rust-lang#164264 - caelyreth:fix-invalid-nan-float, r…
Rollup merge of rust-lang#164264 - caelyreth:fix-invalid-nan-float, r…
Rollup merge of rust-lang#164264 - caelyreth:fix-invalid-nan-float, r…
Rollup merge of rust-lang#164264 - caelyreth:fix-invalid-nan-float, r…
Rollup merge of rust-lang#164264 - caelyreth:fix-invalid-nan-float, r…
Unrolled build for #164264 in rollup 164662
Rollup merge of #164264 - caelyreth:fix-invalid-nan-float, r=jieyouxu
Rollup merge of #164264 - caelyreth:fix-invalid-nan-float, r=jieyouxu
Rollup merge of #164264 - caelyreth:fix-invalid-nan-float, r=jieyouxu