Skip to content

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

Closed
@matthiaskrgr

Description

@matthiaskrgr
Member

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
on Aug 4, 2023
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Aug 4, 2023
removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Aug 5, 2023
added a commit that references this issue on Aug 6, 2023

Rollup merge of rust-lang#114486 - Urgau:const-context-nan-suggestion…

1305a43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @matthiaskrgr@saethlin@rustbot

      Issue actions

        `invalid_nan_comparisons` suggests invalid `5f32.is_nan()` in const context · Issue #114471 · rust-lang/rust