Skip to content

Commit 1305a43

Browse files
authored
Rollup merge of #114486 - Urgau:const-context-nan-suggestion-114471, r=compiler-errors
Avoid invalid NaN lint machine-applicable suggestion in const context This PR removes the machine-applicable suggestion in const context for the `invalid_nan_comparision` lint ~~and replace it with a simple help~~. Fixes #114471
2 parents 8236f63 + 3b3e466 commit 1305a43

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

compiler/rustc_lint/src/lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ pub enum InvalidNanComparisons {
14961496
#[diag(lint_invalid_nan_comparisons_eq_ne)]
14971497
EqNe {
14981498
#[subdiagnostic]
1499-
suggestion: InvalidNanComparisonsSuggestion,
1499+
suggestion: Option<InvalidNanComparisonsSuggestion>,
15001500
},
15011501
#[diag(lint_invalid_nan_comparisons_lt_le_gt_ge)]
15021502
LtLeGtGe,

compiler/rustc_lint/src/types.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -572,32 +572,36 @@ 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+
// FIXME(#72505): This suggestion can be restored if `f{32,64}::is_nan` is made const.
582+
let suggestion = (!cx.tcx.hir().is_inside_const_context(e.hir_id)).then(|| {
581583
if let Some(l_span) = l.span.find_ancestor_inside(e.span) &&
582-
let Some(r_span) = r.span.find_ancestor_inside(e.span) {
584+
let Some(r_span) = r.span.find_ancestor_inside(e.span)
585+
{
583586
f(l_span, r_span)
584587
} else {
585588
InvalidNanComparisonsSuggestion::Spanless
586-
};
589+
}
590+
});
587591

588592
InvalidNanComparisons::EqNe { suggestion }
589593
}
590594

591595
let lint = match binop.node {
592596
hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, l) => {
593-
eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
597+
eq_ne(cx, e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
594598
nan_plus_binop: l_span.until(r_span),
595599
float: r_span.shrink_to_hi(),
596600
neg: (binop.node == hir::BinOpKind::Ne).then(|| r_span.shrink_to_lo()),
597601
})
598602
}
599603
hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, r) => {
600-
eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
604+
eq_ne(cx, e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
601605
nan_plus_binop: l_span.shrink_to_hi().to(r_span),
602606
float: l_span.shrink_to_hi(),
603607
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)