Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

cmp_nan only compares against NaN constant #1205

Closed
killercup opened this issue Aug 28, 2016 · 5 comments · Fixed by #4910
Closed

cmp_nan only compares against NaN constant #1205

killercup opened this issue Aug 28, 2016 · 5 comments · Fixed by #4910
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy T-MIR Type: This lint will require working with the MIR

Comments

@killercup
Copy link
Member

IIUC, the cmp_nan lint just checks whether an expression is a constant whose last path segement is NAN.

That means that

0 == std::f32::NAN; //~ERROR doomed comparison with NAN

works as expected but more complicated (but still rather trivial) stuff like

let z = 42_f64;

let am_i_nan = std::f64::NAN;
z == am_i_nan; //~ERROR doomed comparison with NAN

let am_i_nan = std::f64::NEG_INFINITY / std::f64::INFINITY;
z == am_i_nan; //~ERROR doomed comparison with NAN

does not produce any of the expected errors.

Since the lint is only supposed to work with those standard number representations that have a NaN value (i.e. f32, f64, f128), the number of cases that evaluate to NaN is hopefully not that large and can probably be hard coded.

@killercup
Copy link
Member Author

Oh, or am I misunderstanding the lint? Is this so people write …::is_nan() to be more explicit?

@mcarton
Copy link
Member

mcarton commented Aug 28, 2016

Is this so people write …::is_nan() to be more explicit?

It's not to be more explicit, it's just that foo == NAN does not work, NaN equals nothing, not even itself.

@killercup
Copy link
Member Author

NaN equals nothing, not even itself.

Doh! I totally knew that… I'll grab a non-alcoholic drink next.

@Lokathor
Copy link

So... should we close this?

@flip1995
Copy link
Member

This lint could still be improved, by also evaluating consts: Playground

const MY_NAN: f64 = std::f64::NAN;

fn main() {
    let x = 3.0f64;
    
    if x == MY_NAN { // This currently doesn't get linted
        println!("YaY");
    } else if x == std::f64::NAN {
        println!("NaY")
    }
}

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages T-MIR Type: This lint will require working with the MIR labels Jul 16, 2019
@bors bors closed this as completed in d7c7056 Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy T-MIR Type: This lint will require working with the MIR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants