about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKrishna Veera Reddy <veerareddy@email.arizona.edu>2019-12-17 19:18:42 -0800
committerKrishna Veera Reddy <veerareddy@email.arizona.edu>2019-12-17 19:18:42 -0800
commit460d5a3b5a0c657bce8d00661b1546e78cafe2b1 (patch)
tree9748b6f96950dea8b548a9103498b426e91db641
parenteb0408ea65d9e93884269fec502bac1966b1565f (diff)
downloadrust-460d5a3b5a0c657bce8d00661b1546e78cafe2b1.tar.gz
rust-460d5a3b5a0c657bce8d00661b1546e78cafe2b1.zip
Prevent `cmp_nan` when inside constants
`std::{f32,f64}::is_nan` isn't a const fn so prevent `cmp_nan`
lint from running within constant comparisons.
-rw-r--r--clippy_lints/src/misc.rs36
1 files changed, 20 insertions, 16 deletions
diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs
index 434acfe66a4..687776c6798 100644
--- a/clippy_lints/src/misc.rs
+++ b/clippy_lints/src/misc.rs
@@ -343,8 +343,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
             ExprKind::Binary(ref cmp, ref left, ref right) => {
                 let op = cmp.node;
                 if op.is_comparison() {
-                    check_nan(cx, left, expr.span);
-                    check_nan(cx, right, expr.span);
+                    check_nan(cx, left, expr);
+                    check_nan(cx, right, expr);
                     check_to_owned(cx, left, right);
                     check_to_owned(cx, right, left);
                 }
@@ -440,21 +440,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
     }
 }
 
-fn check_nan(cx: &LateContext<'_, '_>, expr: &Expr, cmp_span: Span) {
-    if let Some((value, _)) = constant(cx, cx.tables, expr) {
-        let needs_lint = match value {
-            Constant::F32(num) => num.is_nan(),
-            Constant::F64(num) => num.is_nan(),
-            _ => false,
-        };
+fn check_nan(cx: &LateContext<'_, '_>, expr: &Expr, cmp_expr: &Expr) {
+    if_chain! {
+        if !in_constant(cx, cmp_expr.hir_id);
+        if let Some((value, _)) = constant(cx, cx.tables, expr);
+        then {
+            let needs_lint = match value {
+                Constant::F32(num) => num.is_nan(),
+                Constant::F64(num) => num.is_nan(),
+                _ => false,
+            };
 
-        if needs_lint {
-            span_lint(
-                cx,
-                CMP_NAN,
-                cmp_span,
-                "doomed comparison with NAN, use `std::{f32,f64}::is_nan()` instead",
-            );
+            if needs_lint {
+                span_lint(
+                    cx,
+                    CMP_NAN,
+                    cmp_expr.span,
+                    "doomed comparison with NAN, use `std::{f32,f64}::is_nan()` instead",
+                );
+            }
         }
     }
 }