]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/operators/cmp_nan.rs
Merge branch 'master' of http://localhost:8000/rust-lang/rust.git:at_commit=75dd959a3...
[rust.git] / src / tools / clippy / clippy_lints / src / operators / cmp_nan.rs
1 use clippy_utils::consts::{constant, Constant};
2 use clippy_utils::diagnostics::span_lint;
3 use clippy_utils::in_constant;
4 use rustc_hir::{BinOpKind, Expr};
5 use rustc_lint::LateContext;
6
7 use super::CMP_NAN;
8
9 pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, op: BinOpKind, lhs: &Expr<'_>, rhs: &Expr<'_>) {
10     if op.is_comparison() && !in_constant(cx, e.hir_id) && (is_nan(cx, lhs) || is_nan(cx, rhs)) {
11         span_lint(
12             cx,
13             CMP_NAN,
14             e.span,
15             "doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead",
16         );
17     }
18 }
19
20 fn is_nan(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
21     if let Some((value, _)) = constant(cx, cx.typeck_results(), e) {
22         match value {
23             Constant::F32(num) => num.is_nan(),
24             Constant::F64(num) => num.is_nan(),
25             _ => false,
26         }
27     } else {
28         false
29     }
30 }