Skip to content

Commit 460d5a3

Browse files
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.
1 parent eb0408e commit 460d5a3

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

clippy_lints/src/misc.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
343343
ExprKind::Binary(ref cmp, ref left, ref right) => {
344344
let op = cmp.node;
345345
if op.is_comparison() {
346-
check_nan(cx, left, expr.span);
347-
check_nan(cx, right, expr.span);
346+
check_nan(cx, left, expr);
347+
check_nan(cx, right, expr);
348348
check_to_owned(cx, left, right);
349349
check_to_owned(cx, right, left);
350350
}
@@ -440,21 +440,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
440440
}
441441
}
442442

443-
fn check_nan(cx: &LateContext<'_, '_>, expr: &Expr, cmp_span: Span) {
444-
if let Some((value, _)) = constant(cx, cx.tables, expr) {
445-
let needs_lint = match value {
446-
Constant::F32(num) => num.is_nan(),
447-
Constant::F64(num) => num.is_nan(),
448-
_ => false,
449-
};
443+
fn check_nan(cx: &LateContext<'_, '_>, expr: &Expr, cmp_expr: &Expr) {
444+
if_chain! {
445+
if !in_constant(cx, cmp_expr.hir_id);
446+
if let Some((value, _)) = constant(cx, cx.tables, expr);
447+
then {
448+
let needs_lint = match value {
449+
Constant::F32(num) => num.is_nan(),
450+
Constant::F64(num) => num.is_nan(),
451+
_ => false,
452+
};
450453

451-
if needs_lint {
452-
span_lint(
453-
cx,
454-
CMP_NAN,
455-
cmp_span,
456-
"doomed comparison with NAN, use `std::{f32,f64}::is_nan()` instead",
457-
);
454+
if needs_lint {
455+
span_lint(
456+
cx,
457+
CMP_NAN,
458+
cmp_expr.span,
459+
"doomed comparison with NAN, use `std::{f32,f64}::is_nan()` instead",
460+
);
461+
}
458462
}
459463
}
460464
}

0 commit comments

Comments
 (0)