Skip to content

invalid_nan_comparisons suggests invalid 5f32.is_nan() in const context #114471

Closed
@matthiaskrgr

Description

@matthiaskrgr

Code

fn main() {
    const TEST: bool = 5f32 == f32::NAN;
}

Current output

warning: constant `TEST` is never used
 --> src/main.rs:2:11
  |
2 |     const TEST: bool = 5f32 == f32::NAN;
  |           ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
 --> src/main.rs:2:24
  |
2 |     const TEST: bool = 5f32 == f32::NAN;
  |                        ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(invalid_nan_comparisons)]` on by default
help: use `f32::is_nan()` or `f64::is_nan()` instead
  |
2 -     const TEST: bool = 5f32 == f32::NAN;
2 +     const TEST: bool = 5f32.is_nan();
  |

warning: 2 warnings emitted

Desired output

No response

Rationale and extra context

The suggestion is auto applicable

fn main() {
    const TEST: bool = 5f32.is_nan();
}

but does not compiled because it relies on unstabilized features

error: `core::f32::<impl f32>::is_nan` is not yet stable as a const fn
 --> src/main.rs:2:24
  |
2 |     const TEST: bool = 5f32.is_nan();
  |                        ^^^^^^^^^^^^^
  |
  = help: add `#![feature(const_float_classify)]` to the crate attributes to enable

error: aborting due to previous error

Other cases

No response

Anything else?

rustc 1.73.0-nightly (474709a 2023-08-03)
binary: rustc
commit-hash: 474709a
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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