Skip to content

Commit b71f2be

Browse files
committed
Avoid invalid NaN lint machine-applicable suggestion in const context
1 parent c435af0 commit b71f2be

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

compiler/rustc_lint/src/lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ pub enum InvalidNanComparisons {
14671467
#[diag(lint_invalid_nan_comparisons_eq_ne)]
14681468
EqNe {
14691469
#[subdiagnostic]
1470-
suggestion: InvalidNanComparisonsSuggestion,
1470+
suggestion: Option<InvalidNanComparisonsSuggestion>,
14711471
},
14721472
#[diag(lint_invalid_nan_comparisons_lt_le_gt_ge)]
14731473
LtLeGtGe,

compiler/rustc_lint/src/types.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -572,32 +572,35 @@ fn lint_nan<'tcx>(
572572
}
573573

574574
fn eq_ne(
575+
cx: &LateContext<'_>,
575576
e: &hir::Expr<'_>,
576577
l: &hir::Expr<'_>,
577578
r: &hir::Expr<'_>,
578579
f: impl FnOnce(Span, Span) -> InvalidNanComparisonsSuggestion,
579580
) -> InvalidNanComparisons {
580-
let suggestion =
581+
let suggestion = (!cx.tcx.hir().is_inside_const_context(e.hir_id)).then(|| {
581582
if let Some(l_span) = l.span.find_ancestor_inside(e.span) &&
582-
let Some(r_span) = r.span.find_ancestor_inside(e.span) {
583+
let Some(r_span) = r.span.find_ancestor_inside(e.span)
584+
{
583585
f(l_span, r_span)
584586
} else {
585587
InvalidNanComparisonsSuggestion::Spanless
586-
};
588+
}
589+
});
587590

588591
InvalidNanComparisons::EqNe { suggestion }
589592
}
590593

591594
let lint = match binop.node {
592595
hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, l) => {
593-
eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
596+
eq_ne(cx, e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
594597
nan_plus_binop: l_span.until(r_span),
595598
float: r_span.shrink_to_hi(),
596599
neg: (binop.node == hir::BinOpKind::Ne).then(|| r_span.shrink_to_lo()),
597600
})
598601
}
599602
hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, r) => {
600-
eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
603+
eq_ne(cx, e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
601604
nan_plus_binop: l_span.shrink_to_hi().to(r_span),
602605
float: l_span.shrink_to_hi(),
603606
neg: (binop.node == hir::BinOpKind::Ne).then(|| l_span.shrink_to_lo()),

tests/ui/lint/invalid-nan-comparison.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ LL | const TEST: bool = 5f32 == f32::NAN;
55
| ^^^^^^^^^^^^^^^^
66
|
77
= note: `#[warn(invalid_nan_comparisons)]` on by default
8-
help: use `f32::is_nan()` or `f64::is_nan()` instead
9-
|
10-
LL - const TEST: bool = 5f32 == f32::NAN;
11-
LL + const TEST: bool = 5f32.is_nan();
12-
|
138

149
warning: incorrect NaN comparison, NaN cannot be directly compared to itself
1510
--> $DIR/invalid-nan-comparison.rs:14:5

0 commit comments

Comments
 (0)