Skip to content

Warn for unmatchable NaN case. #6804

@cscott

Description

@cscott

Rust already warns for unmatchable cases in a match expression. Since NaN != NaN, any use of NaN (f64, float, other?) in a match pattern should generate the same unmatchable case warning.

If the reader is curious, the proper way to match NaN is something like:

match x {
    x if f64::isNaN(x) => { ... }
...
}

For bonus points, the unmatchable case warning for NaN could mention this: "use isNaN in a guard expression instead".

Activity

emillon

emillon commented on Jul 24, 2013

@emillon
Contributor

Hi,

I'm not sure what the "bad" way to match NaN would be. Since NaN is not a variant (it's just a constant defined as 0.0/0.0), matching against it (NaN => ...) binds a new variable that shadows NaN. It's an irrefutable pattern so it will definitely be noticed. I may have overlooked something though.

huonw

huonw commented on Jul 24, 2013

@huonw
Contributor

@emillon it is a static, which are valid in patterns.

emillon

emillon commented on Jul 24, 2013

@emillon
Contributor

Oops, thanks the clarification. So the test case is:

use std::float::NaN;

fn main() {
    let x = NaN;
    let isnan = match(x) {
        NaN => true,
        _ => false,
    };
    println (fmt!("%b", isnan))
}

Interesting project to dive in the compiler, let's see how it goes.

cscott

cscott commented on Jul 24, 2013

@cscott
Author

Ideally nested uses of NaN will trigger the same error, ie:

   match vector {
      (NaN, _) => true,
      _ => false
    }
added a commit that references this issue on Jul 24, 2013
5c729c0
added a commit that references this issue on Jul 25, 2013
added a commit that references this issue on May 6, 2021
9dd8705
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-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cscott@emillon@huonw

        Issue actions

          Warn for unmatchable NaN case. · Issue #6804 · rust-lang/rust