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.
This commit is contained in:
Krishna Veera Reddy 2019-12-17 19:18:42 -08:00
parent eb0408ea65
commit 460d5a3b5a

View File

@ -343,8 +343,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
ExprKind::Binary(ref cmp, ref left, ref right) => { ExprKind::Binary(ref cmp, ref left, ref right) => {
let op = cmp.node; let op = cmp.node;
if op.is_comparison() { if op.is_comparison() {
check_nan(cx, left, expr.span); check_nan(cx, left, expr);
check_nan(cx, right, expr.span); check_nan(cx, right, expr);
check_to_owned(cx, left, right); check_to_owned(cx, left, right);
check_to_owned(cx, right, left); 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) { fn check_nan(cx: &LateContext<'_, '_>, expr: &Expr, cmp_expr: &Expr) {
if let Some((value, _)) = constant(cx, cx.tables, expr) { if_chain! {
let needs_lint = match value { if !in_constant(cx, cmp_expr.hir_id);
Constant::F32(num) => num.is_nan(), if let Some((value, _)) = constant(cx, cx.tables, expr);
Constant::F64(num) => num.is_nan(), then {
_ => false, let needs_lint = match value {
}; Constant::F32(num) => num.is_nan(),
Constant::F64(num) => num.is_nan(),
_ => false,
};
if needs_lint { if needs_lint {
span_lint( span_lint(
cx, cx,
CMP_NAN, CMP_NAN,
cmp_span, cmp_expr.span,
"doomed comparison with NAN, use `std::{f32,f64}::is_nan()` instead", "doomed comparison with NAN, use `std::{f32,f64}::is_nan()` instead",
); );
}
} }
} }
} }