Skip to content

Warn for unmatchable NaN case. #6804

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
cscott opened this issue May 29, 2013 · 4 comments
Closed

Warn for unmatchable NaN case. #6804

cscott opened this issue May 29, 2013 · 4 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@cscott
Copy link

cscott commented May 29, 2013

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".

@emillon
Copy link
Contributor

emillon commented Jul 24, 2013

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
Copy link
Member

huonw commented Jul 24, 2013

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

@emillon
Copy link
Contributor

emillon commented Jul 24, 2013

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
Copy link
Author

cscott commented Jul 24, 2013

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

   match vector {
      (NaN, _) => true,
      _ => false
    }

bors added a commit that referenced this issue Jul 25, 2013
Hi,

As noted in #6804, a pattern that contains `NaN` will never match because `NaN != NaN`. This adds a warning for such a case. The first commit handles the basic case and the second one generalizes it to more complex patterns using `walk_pat`.
@bors bors closed this as completed in 5c729c0 Jul 25, 2013
arielb1 added a commit to arielb1/rust that referenced this issue Oct 26, 2016
flip1995 pushed a commit to flip1995/rust that referenced this issue May 6, 2021
Fix unnecessary_filter_map false positive

changelog: Fix an [`unnecessary_filter_map`] false positive

Fixes rust-lang#6804
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

3 participants