From 6fc1dc1bbfd90a36ba80662c73378054a632a59c Mon Sep 17 00:00:00 2001 From: Yu Date: Tue, 13 Oct 2026 21:03:02 +0800 Subject: [PATCH 1/7] lint: suggest fXX::is_nan for unsuffixed NaN comparisons --- compiler/rustc_lint/src/diagnostics.rs | 16 ++ compiler/rustc_lint/src/types.rs | 163 ++++++++++++++---- .../invalid-nan-comparison-suggestion.fixed | 42 +++++ .../lint/invalid-nan-comparison-suggestion.rs | 42 +++++ .../invalid-nan-comparison-suggestion.stderr | 114 +++++++++++- 5 files changed, 336 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_lint/src/diagnostics.rs b/compiler/rustc_lint/src/diagnostics.rs index 601f1213650e1..cdb962a12d111 100644 --- a/compiler/rustc_lint/src/diagnostics.rs +++ b/compiler/rustc_lint/src/diagnostics.rs @@ -2334,6 +2334,22 @@ pub(crate) enum InvalidNanComparisonsSuggestion { #[suggestion_part(code = "")] nan_plus_binop: Span, }, + #[multipart_suggestion( + "use `f32::is_nan()` or `f64::is_nan()` instead", + style = "verbose", + applicability = "machine-applicable" + )] + Ufcs { + prefix: String, + #[suggestion_part(code = "{prefix}")] + prefix_span: Span, + #[suggestion_part(code = ")")] + close_span: Span, + #[suggestion_part(code = "")] + remove_nan: Span, + #[suggestion_part(code = "")] + remove_borrow: Option, + }, #[help("use `f32::is_nan()` or `f64::is_nan()` instead")] Spanless, } diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 9897e9cbb4f07..7549014dab97a 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -229,58 +229,145 @@ fn lint_nan<'tcx>( l: &'tcx hir::Expr<'tcx>, r: &'tcx hir::Expr<'tcx>, ) { - fn is_nan(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool { + fn nan_ty(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option { let expr = expr.peel_blocks().peel_borrows(); - match expr.kind { - ExprKind::Path(qpath) => { - let Some(def_id) = cx.typeck_results().qpath_res(&qpath, expr.hir_id).opt_def_id() - else { - return false; - }; + let ExprKind::Path(qpath) = expr.kind else { return None }; + let def_id = cx.typeck_results().qpath_res(&qpath, expr.hir_id).opt_def_id()?; + if !matches!( + cx.tcx.get_diagnostic_name(def_id), + Some(sym::f16_nan | sym::f32_nan | sym::f64_nan | sym::f128_nan) + ) { + return None; + } + match cx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip().kind() { + ty::Float(fty) => Some(*fty), + _ => None, + } + } - matches!( - cx.tcx.get_diagnostic_name(def_id), - Some(sym::f16_nan | sym::f32_nan | sym::f64_nan | sym::f128_nan) - ) - } - _ => false, + fn is_primitive_float(mut ty: &hir::Ty<'_>) -> bool { + while let hir::TyKind::Ref(_, inner, _) = ty.kind { + ty = inner; } + matches!( + ty.kind, + hir::TyKind::Path(hir::QPath::Resolved(_, path)) + if matches!(path.res, hir::def::Res::PrimTy(hir::PrimTy::Float(_))) + ) } - fn eq_ne( - e: &hir::Expr<'_>, - l: &hir::Expr<'_>, - r: &hir::Expr<'_>, - f: impl FnOnce(Span, Span) -> InvalidNanComparisonsSuggestion, - ) -> InvalidNanComparisons { - let suggestion = if let Some(l_span) = l.span.find_ancestor_inside(e.span) - && let Some(r_span) = r.span.find_ancestor_inside(e.span) + // `true` when `expr.is_nan()` still type-checks after this comparison is removed. + // `let [mut] b = &3.3; b != &f64::NAN` is `&f64` only because of that comparison, so + // `b.is_nan()` leaves `{float}` and does not compile. `let b: f64 = 3.3` does. + fn can_suggest_is_nan_method<'a>(cx: &LateContext<'a>, expr: &'a hir::Expr<'a>) -> bool { + let mut cursor = expr.peel_blocks().peel_borrows(); + while let ExprKind::Path(qpath) = &cursor.kind + && let hir::def::Res::Local(hir_id) = cx.qpath_res(qpath, cursor.hir_id) + && let hir::Node::LetStmt(local) = cx.tcx.parent_hir_node(hir_id) + { + if local.ty.is_some_and(is_primitive_float) { + return true; + } + if let hir::PatKind::Binding(..) = local.pat.kind + && let Some(init) = local.init + { + cursor = init.peel_blocks().peel_borrows(); + } else { + break; + } + } + if let ExprKind::Lit(lit) = cursor.kind + && let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node { - f(l_span, r_span) + return false; + } + cx.typeck_results().expr_ty_opt(cursor).is_some_and(|ty| ty.peel_refs().is_floating_point()) + } + + fn eq_ne_suggestion<'a>( + cx: &LateContext<'a>, + cmp: &'a hir::Expr<'a>, + value: &'a hir::Expr<'a>, + nan: &'a hir::Expr<'a>, + float_ty: ty::FloatTy, + is_ne: bool, + ) -> InvalidNanComparisonsSuggestion { + let (Some(value_span), Some(nan_span)) = + (value.span.find_ancestor_inside(cmp.span), nan.span.find_ancestor_inside(cmp.span)) + else { + return InvalidNanComparisonsSuggestion::Spanless; + }; + let remove_nan = if nan_span.lo() < value_span.lo() { + nan_span.until(value_span) } else { - InvalidNanComparisonsSuggestion::Spanless + value_span.shrink_to_hi().to(nan_span) }; - InvalidNanComparisons::EqNe { suggestion } + if can_suggest_is_nan_method(cx, value) { + return InvalidNanComparisonsSuggestion::Spanful { + neg: is_ne.then(|| value_span.shrink_to_lo()), + float: value_span.shrink_to_hi(), + nan_plus_binop: remove_nan, + }; + } + + let Some(mut ty) = cx.typeck_results().expr_ty_opt(value) else { + return InvalidNanComparisonsSuggestion::Spanless; + }; + let mut operand = value.peel_blocks(); + let Some(mut operand_span) = operand.span.find_ancestor_inside(value_span) else { + return InvalidNanComparisonsSuggestion::Spanless; + }; + let outer_span = operand_span; + let mut deref_count = 0; + while let ty::Ref(_, inner, _) = *ty.kind() { + if let ExprKind::AddrOf(_, _, inner_expr) = operand.kind + && let peeled = inner_expr.peel_blocks() + && let Some(peeled_span) = peeled.span.find_ancestor_inside(operand_span) + { + operand = peeled; + operand_span = peeled_span; + } else { + deref_count += 1; + } + ty = inner; + } + let matches_float = match *ty.kind() { + ty::Float(fty) => fty == float_ty, + ty::Infer(ty::FloatVar(_)) => true, + _ => false, + }; + if !matches_float { + return InvalidNanComparisonsSuggestion::Spanless; + } + let written_borrow = outer_span.until(operand_span); + let neg = if is_ne { "!" } else { "" }; + InvalidNanComparisonsSuggestion::Ufcs { + prefix: format!("{neg}{}::is_nan({}", float_ty.name_str(), "*".repeat(deref_count)), + prefix_span: operand_span.shrink_to_lo(), + close_span: operand_span.shrink_to_hi(), + remove_nan, + remove_borrow: (!written_borrow.is_empty()).then_some(written_borrow), + } } + let l_nan = nan_ty(cx, l); + let r_nan = nan_ty(cx, r); + let is_ne = binop == hir::BinOpKind::Ne; let lint = match binop { - hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, l) => { - eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful { - nan_plus_binop: l_span.until(r_span), - float: r_span.shrink_to_hi(), - neg: (binop == hir::BinOpKind::Ne).then(|| r_span.shrink_to_lo()), - }) - } - hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, r) => { - eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful { - nan_plus_binop: l_span.shrink_to_hi().to(r_span), - float: l_span.shrink_to_hi(), - neg: (binop == hir::BinOpKind::Ne).then(|| l_span.shrink_to_lo()), - }) + hir::BinOpKind::Eq | hir::BinOpKind::Ne => { + let Some((value, nan, float_ty)) = l_nan + .map(|float_ty| (r, l, float_ty)) + .or_else(|| r_nan.map(|float_ty| (l, r, float_ty))) + else { + return; + }; + InvalidNanComparisons::EqNe { + suggestion: eq_ne_suggestion(cx, e, value, nan, float_ty, is_ne), + } } hir::BinOpKind::Lt | hir::BinOpKind::Le | hir::BinOpKind::Gt | hir::BinOpKind::Ge - if is_nan(cx, l) || is_nan(cx, r) => + if l_nan.is_some() || r_nan.is_some() => { InvalidNanComparisons::LtLeGtGe } diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.fixed b/tests/ui/lint/invalid-nan-comparison-suggestion.fixed index 2d88c274080b6..39d2ba3e93788 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.fixed +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.fixed @@ -39,6 +39,38 @@ fn main() { let _ = !b.is_nan(); + // Issue #164143: an unsuffixed float is constrained by the `NAN` operand. + // Suggesting `.is_nan()` drops that constraint and does not compile. + let b = &3.3; + if !f64::is_nan(*b) {} + //~^ WARN incorrect NaN comparison + + let mut m = &3.3; + if !f64::is_nan(*m) { + //~^ WARN incorrect NaN comparison + m = &1.0; + } + let _ = m; + + let _ = f64::is_nan(3.3); + //~^ WARN incorrect NaN comparison + + let _ = !f64::is_nan(3.3); + //~^ WARN incorrect NaN comparison + + let suffixed = 3.3f64; + let copied = suffixed; + let _ = !copied.is_nan(); + //~^ WARN incorrect NaN comparison + + let ascribed: f64 = 3.3; + let _ = !ascribed.is_nan(); + //~^ WARN incorrect NaN comparison + + parameter(1.0); + parameter_ref(&1.0); + let _ = |x: f64| !x.is_nan(); //~ WARN incorrect NaN comparison + #[allow(unused_macros)] macro_rules! nan { () => { f32::NAN }; } macro_rules! number { () => { 5f32 }; } @@ -48,3 +80,13 @@ fn main() { let _ = !number!().is_nan(); //~^ WARN incorrect NaN comparison } + +fn parameter(x: f64) { + let _ = x.is_nan(); + //~^ WARN incorrect NaN comparison +} + +fn parameter_ref(x: &f32) { + if !x.is_nan() {} + //~^ WARN incorrect NaN comparison +} diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.rs b/tests/ui/lint/invalid-nan-comparison-suggestion.rs index 9175344786948..18e4a4093d10f 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.rs +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.rs @@ -42,6 +42,38 @@ fn main() { &f32::NAN }; + // Issue #164143: an unsuffixed float is constrained by the `NAN` operand. + // Suggesting `.is_nan()` drops that constraint and does not compile. + let b = &3.3; + if b != &f64::NAN {} + //~^ WARN incorrect NaN comparison + + let mut m = &3.3; + if m != &f64::NAN { + //~^ WARN incorrect NaN comparison + m = &1.0; + } + let _ = m; + + let _ = 3.3 == f64::NAN; + //~^ WARN incorrect NaN comparison + + let _ = &3.3 != &f64::NAN; + //~^ WARN incorrect NaN comparison + + let suffixed = 3.3f64; + let copied = suffixed; + let _ = copied != f64::NAN; + //~^ WARN incorrect NaN comparison + + let ascribed: f64 = 3.3; + let _ = ascribed != f64::NAN; + //~^ WARN incorrect NaN comparison + + parameter(1.0); + parameter_ref(&1.0); + let _ = |x: f64| x != f64::NAN; //~ WARN incorrect NaN comparison + #[allow(unused_macros)] macro_rules! nan { () => { f32::NAN }; } macro_rules! number { () => { 5f32 }; } @@ -51,3 +83,13 @@ fn main() { let _ = number!() != nan!(); //~^ WARN incorrect NaN comparison } + +fn parameter(x: f64) { + let _ = x == f64::NAN; + //~^ WARN incorrect NaN comparison +} + +fn parameter_ref(x: &f32) { + if x != &f32::NAN {} + //~^ WARN incorrect NaN comparison +} diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr b/tests/ui/lint/invalid-nan-comparison-suggestion.stderr index f9b1428de464c..5252977148e09 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.stderr @@ -138,7 +138,91 @@ LL + !b.is_nan(); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:49:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:48:8 + | +LL | if b != &f64::NAN {} + | ^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if b != &f64::NAN {} +LL + if !f64::is_nan(*b) {} + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:52:8 + | +LL | if m != &f64::NAN { + | ^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if m != &f64::NAN { +LL + if !f64::is_nan(*m) { + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:58:13 + | +LL | let _ = 3.3 == f64::NAN; + | ^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - let _ = 3.3 == f64::NAN; +LL + let _ = f64::is_nan(3.3); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:61:13 + | +LL | let _ = &3.3 != &f64::NAN; + | ^^^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - let _ = &3.3 != &f64::NAN; +LL + let _ = !f64::is_nan(3.3); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:66:13 + | +LL | let _ = copied != f64::NAN; + | ^^^^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - let _ = copied != f64::NAN; +LL + let _ = !copied.is_nan(); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:70:13 + | +LL | let _ = ascribed != f64::NAN; + | ^^^^^^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - let _ = ascribed != f64::NAN; +LL + let _ = !ascribed.is_nan(); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:75:22 + | +LL | let _ = |x: f64| x != f64::NAN; + | ^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - let _ = |x: f64| x != f64::NAN; +LL + let _ = |x: f64| !x.is_nan(); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:81:13 | LL | let _ = nan!() == number!(); | ^^^^^^^^^^^^^^^^^^^ @@ -150,7 +234,7 @@ LL + let _ = number!().is_nan(); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:51:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:83:13 | LL | let _ = number!() != nan!(); | ^^^^^^^^^^^^^^^^^^^ @@ -161,5 +245,29 @@ LL - let _ = number!() != nan!(); LL + let _ = !number!().is_nan(); | -warning: 13 warnings emitted +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:88:13 + | +LL | let _ = x == f64::NAN; + | ^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - let _ = x == f64::NAN; +LL + let _ = x.is_nan(); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:93:8 + | +LL | if x != &f32::NAN {} + | ^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if x != &f32::NAN {} +LL + if !x.is_nan() {} + | + +warning: 22 warnings emitted From 4f9916a7b7dba2794fa0825f778a6228586ba787 Mon Sep 17 00:00:00 2001 From: Yu Date: Tue, 13 Oct 2026 23:49:26 +0800 Subject: [PATCH 2/7] lint: use UFCS for NaN comparison suggestions --- compiler/rustc_lint/src/diagnostics.rs | 13 --- compiler/rustc_lint/src/types.rs | 47 -------- .../invalid-nan-comparison-suggestion.fixed | 52 ++++++--- .../lint/invalid-nan-comparison-suggestion.rs | 16 +++ .../invalid-nan-comparison-suggestion.stderr | 104 +++++++++++++----- tests/ui/lint/invalid-nan-comparison.stderr | 34 +++--- 6 files changed, 143 insertions(+), 123 deletions(-) diff --git a/compiler/rustc_lint/src/diagnostics.rs b/compiler/rustc_lint/src/diagnostics.rs index cdb962a12d111..ea36a485fc3ac 100644 --- a/compiler/rustc_lint/src/diagnostics.rs +++ b/compiler/rustc_lint/src/diagnostics.rs @@ -2321,19 +2321,6 @@ pub(crate) enum InvalidNanComparisons { #[derive(Subdiagnostic)] pub(crate) enum InvalidNanComparisonsSuggestion { - #[multipart_suggestion( - "use `f32::is_nan()` or `f64::is_nan()` instead", - style = "verbose", - applicability = "machine-applicable" - )] - Spanful { - #[suggestion_part(code = "!")] - neg: Option, - #[suggestion_part(code = ".is_nan()")] - float: Span, - #[suggestion_part(code = "")] - nan_plus_binop: Span, - }, #[multipart_suggestion( "use `f32::is_nan()` or `f64::is_nan()` instead", style = "verbose", diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 7549014dab97a..4769cb7bf485c 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -245,45 +245,6 @@ fn lint_nan<'tcx>( } } - fn is_primitive_float(mut ty: &hir::Ty<'_>) -> bool { - while let hir::TyKind::Ref(_, inner, _) = ty.kind { - ty = inner; - } - matches!( - ty.kind, - hir::TyKind::Path(hir::QPath::Resolved(_, path)) - if matches!(path.res, hir::def::Res::PrimTy(hir::PrimTy::Float(_))) - ) - } - - // `true` when `expr.is_nan()` still type-checks after this comparison is removed. - // `let [mut] b = &3.3; b != &f64::NAN` is `&f64` only because of that comparison, so - // `b.is_nan()` leaves `{float}` and does not compile. `let b: f64 = 3.3` does. - fn can_suggest_is_nan_method<'a>(cx: &LateContext<'a>, expr: &'a hir::Expr<'a>) -> bool { - let mut cursor = expr.peel_blocks().peel_borrows(); - while let ExprKind::Path(qpath) = &cursor.kind - && let hir::def::Res::Local(hir_id) = cx.qpath_res(qpath, cursor.hir_id) - && let hir::Node::LetStmt(local) = cx.tcx.parent_hir_node(hir_id) - { - if local.ty.is_some_and(is_primitive_float) { - return true; - } - if let hir::PatKind::Binding(..) = local.pat.kind - && let Some(init) = local.init - { - cursor = init.peel_blocks().peel_borrows(); - } else { - break; - } - } - if let ExprKind::Lit(lit) = cursor.kind - && let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node - { - return false; - } - cx.typeck_results().expr_ty_opt(cursor).is_some_and(|ty| ty.peel_refs().is_floating_point()) - } - fn eq_ne_suggestion<'a>( cx: &LateContext<'a>, cmp: &'a hir::Expr<'a>, @@ -303,14 +264,6 @@ fn lint_nan<'tcx>( value_span.shrink_to_hi().to(nan_span) }; - if can_suggest_is_nan_method(cx, value) { - return InvalidNanComparisonsSuggestion::Spanful { - neg: is_ne.then(|| value_span.shrink_to_lo()), - float: value_span.shrink_to_hi(), - nan_plus_binop: remove_nan, - }; - } - let Some(mut ty) = cx.typeck_results().expr_ty_opt(value) else { return InvalidNanComparisonsSuggestion::Spanless; }; diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.fixed b/tests/ui/lint/invalid-nan-comparison-suggestion.fixed index 39d2ba3e93788..857ccf152fb4e 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.fixed +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.fixed @@ -5,39 +5,39 @@ fn main() { let x = 5f16; - let _ = x.is_nan(); + let _ = f16::is_nan(x); //~^ WARN incorrect NaN comparison - let _ = !x.is_nan(); + let _ = !f16::is_nan(x); //~^ WARN incorrect NaN comparison let x = 5f32; - let _ = x.is_nan(); + let _ = f32::is_nan(x); //~^ WARN incorrect NaN comparison - let _ = !x.is_nan(); + let _ = !f32::is_nan(x); //~^ WARN incorrect NaN comparison let x = 5f64; - let _ = x.is_nan(); + let _ = f64::is_nan(x); //~^ WARN incorrect NaN comparison - let _ = !x.is_nan(); + let _ = !f64::is_nan(x); //~^ WARN incorrect NaN comparison let x = 5f128; - let _ = x.is_nan(); + let _ = f128::is_nan(x); //~^ WARN incorrect NaN comparison - let _ = !x.is_nan(); + let _ = !f128::is_nan(x); //~^ WARN incorrect NaN comparison let b = &2.3f32; - if !b.is_nan() {} + if !f32::is_nan(*b) {} //~^ WARN incorrect NaN comparison let b = &2.3f32; - if !b.is_nan() {} + if !f32::is_nan(*b) {} //~^ WARN incorrect NaN comparison let _ = - !b.is_nan(); + !f32::is_nan(*b); // Issue #164143: an unsuffixed float is constrained by the `NAN` operand. // Suggesting `.is_nan()` drops that constraint and does not compile. @@ -52,6 +52,22 @@ fn main() { } let _ = m; + let unary = &(-3.3); + if !f64::is_nan(*unary) {} + //~^ WARN incorrect NaN comparison + + let block = &{ 3.3 }; + if !f64::is_nan(*block) {} + //~^ WARN incorrect NaN comparison + + let (tuple,) = (&3.3,); + if !f64::is_nan(*tuple) {} + //~^ WARN incorrect NaN comparison + + let arithmetic = &(3.3 + 0.0); + if !f64::is_nan(*arithmetic) {} + //~^ WARN incorrect NaN comparison + let _ = f64::is_nan(3.3); //~^ WARN incorrect NaN comparison @@ -60,33 +76,33 @@ fn main() { let suffixed = 3.3f64; let copied = suffixed; - let _ = !copied.is_nan(); + let _ = !f64::is_nan(copied); //~^ WARN incorrect NaN comparison let ascribed: f64 = 3.3; - let _ = !ascribed.is_nan(); + let _ = !f64::is_nan(ascribed); //~^ WARN incorrect NaN comparison parameter(1.0); parameter_ref(&1.0); - let _ = |x: f64| !x.is_nan(); //~ WARN incorrect NaN comparison + let _ = |x: f64| !f64::is_nan(x); //~ WARN incorrect NaN comparison #[allow(unused_macros)] macro_rules! nan { () => { f32::NAN }; } macro_rules! number { () => { 5f32 }; } - let _ = number!().is_nan(); + let _ = f32::is_nan(number!()); //~^ WARN incorrect NaN comparison - let _ = !number!().is_nan(); + let _ = !f32::is_nan(number!()); //~^ WARN incorrect NaN comparison } fn parameter(x: f64) { - let _ = x.is_nan(); + let _ = f64::is_nan(x); //~^ WARN incorrect NaN comparison } fn parameter_ref(x: &f32) { - if !x.is_nan() {} + if !f32::is_nan(*x) {} //~^ WARN incorrect NaN comparison } diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.rs b/tests/ui/lint/invalid-nan-comparison-suggestion.rs index 18e4a4093d10f..44979955c444e 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.rs +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.rs @@ -55,6 +55,22 @@ fn main() { } let _ = m; + let unary = &(-3.3); + if unary != &f64::NAN {} + //~^ WARN incorrect NaN comparison + + let block = &{ 3.3 }; + if block != &f64::NAN {} + //~^ WARN incorrect NaN comparison + + let (tuple,) = (&3.3,); + if tuple != &f64::NAN {} + //~^ WARN incorrect NaN comparison + + let arithmetic = &(3.3 + 0.0); + if arithmetic != &f64::NAN {} + //~^ WARN incorrect NaN comparison + let _ = 3.3 == f64::NAN; //~^ WARN incorrect NaN comparison diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr b/tests/ui/lint/invalid-nan-comparison-suggestion.stderr index 5252977148e09..1b58a4e97a97a 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.stderr @@ -8,7 +8,7 @@ LL | let _ = x == f16::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = x == f16::NAN; -LL + let _ = x.is_nan(); +LL + let _ = f16::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -20,7 +20,7 @@ LL | let _ = x != f16::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = x != f16::NAN; -LL + let _ = !x.is_nan(); +LL + let _ = !f16::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -32,7 +32,7 @@ LL | let _ = x == f32::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = x == f32::NAN; -LL + let _ = x.is_nan(); +LL + let _ = f32::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -44,7 +44,7 @@ LL | let _ = x != f32::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = x != f32::NAN; -LL + let _ = !x.is_nan(); +LL + let _ = !f32::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -56,7 +56,7 @@ LL | let _ = x == f64::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = x == f64::NAN; -LL + let _ = x.is_nan(); +LL + let _ = f64::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -68,7 +68,7 @@ LL | let _ = x != f64::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = x != f64::NAN; -LL + let _ = !x.is_nan(); +LL + let _ = !f64::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -80,7 +80,7 @@ LL | let _ = x == f128::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = x == f128::NAN; -LL + let _ = x.is_nan(); +LL + let _ = f128::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -92,7 +92,7 @@ LL | let _ = x != f128::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = x != f128::NAN; -LL + let _ = !x.is_nan(); +LL + let _ = !f128::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -104,7 +104,7 @@ LL | if b != &f32::NAN {} help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - if b != &f32::NAN {} -LL + if !b.is_nan() {} +LL + if !f32::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -116,7 +116,7 @@ LL | if b != { &f32::NAN } {} help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - if b != { &f32::NAN } {} -LL + if !b.is_nan() {} +LL + if !f32::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -134,7 +134,7 @@ LL - b != { LL - LL - &f32::NAN LL - }; -LL + !b.is_nan(); +LL + !f32::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -162,7 +162,55 @@ LL + if !f64::is_nan(*m) { | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:58:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:59:8 + | +LL | if unary != &f64::NAN {} + | ^^^^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if unary != &f64::NAN {} +LL + if !f64::is_nan(*unary) {} + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:63:8 + | +LL | if block != &f64::NAN {} + | ^^^^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if block != &f64::NAN {} +LL + if !f64::is_nan(*block) {} + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:67:8 + | +LL | if tuple != &f64::NAN {} + | ^^^^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if tuple != &f64::NAN {} +LL + if !f64::is_nan(*tuple) {} + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:71:8 + | +LL | if arithmetic != &f64::NAN {} + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if arithmetic != &f64::NAN {} +LL + if !f64::is_nan(*arithmetic) {} + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:74:13 | LL | let _ = 3.3 == f64::NAN; | ^^^^^^^^^^^^^^^ @@ -174,7 +222,7 @@ LL + let _ = f64::is_nan(3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:61:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:77:13 | LL | let _ = &3.3 != &f64::NAN; | ^^^^^^^^^^^^^^^^^ @@ -186,7 +234,7 @@ LL + let _ = !f64::is_nan(3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:66:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:82:13 | LL | let _ = copied != f64::NAN; | ^^^^^^^^^^^^^^^^^^ @@ -194,11 +242,11 @@ LL | let _ = copied != f64::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = copied != f64::NAN; -LL + let _ = !copied.is_nan(); +LL + let _ = !f64::is_nan(copied); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:70:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:86:13 | LL | let _ = ascribed != f64::NAN; | ^^^^^^^^^^^^^^^^^^^^ @@ -206,11 +254,11 @@ LL | let _ = ascribed != f64::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = ascribed != f64::NAN; -LL + let _ = !ascribed.is_nan(); +LL + let _ = !f64::is_nan(ascribed); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:75:22 + --> $DIR/invalid-nan-comparison-suggestion.rs:91:22 | LL | let _ = |x: f64| x != f64::NAN; | ^^^^^^^^^^^^^ @@ -218,11 +266,11 @@ LL | let _ = |x: f64| x != f64::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = |x: f64| x != f64::NAN; -LL + let _ = |x: f64| !x.is_nan(); +LL + let _ = |x: f64| !f64::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:81:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:97:13 | LL | let _ = nan!() == number!(); | ^^^^^^^^^^^^^^^^^^^ @@ -230,11 +278,11 @@ LL | let _ = nan!() == number!(); help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = nan!() == number!(); -LL + let _ = number!().is_nan(); +LL + let _ = f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:83:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:99:13 | LL | let _ = number!() != nan!(); | ^^^^^^^^^^^^^^^^^^^ @@ -242,11 +290,11 @@ LL | let _ = number!() != nan!(); help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = number!() != nan!(); -LL + let _ = !number!().is_nan(); +LL + let _ = !f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:88:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:104:13 | LL | let _ = x == f64::NAN; | ^^^^^^^^^^^^^ @@ -254,11 +302,11 @@ LL | let _ = x == f64::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - let _ = x == f64::NAN; -LL + let _ = x.is_nan(); +LL + let _ = f64::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:93:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:109:8 | LL | if x != &f32::NAN {} | ^^^^^^^^^^^^^^ @@ -266,8 +314,8 @@ LL | if x != &f32::NAN {} help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - if x != &f32::NAN {} -LL + if !x.is_nan() {} +LL + if !f32::is_nan(*x) {} | -warning: 22 warnings emitted +warning: 26 warnings emitted diff --git a/tests/ui/lint/invalid-nan-comparison.stderr b/tests/ui/lint/invalid-nan-comparison.stderr index 486d2a9636c95..4b31abf84ff54 100644 --- a/tests/ui/lint/invalid-nan-comparison.stderr +++ b/tests/ui/lint/invalid-nan-comparison.stderr @@ -8,7 +8,7 @@ LL | const TEST: bool = 5f32 == f32::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - const TEST: bool = 5f32 == f32::NAN; -LL + const TEST: bool = 5f32.is_nan(); +LL + const TEST: bool = f32::is_nan(5f32); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -20,7 +20,7 @@ LL | x == f16::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - x == f16::NAN; -LL + x.is_nan(); +LL + f16::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -32,7 +32,7 @@ LL | x != f16::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - x != f16::NAN; -LL + !x.is_nan(); +LL + !f16::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable @@ -68,7 +68,7 @@ LL | number!() == f16::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - number!() == f16::NAN; -LL + number!().is_nan(); +LL + f16::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -80,7 +80,7 @@ LL | f16::NAN != number!(); help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - f16::NAN != number!(); -LL + !number!().is_nan(); +LL + !f16::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -92,7 +92,7 @@ LL | x == f32::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - x == f32::NAN; -LL + x.is_nan(); +LL + f32::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -104,7 +104,7 @@ LL | x != f32::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - x != f32::NAN; -LL + !x.is_nan(); +LL + !f32::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable @@ -140,7 +140,7 @@ LL | number!() == f32::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - number!() == f32::NAN; -LL + number!().is_nan(); +LL + f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -152,7 +152,7 @@ LL | f32::NAN != number!(); help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - f32::NAN != number!(); -LL + !number!().is_nan(); +LL + !f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -164,7 +164,7 @@ LL | x == f64::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - x == f64::NAN; -LL + x.is_nan(); +LL + f64::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -176,7 +176,7 @@ LL | x != f64::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - x != f64::NAN; -LL + !x.is_nan(); +LL + !f64::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable @@ -212,7 +212,7 @@ LL | number!() == f64::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - number!() == f64::NAN; -LL + number!().is_nan(); +LL + f64::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -224,7 +224,7 @@ LL | f64::NAN != number!(); help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - f64::NAN != number!(); -LL + !number!().is_nan(); +LL + !f64::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -236,7 +236,7 @@ LL | x == f128::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - x == f128::NAN; -LL + x.is_nan(); +LL + f128::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -248,7 +248,7 @@ LL | x != f128::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - x != f128::NAN; -LL + !x.is_nan(); +LL + !f128::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable @@ -284,7 +284,7 @@ LL | number!() == f128::NAN; help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - number!() == f128::NAN; -LL + number!().is_nan(); +LL + f128::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -296,7 +296,7 @@ LL | f128::NAN != number!(); help: use `f32::is_nan()` or `f64::is_nan()` instead | LL - f128::NAN != number!(); -LL + !number!().is_nan(); +LL + !f128::is_nan(number!()); | warning: 33 warnings emitted From ed4718fea5784a076c587b7467737d4e9bf8d133 Mon Sep 17 00:00:00 2001 From: Yu Date: Wed, 14 Oct 2026 01:19:39 +0800 Subject: [PATCH 3/7] lint: preserve qualified paths in NaN suggestions --- compiler/rustc_lint/src/types.rs | 13 +- .../invalid-nan-comparison-suggestion.fixed | 28 +++++ .../lint/invalid-nan-comparison-suggestion.rs | 28 +++++ .../invalid-nan-comparison-suggestion.stderr | 114 +++++++++++++----- 4 files changed, 155 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 4769cb7bf485c..78cf3429116ec 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -245,6 +245,14 @@ fn lint_nan<'tcx>( } } + fn nan_method_path(cx: &LateContext<'_>, nan: &hir::Expr<'_>) -> Option { + let expr = nan.peel_blocks().peel_borrows(); + let path = cx.sess().source_map().span_to_snippet(expr.span).ok()?; + path.strip_suffix("NAN") + .filter(|path| path.contains("::")) + .map(|path| format!("{path}is_nan")) + } + fn eq_ne_suggestion<'a>( cx: &LateContext<'a>, cmp: &'a hir::Expr<'a>, @@ -295,8 +303,11 @@ fn lint_nan<'tcx>( } let written_borrow = outer_span.until(operand_span); let neg = if is_ne { "!" } else { "" }; + let Some(method_path) = nan_method_path(cx, nan) else { + return InvalidNanComparisonsSuggestion::Spanless; + }; InvalidNanComparisonsSuggestion::Ufcs { - prefix: format!("{neg}{}::is_nan({}", float_ty.name_str(), "*".repeat(deref_count)), + prefix: format!("{neg}{method_path}({}", "*".repeat(deref_count)), prefix_span: operand_span.shrink_to_lo(), close_span: operand_span.shrink_to_hi(), remove_nan, diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.fixed b/tests/ui/lint/invalid-nan-comparison-suggestion.fixed index 857ccf152fb4e..8fbdd079053c2 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.fixed +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.fixed @@ -3,6 +3,8 @@ #![feature(f16, f128)] +extern crate core; + fn main() { let x = 5f16; let _ = f16::is_nan(x); @@ -68,6 +70,17 @@ fn main() { if !f64::is_nan(*arithmetic) {} //~^ WARN incorrect NaN comparison + let x = 1.0f64; + if !f64::is_nan(x) {} + //~^ WARN incorrect NaN comparison + let p = &x; + let _ = f64::is_nan(*p); + //~^ WARN incorrect NaN comparison + let _ = f64::is_nan(-x); + //~^ WARN incorrect NaN comparison + + shadowed::check(); + let _ = f64::is_nan(3.3); //~^ WARN incorrect NaN comparison @@ -106,3 +119,18 @@ fn parameter_ref(x: &f32) { if !f32::is_nan(*x) {} //~^ WARN incorrect NaN comparison } + +mod shadowed { + #[allow(dead_code, non_camel_case_types)] + struct f64; + use ::core::primitive::f64 as Float; + + pub fn check() { + let b = &3.3; + if !::core::primitive::f64::is_nan(*b) {} + //~^ WARN incorrect NaN comparison + let c = &4.4; + if !Float::is_nan(*c) {} + //~^ WARN incorrect NaN comparison + } +} diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.rs b/tests/ui/lint/invalid-nan-comparison-suggestion.rs index 44979955c444e..fb23208dfcfc9 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.rs +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.rs @@ -3,6 +3,8 @@ #![feature(f16, f128)] +extern crate core; + fn main() { let x = 5f16; let _ = x == f16::NAN; @@ -71,6 +73,17 @@ fn main() { if arithmetic != &f64::NAN {} //~^ WARN incorrect NaN comparison + let x = 1.0f64; + if &x != &f64::NAN {} + //~^ WARN incorrect NaN comparison + let p = &x; + let _ = *p == f64::NAN; + //~^ WARN incorrect NaN comparison + let _ = -x == f64::NAN; + //~^ WARN incorrect NaN comparison + + shadowed::check(); + let _ = 3.3 == f64::NAN; //~^ WARN incorrect NaN comparison @@ -109,3 +122,18 @@ fn parameter_ref(x: &f32) { if x != &f32::NAN {} //~^ WARN incorrect NaN comparison } + +mod shadowed { + #[allow(dead_code, non_camel_case_types)] + struct f64; + use ::core::primitive::f64 as Float; + + pub fn check() { + let b = &3.3; + if b != &::core::primitive::f64::NAN {} + //~^ WARN incorrect NaN comparison + let c = &4.4; + if c != &Float::NAN {} + //~^ WARN incorrect NaN comparison + } +} diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr b/tests/ui/lint/invalid-nan-comparison-suggestion.stderr index 1b58a4e97a97a..c9f3b6bd95052 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.stderr @@ -1,5 +1,5 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:8:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:10:13 | LL | let _ = x == f16::NAN; | ^^^^^^^^^^^^^ @@ -12,7 +12,7 @@ LL + let _ = f16::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:10:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:12:13 | LL | let _ = x != f16::NAN; | ^^^^^^^^^^^^^ @@ -24,7 +24,7 @@ LL + let _ = !f16::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:14:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:16:13 | LL | let _ = x == f32::NAN; | ^^^^^^^^^^^^^ @@ -36,7 +36,7 @@ LL + let _ = f32::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:16:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:18:13 | LL | let _ = x != f32::NAN; | ^^^^^^^^^^^^^ @@ -48,7 +48,7 @@ LL + let _ = !f32::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:20:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:22:13 | LL | let _ = x == f64::NAN; | ^^^^^^^^^^^^^ @@ -60,7 +60,7 @@ LL + let _ = f64::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:22:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:24:13 | LL | let _ = x != f64::NAN; | ^^^^^^^^^^^^^ @@ -72,7 +72,7 @@ LL + let _ = !f64::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:26:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:28:13 | LL | let _ = x == f128::NAN; | ^^^^^^^^^^^^^^ @@ -84,7 +84,7 @@ LL + let _ = f128::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:28:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:30:13 | LL | let _ = x != f128::NAN; | ^^^^^^^^^^^^^^ @@ -96,7 +96,7 @@ LL + let _ = !f128::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:32:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:34:8 | LL | if b != &f32::NAN {} | ^^^^^^^^^^^^^^ @@ -108,7 +108,7 @@ LL + if !f32::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:36:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:38:8 | LL | if b != { &f32::NAN } {} | ^^^^^^^^^^^^^^^^^^ @@ -120,7 +120,7 @@ LL + if !f32::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:40:9 + --> $DIR/invalid-nan-comparison-suggestion.rs:42:9 | LL | / b != { LL | | @@ -138,7 +138,7 @@ LL + !f32::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:48:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:50:8 | LL | if b != &f64::NAN {} | ^^^^^^^^^^^^^^ @@ -150,7 +150,7 @@ LL + if !f64::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:52:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:54:8 | LL | if m != &f64::NAN { | ^^^^^^^^^^^^^^ @@ -162,7 +162,7 @@ LL + if !f64::is_nan(*m) { | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:59:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:61:8 | LL | if unary != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^ @@ -174,7 +174,7 @@ LL + if !f64::is_nan(*unary) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:63:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:65:8 | LL | if block != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^ @@ -186,7 +186,7 @@ LL + if !f64::is_nan(*block) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:67:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:69:8 | LL | if tuple != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^ @@ -198,7 +198,7 @@ LL + if !f64::is_nan(*tuple) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:71:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:73:8 | LL | if arithmetic != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -210,7 +210,43 @@ LL + if !f64::is_nan(*arithmetic) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:74:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:77:8 + | +LL | if &x != &f64::NAN {} + | ^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if &x != &f64::NAN {} +LL + if !f64::is_nan(x) {} + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:80:13 + | +LL | let _ = *p == f64::NAN; + | ^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - let _ = *p == f64::NAN; +LL + let _ = f64::is_nan(*p); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:82:13 + | +LL | let _ = -x == f64::NAN; + | ^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - let _ = -x == f64::NAN; +LL + let _ = f64::is_nan(-x); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:87:13 | LL | let _ = 3.3 == f64::NAN; | ^^^^^^^^^^^^^^^ @@ -222,7 +258,7 @@ LL + let _ = f64::is_nan(3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:77:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:90:13 | LL | let _ = &3.3 != &f64::NAN; | ^^^^^^^^^^^^^^^^^ @@ -234,7 +270,7 @@ LL + let _ = !f64::is_nan(3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:82:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:95:13 | LL | let _ = copied != f64::NAN; | ^^^^^^^^^^^^^^^^^^ @@ -246,7 +282,7 @@ LL + let _ = !f64::is_nan(copied); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:86:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:99:13 | LL | let _ = ascribed != f64::NAN; | ^^^^^^^^^^^^^^^^^^^^ @@ -258,7 +294,7 @@ LL + let _ = !f64::is_nan(ascribed); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:91:22 + --> $DIR/invalid-nan-comparison-suggestion.rs:104:22 | LL | let _ = |x: f64| x != f64::NAN; | ^^^^^^^^^^^^^ @@ -270,7 +306,7 @@ LL + let _ = |x: f64| !f64::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:97:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:110:13 | LL | let _ = nan!() == number!(); | ^^^^^^^^^^^^^^^^^^^ @@ -282,7 +318,7 @@ LL + let _ = f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:99:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:112:13 | LL | let _ = number!() != nan!(); | ^^^^^^^^^^^^^^^^^^^ @@ -294,7 +330,7 @@ LL + let _ = !f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:104:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:117:13 | LL | let _ = x == f64::NAN; | ^^^^^^^^^^^^^ @@ -306,7 +342,7 @@ LL + let _ = f64::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:109:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:122:8 | LL | if x != &f32::NAN {} | ^^^^^^^^^^^^^^ @@ -317,5 +353,29 @@ LL - if x != &f32::NAN {} LL + if !f32::is_nan(*x) {} | -warning: 26 warnings emitted +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:133:12 + | +LL | if b != &::core::primitive::f64::NAN {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if b != &::core::primitive::f64::NAN {} +LL + if !::core::primitive::f64::is_nan(*b) {} + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:136:12 + | +LL | if c != &Float::NAN {} + | ^^^^^^^^^^^^^^^^ + | +help: use `f32::is_nan()` or `f64::is_nan()` instead + | +LL - if c != &Float::NAN {} +LL + if !Float::is_nan(*c) {} + | + +warning: 31 warnings emitted From 62cf356902c44a2c2362b02b901831d4c8dd9b40 Mon Sep 17 00:00:00 2001 From: Yu Date: Wed, 14 Oct 2026 14:05:14 +0800 Subject: [PATCH 4/7] lint: fix NaN suggestions for nested operands and macros --- compiler/rustc_lint/src/diagnostics.rs | 9 +- compiler/rustc_lint/src/types.rs | 118 ++++--- ...nvalid-nan-comparison-custom-partial-eq.rs | 25 ++ ...id-nan-comparison-custom-partial-eq.stderr | 27 ++ .../lint/invalid-nan-comparison-no-std.fixed | 18 ++ .../ui/lint/invalid-nan-comparison-no-std.rs | 18 ++ .../lint/invalid-nan-comparison-no-std.stderr | 15 + .../invalid-nan-comparison-no-suggestion.rs | 28 ++ ...nvalid-nan-comparison-no-suggestion.stderr | 50 +++ .../invalid-nan-comparison-suggestion.fixed | 113 +++++-- .../lint/invalid-nan-comparison-suggestion.rs | 51 +++ .../invalid-nan-comparison-suggestion.stderr | 304 +++++++++++++----- tests/ui/lint/invalid-nan-comparison.stderr | 68 ++-- 13 files changed, 649 insertions(+), 195 deletions(-) create mode 100644 tests/ui/lint/invalid-nan-comparison-custom-partial-eq.rs create mode 100644 tests/ui/lint/invalid-nan-comparison-custom-partial-eq.stderr create mode 100644 tests/ui/lint/invalid-nan-comparison-no-std.fixed create mode 100644 tests/ui/lint/invalid-nan-comparison-no-std.rs create mode 100644 tests/ui/lint/invalid-nan-comparison-no-std.stderr create mode 100644 tests/ui/lint/invalid-nan-comparison-no-suggestion.rs create mode 100644 tests/ui/lint/invalid-nan-comparison-no-suggestion.stderr diff --git a/compiler/rustc_lint/src/diagnostics.rs b/compiler/rustc_lint/src/diagnostics.rs index ea36a485fc3ac..d66a3c1ca13f3 100644 --- a/compiler/rustc_lint/src/diagnostics.rs +++ b/compiler/rustc_lint/src/diagnostics.rs @@ -2322,7 +2322,7 @@ pub(crate) enum InvalidNanComparisons { #[derive(Subdiagnostic)] pub(crate) enum InvalidNanComparisonsSuggestion { #[multipart_suggestion( - "use `f32::is_nan()` or `f64::is_nan()` instead", + "use the `is_nan` method instead", style = "verbose", applicability = "machine-applicable" )] @@ -2330,14 +2330,13 @@ pub(crate) enum InvalidNanComparisonsSuggestion { prefix: String, #[suggestion_part(code = "{prefix}")] prefix_span: Span, - #[suggestion_part(code = ")")] + suffix: &'static str, + #[suggestion_part(code = "{suffix}")] close_span: Span, #[suggestion_part(code = "")] remove_nan: Span, - #[suggestion_part(code = "")] - remove_borrow: Option, }, - #[help("use `f32::is_nan()` or `f64::is_nan()` instead")] + #[help("use the `is_nan` method instead")] Spanless, } diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 78cf3429116ec..c38e77c5fe6c7 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -2,6 +2,7 @@ use std::iter; use rustc_abi::{BackendRepr, TagEncoding, Variants, WrappingRange}; use rustc_ast as ast; +use rustc_ast::util::parser::ExprPrecedence; use rustc_attr_ir::find_attr; use rustc_attr_ir::lang_items::LangItem; use rustc_hir as hir; @@ -113,8 +114,8 @@ declare_lint! { } declare_lint! { - /// The `invalid_nan_comparisons` lint checks comparison with `f32::NAN` or `f64::NAN` - /// as one of the operand. + /// The `invalid_nan_comparisons` lint checks comparisons with the `NAN` constants + /// of `f16`, `f32`, `f64`, and `f128`. /// /// ### Example /// @@ -127,8 +128,8 @@ declare_lint! { /// /// ### Explanation /// - /// NaN does not compare meaningfully to anything – not - /// even itself – so those comparisons are always false. + /// NaN compares unequal to every floating-point value, including itself. + /// Use the `is_nan` method to check whether a value is NaN. INVALID_NAN_COMPARISONS, Warn, "detects invalid floating point NaN comparisons" @@ -229,28 +230,50 @@ fn lint_nan<'tcx>( l: &'tcx hir::Expr<'tcx>, r: &'tcx hir::Expr<'tcx>, ) { - fn nan_ty(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option { - let expr = expr.peel_blocks().peel_borrows(); + fn nan_ty(cx: &LateContext<'_>, mut expr: &hir::Expr<'_>) -> Option { + loop { + expr = expr.peel_blocks(); + match expr.kind { + ExprKind::AddrOf(.., inner) => expr = inner, + _ => break, + } + } let ExprKind::Path(qpath) = expr.kind else { return None }; let def_id = cx.typeck_results().qpath_res(&qpath, expr.hir_id).opt_def_id()?; - if !matches!( - cx.tcx.get_diagnostic_name(def_id), - Some(sym::f16_nan | sym::f32_nan | sym::f64_nan | sym::f128_nan) - ) { - return None; - } - match cx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip().kind() { - ty::Float(fty) => Some(*fty), - _ => None, - } + Some(match cx.tcx.get_diagnostic_name(def_id)? { + sym::f16_nan => ty::FloatTy::F16, + sym::f32_nan => ty::FloatTy::F32, + sym::f64_nan => ty::FloatTy::F64, + sym::f128_nan => ty::FloatTy::F128, + _ => return None, + }) } - fn nan_method_path(cx: &LateContext<'_>, nan: &hir::Expr<'_>) -> Option { - let expr = nan.peel_blocks().peel_borrows(); - let path = cx.sess().source_map().span_to_snippet(expr.span).ok()?; - path.strip_suffix("NAN") - .filter(|path| path.contains("::")) - .map(|path| format!("{path}is_nan")) + fn nan_method_path( + cx: &LateContext<'_>, + mut nan: &hir::Expr<'_>, + float_ty: ty::FloatTy, + cmp_span: Span, + ) -> Option { + loop { + match nan.kind { + ExprKind::AddrOf(.., inner) => nan = inner, + ExprKind::Block(block, _) if block.stmts.is_empty() => nan = block.expr?, + _ => break, + } + } + let ExprKind::Path(hir::QPath::TypeRelative(ty, _)) = nan.kind else { + return None; + }; + if !ty.span.from_expansion() && cmp_span.contains(ty.span) { + let ty = cx.sess().source_map().span_to_snippet(ty.span).ok()?; + Some(format!("<{ty}>::is_nan")) + } else if matches!(float_ty, ty::FloatTy::F32 | ty::FloatTy::F64) { + let krate = std_or_core(cx)?; + Some(format!("::{krate}::primitive::{}::is_nan", float_ty.name_str())) + } else { + None + } } fn eq_ne_suggestion<'a>( @@ -261,11 +284,18 @@ fn lint_nan<'tcx>( float_ty: ty::FloatTy, is_ne: bool, ) -> InvalidNanComparisonsSuggestion { - let (Some(value_span), Some(nan_span)) = - (value.span.find_ancestor_inside(cmp.span), nan.span.find_ancestor_inside(cmp.span)) - else { + if cmp.span.from_expansion() { + return InvalidNanComparisonsSuggestion::Spanless; + } + let (Some(value_span), Some(nan_span)) = ( + value.span.find_ancestor_inside_same_ctxt(cmp.span), + nan.span.find_ancestor_inside_same_ctxt(cmp.span), + ) else { return InvalidNanComparisonsSuggestion::Spanless; }; + if value_span.overlaps(nan_span) { + return InvalidNanComparisonsSuggestion::Spanless; + } let remove_nan = if nan_span.lo() < value_span.lo() { nan_span.until(value_span) } else { @@ -275,43 +305,29 @@ fn lint_nan<'tcx>( let Some(mut ty) = cx.typeck_results().expr_ty_opt(value) else { return InvalidNanComparisonsSuggestion::Spanless; }; - let mut operand = value.peel_blocks(); - let Some(mut operand_span) = operand.span.find_ancestor_inside(value_span) else { - return InvalidNanComparisonsSuggestion::Spanless; - }; - let outer_span = operand_span; let mut deref_count = 0; while let ty::Ref(_, inner, _) = *ty.kind() { - if let ExprKind::AddrOf(_, _, inner_expr) = operand.kind - && let peeled = inner_expr.peel_blocks() - && let Some(peeled_span) = peeled.span.find_ancestor_inside(operand_span) - { - operand = peeled; - operand_span = peeled_span; - } else { - deref_count += 1; - } + deref_count += 1; ty = inner; } - let matches_float = match *ty.kind() { - ty::Float(fty) => fty == float_ty, - ty::Infer(ty::FloatVar(_)) => true, - _ => false, - }; - if !matches_float { + if !matches!(*ty.kind(), ty::Float(fty) if fty == float_ty) { return InvalidNanComparisonsSuggestion::Spanless; } - let written_borrow = outer_span.until(operand_span); let neg = if is_ne { "!" } else { "" }; - let Some(method_path) = nan_method_path(cx, nan) else { + let Some(method_path) = nan_method_path(cx, nan, float_ty, cmp.span) else { return InvalidNanComparisonsSuggestion::Spanless; }; + let (open, suffix) = if deref_count != 0 && cx.precedence(value) < ExprPrecedence::Prefix { + ("(", "))") + } else { + ("", ")") + }; InvalidNanComparisonsSuggestion::Ufcs { - prefix: format!("{neg}{method_path}({}", "*".repeat(deref_count)), - prefix_span: operand_span.shrink_to_lo(), - close_span: operand_span.shrink_to_hi(), + prefix: format!("{neg}{method_path}({}{open}", "*".repeat(deref_count)), + prefix_span: value_span.shrink_to_lo(), + suffix, + close_span: value_span.shrink_to_hi(), remove_nan, - remove_borrow: (!written_borrow.is_empty()).then_some(written_borrow), } } diff --git a/tests/ui/lint/invalid-nan-comparison-custom-partial-eq.rs b/tests/ui/lint/invalid-nan-comparison-custom-partial-eq.rs new file mode 100644 index 0000000000000..be3351916ddc4 --- /dev/null +++ b/tests/ui/lint/invalid-nan-comparison-custom-partial-eq.rs @@ -0,0 +1,25 @@ +//@ check-pass + +struct NotFloat; + +impl PartialEq for NotFloat { + fn eq(&self, _: &f64) -> bool { + true + } +} + +impl PartialEq for f64 { + fn eq(&self, _: &NotFloat) -> bool { + true + } +} + +fn main() { + let value = NotFloat; + let _ = value == f64::NAN; + //~^ WARN incorrect NaN comparison + let _ = f64::NAN != value; + //~^ WARN incorrect NaN comparison + let _ = &value != &f64::NAN; + //~^ WARN incorrect NaN comparison +} diff --git a/tests/ui/lint/invalid-nan-comparison-custom-partial-eq.stderr b/tests/ui/lint/invalid-nan-comparison-custom-partial-eq.stderr new file mode 100644 index 0000000000000..daf91b092a3bb --- /dev/null +++ b/tests/ui/lint/invalid-nan-comparison-custom-partial-eq.stderr @@ -0,0 +1,27 @@ +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-custom-partial-eq.rs:19:13 + | +LL | let _ = value == f64::NAN; + | ^^^^^^^^^^^^^^^^^ + | + = help: use the `is_nan` method instead + = note: `#[warn(invalid_nan_comparisons)]` on by default + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-custom-partial-eq.rs:21:13 + | +LL | let _ = f64::NAN != value; + | ^^^^^^^^^^^^^^^^^ + | + = help: use the `is_nan` method instead + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-custom-partial-eq.rs:23:13 + | +LL | let _ = &value != &f64::NAN; + | ^^^^^^^^^^^^^^^^^^^ + | + = help: use the `is_nan` method instead + +warning: 3 warnings emitted + diff --git a/tests/ui/lint/invalid-nan-comparison-no-std.fixed b/tests/ui/lint/invalid-nan-comparison-no-std.fixed new file mode 100644 index 0000000000000..3e35d14d223d7 --- /dev/null +++ b/tests/ui/lint/invalid-nan-comparison-no-std.fixed @@ -0,0 +1,18 @@ +//@ check-pass +//@ run-rustfix +//@ compile-flags: --crate-type=lib + +#![no_std] + +pub type MacroFloat = ::core::primitive::f64; + +#[allow(non_camel_case_types)] +pub struct f64; + +pub fn check() { + #[allow(unused_macros)] + macro_rules! nan { () => { $crate::MacroFloat::NAN }; } + let b = &3.3; + let _ = !::core::primitive::f64::is_nan(*b); + //~^ WARN incorrect NaN comparison +} diff --git a/tests/ui/lint/invalid-nan-comparison-no-std.rs b/tests/ui/lint/invalid-nan-comparison-no-std.rs new file mode 100644 index 0000000000000..7ace5b976c890 --- /dev/null +++ b/tests/ui/lint/invalid-nan-comparison-no-std.rs @@ -0,0 +1,18 @@ +//@ check-pass +//@ run-rustfix +//@ compile-flags: --crate-type=lib + +#![no_std] + +pub type MacroFloat = ::core::primitive::f64; + +#[allow(non_camel_case_types)] +pub struct f64; + +pub fn check() { + #[allow(unused_macros)] + macro_rules! nan { () => { $crate::MacroFloat::NAN }; } + let b = &3.3; + let _ = b != &nan!(); + //~^ WARN incorrect NaN comparison +} diff --git a/tests/ui/lint/invalid-nan-comparison-no-std.stderr b/tests/ui/lint/invalid-nan-comparison-no-std.stderr new file mode 100644 index 0000000000000..143736b352ad9 --- /dev/null +++ b/tests/ui/lint/invalid-nan-comparison-no-std.stderr @@ -0,0 +1,15 @@ +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-no-std.rs:16:13 + | +LL | let _ = b != &nan!(); + | ^^^^^^^^^^^^ + | + = note: `#[warn(invalid_nan_comparisons)]` on by default +help: use the `is_nan` method instead + | +LL - let _ = b != &nan!(); +LL + let _ = !::core::primitive::f64::is_nan(*b); + | + +warning: 1 warning emitted + diff --git a/tests/ui/lint/invalid-nan-comparison-no-suggestion.rs b/tests/ui/lint/invalid-nan-comparison-no-suggestion.rs new file mode 100644 index 0000000000000..afe3cb7cfd259 --- /dev/null +++ b/tests/ui/lint/invalid-nan-comparison-no-suggestion.rs @@ -0,0 +1,28 @@ +//@ check-pass + +#![feature(f16, f128)] + +fn main() { + let mut effects = 0; + let _ = 3.3 == { + effects += 1; + f64::NAN + }; + //~^^^^ WARN incorrect NaN comparison + let _ = effects; + + macro_rules! scoped_nan { () => {{ type Float = f64; Float::NAN }}; } + let _ = 3.3 == scoped_nan!(); + //~^ WARN incorrect NaN comparison + + macro_rules! nan16 { () => { f16::NAN }; } + let _ = 3.3 == nan16!(); + //~^ WARN incorrect NaN comparison + macro_rules! nan128 { () => { f128::NAN }; } + let _ = 3.3 != nan128!(); + //~^ WARN incorrect NaN comparison + + macro_rules! compare { () => { 3.3 != f64::NAN }; } + //~^ WARN incorrect NaN comparison + let _ = compare!(); +} diff --git a/tests/ui/lint/invalid-nan-comparison-no-suggestion.stderr b/tests/ui/lint/invalid-nan-comparison-no-suggestion.stderr new file mode 100644 index 0000000000000..659deb868b027 --- /dev/null +++ b/tests/ui/lint/invalid-nan-comparison-no-suggestion.stderr @@ -0,0 +1,50 @@ +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-no-suggestion.rs:7:13 + | +LL | let _ = 3.3 == { + | _____________^ +LL | | effects += 1; +LL | | f64::NAN +LL | | }; + | |_____^ + | + = help: use the `is_nan` method instead + = note: `#[warn(invalid_nan_comparisons)]` on by default + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-no-suggestion.rs:15:13 + | +LL | let _ = 3.3 == scoped_nan!(); + | ^^^^^^^^^^^^^^^^^^^^ + | + = help: use the `is_nan` method instead + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-no-suggestion.rs:19:13 + | +LL | let _ = 3.3 == nan16!(); + | ^^^^^^^^^^^^^^^ + | + = help: use the `is_nan` method instead + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-no-suggestion.rs:22:13 + | +LL | let _ = 3.3 != nan128!(); + | ^^^^^^^^^^^^^^^^ + | + = help: use the `is_nan` method instead + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-no-suggestion.rs:25:36 + | +LL | macro_rules! compare { () => { 3.3 != f64::NAN }; } + | ^^^^^^^^^^^^^^^ +LL | +LL | let _ = compare!(); + | ---------- in this macro invocation + | + = help: use the `is_nan` method instead + +warning: 5 warnings emitted + diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.fixed b/tests/ui/lint/invalid-nan-comparison-suggestion.fixed index 8fbdd079053c2..d3043e3f72516 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.fixed +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.fixed @@ -7,116 +7,164 @@ extern crate core; fn main() { let x = 5f16; - let _ = f16::is_nan(x); + let _ = ::is_nan(x); //~^ WARN incorrect NaN comparison - let _ = !f16::is_nan(x); + let _ = !::is_nan(x); //~^ WARN incorrect NaN comparison let x = 5f32; - let _ = f32::is_nan(x); + let _ = ::is_nan(x); //~^ WARN incorrect NaN comparison - let _ = !f32::is_nan(x); + let _ = !::is_nan(x); //~^ WARN incorrect NaN comparison let x = 5f64; - let _ = f64::is_nan(x); + let _ = ::is_nan(x); //~^ WARN incorrect NaN comparison - let _ = !f64::is_nan(x); + let _ = !::is_nan(x); //~^ WARN incorrect NaN comparison let x = 5f128; - let _ = f128::is_nan(x); + let _ = ::is_nan(x); //~^ WARN incorrect NaN comparison - let _ = !f128::is_nan(x); + let _ = !::is_nan(x); //~^ WARN incorrect NaN comparison let b = &2.3f32; - if !f32::is_nan(*b) {} + if !::is_nan(*b) {} //~^ WARN incorrect NaN comparison let b = &2.3f32; - if !f32::is_nan(*b) {} + if !::is_nan(*b) {} //~^ WARN incorrect NaN comparison let _ = - !f32::is_nan(*b); + !::is_nan(*b); // Issue #164143: an unsuffixed float is constrained by the `NAN` operand. // Suggesting `.is_nan()` drops that constraint and does not compile. let b = &3.3; - if !f64::is_nan(*b) {} + if !::is_nan(*b) {} //~^ WARN incorrect NaN comparison let mut m = &3.3; - if !f64::is_nan(*m) { + if !::is_nan(*m) { //~^ WARN incorrect NaN comparison m = &1.0; } let _ = m; let unary = &(-3.3); - if !f64::is_nan(*unary) {} + if !::is_nan(*unary) {} //~^ WARN incorrect NaN comparison let block = &{ 3.3 }; - if !f64::is_nan(*block) {} + if !::is_nan(*block) {} //~^ WARN incorrect NaN comparison let (tuple,) = (&3.3,); - if !f64::is_nan(*tuple) {} + if !::is_nan(*tuple) {} //~^ WARN incorrect NaN comparison let arithmetic = &(3.3 + 0.0); - if !f64::is_nan(*arithmetic) {} + if !::is_nan(*arithmetic) {} //~^ WARN incorrect NaN comparison let x = 1.0f64; - if !f64::is_nan(x) {} + if !::is_nan(*&x) {} //~^ WARN incorrect NaN comparison let p = &x; - let _ = f64::is_nan(*p); + let _ = ::is_nan(*p); //~^ WARN incorrect NaN comparison - let _ = f64::is_nan(-x); + let _ = ::is_nan(-x); //~^ WARN incorrect NaN comparison shadowed::check(); + nested_operands(); + macro_paths(); - let _ = f64::is_nan(3.3); + let _ = ::is_nan(3.3); //~^ WARN incorrect NaN comparison - let _ = !f64::is_nan(3.3); + let _ = !::is_nan(*&3.3); //~^ WARN incorrect NaN comparison let suffixed = 3.3f64; let copied = suffixed; - let _ = !f64::is_nan(copied); + let _ = !::is_nan(copied); //~^ WARN incorrect NaN comparison let ascribed: f64 = 3.3; - let _ = !f64::is_nan(ascribed); + let _ = !::is_nan(ascribed); //~^ WARN incorrect NaN comparison parameter(1.0); parameter_ref(&1.0); - let _ = |x: f64| !f64::is_nan(x); //~ WARN incorrect NaN comparison + let _ = |x: f64| !::is_nan(x); //~ WARN incorrect NaN comparison #[allow(unused_macros)] macro_rules! nan { () => { f32::NAN }; } macro_rules! number { () => { 5f32 }; } - let _ = f32::is_nan(number!()); + let _ = ::std::primitive::f32::is_nan(number!()); //~^ WARN incorrect NaN comparison - let _ = !f32::is_nan(number!()); + let _ = !::std::primitive::f32::is_nan(number!()); //~^ WARN incorrect NaN comparison } fn parameter(x: f64) { - let _ = f64::is_nan(x); + let _ = ::is_nan(x); //~^ WARN incorrect NaN comparison } fn parameter_ref(x: &f32) { - if !f32::is_nan(*x) {} + if !::is_nan(*x) {} + //~^ WARN incorrect NaN comparison +} + +fn nested_operands() { + let x = 1.0f64; + let _ = ::is_nan(*(&x as &f64)); + //~^ WARN incorrect NaN comparison + let _ = !::is_nan(*{ &{ 3.3 } }); + //~^ WARN incorrect NaN comparison + let _ = ::is_nan(***&&&3.3); + //~^ WARN incorrect NaN comparison + let b = &&&3.3; + let _ = !::is_nan(***b); + //~^ WARN incorrect NaN comparison + let b = &mut 3.3; + let _ = !::is_nan(*b); + //~^ WARN incorrect NaN comparison + let b = &3.3; + let _ = ::is_nan(*b); + //~^ WARN incorrect NaN comparison + let b = &3.3; + let _ = !::is_nan(*b); + //~^ WARN incorrect NaN comparison + let b = &&3.3; + let _ = ::is_nan(**b); + //~^ WARN incorrect NaN comparison + + let b = &(-3.3); + let _ = !::is_nan(*b); + //~^ WARN incorrect NaN comparison + let b = &(3.3 + 0.0); + let _ = ::is_nan(*b); + //~^ WARN incorrect NaN comparison + + macro_rules! borrowed { () => { &3.3 }; } + let _ = !::is_nan(*borrowed!()); + //~^ WARN incorrect NaN comparison +} + +pub type MacroFloat = f64; + +fn macro_paths() { + #[allow(unused_macros)] + macro_rules! nan { () => { $crate::MacroFloat::NAN }; } + let b = &3.3; + let _ = !::std::primitive::f64::is_nan(*b); //~^ WARN incorrect NaN comparison } @@ -127,10 +175,13 @@ mod shadowed { pub fn check() { let b = &3.3; - if !::core::primitive::f64::is_nan(*b) {} + if !<::core::primitive::f64>::is_nan(*b) {} //~^ WARN incorrect NaN comparison let c = &4.4; - if !Float::is_nan(*c) {} + if !::is_nan(*c) {} + //~^ WARN incorrect NaN comparison + let d = &5.5; + if !::is_nan(*d) {} //~^ WARN incorrect NaN comparison } } diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.rs b/tests/ui/lint/invalid-nan-comparison-suggestion.rs index fb23208dfcfc9..d5376db904453 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.rs +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.rs @@ -83,6 +83,8 @@ fn main() { //~^ WARN incorrect NaN comparison shadowed::check(); + nested_operands(); + macro_paths(); let _ = 3.3 == f64::NAN; //~^ WARN incorrect NaN comparison @@ -123,6 +125,52 @@ fn parameter_ref(x: &f32) { //~^ WARN incorrect NaN comparison } +fn nested_operands() { + let x = 1.0f64; + let _ = &x as &f64 == &f64::NAN; + //~^ WARN incorrect NaN comparison + let _ = { &{ 3.3 } } != &f64::NAN; + //~^ WARN incorrect NaN comparison + let _ = &&&3.3 == &&&f64::NAN; + //~^ WARN incorrect NaN comparison + let b = &&&3.3; + let _ = &&&f64::NAN != b; + //~^ WARN incorrect NaN comparison + let b = &mut 3.3; + let _ = b != &f64::NAN; + //~^ WARN incorrect NaN comparison + let b = &3.3; + let _ = &(::NAN) == b; + //~^ WARN incorrect NaN comparison + let b = &3.3; + let _ = b != &{ f64::NAN }; + //~^ WARN incorrect NaN comparison + let b = &&3.3; + let _ = &{ &{ f64::NAN } } == b; + //~^ WARN incorrect NaN comparison + + let b = &(-3.3); + let _ = b != &f16::NAN; + //~^ WARN incorrect NaN comparison + let b = &(3.3 + 0.0); + let _ = b == &f128::NAN; + //~^ WARN incorrect NaN comparison + + macro_rules! borrowed { () => { &3.3 }; } + let _ = borrowed!() != &f64::NAN; + //~^ WARN incorrect NaN comparison +} + +pub type MacroFloat = f64; + +fn macro_paths() { + #[allow(unused_macros)] + macro_rules! nan { () => { $crate::MacroFloat::NAN }; } + let b = &3.3; + let _ = b != &nan!(); + //~^ WARN incorrect NaN comparison +} + mod shadowed { #[allow(dead_code, non_camel_case_types)] struct f64; @@ -135,5 +183,8 @@ mod shadowed { let c = &4.4; if c != &Float::NAN {} //~^ WARN incorrect NaN comparison + let d = &5.5; + if d != &Float::r#NAN {} + //~^ WARN incorrect NaN comparison } } diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr b/tests/ui/lint/invalid-nan-comparison-suggestion.stderr index c9f3b6bd95052..9c920496c3f54 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr +++ b/tests/ui/lint/invalid-nan-comparison-suggestion.stderr @@ -5,10 +5,10 @@ LL | let _ = x == f16::NAN; | ^^^^^^^^^^^^^ | = note: `#[warn(invalid_nan_comparisons)]` on by default -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = x == f16::NAN; -LL + let _ = f16::is_nan(x); +LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -17,10 +17,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | let _ = x != f16::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = x != f16::NAN; -LL + let _ = !f16::is_nan(x); +LL + let _ = !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -29,10 +29,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | let _ = x == f32::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = x == f32::NAN; -LL + let _ = f32::is_nan(x); +LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -41,10 +41,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | let _ = x != f32::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = x != f32::NAN; -LL + let _ = !f32::is_nan(x); +LL + let _ = !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -53,10 +53,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | let _ = x == f64::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = x == f64::NAN; -LL + let _ = f64::is_nan(x); +LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -65,10 +65,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | let _ = x != f64::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = x != f64::NAN; -LL + let _ = !f64::is_nan(x); +LL + let _ = !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -77,10 +77,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | let _ = x == f128::NAN; | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = x == f128::NAN; -LL + let _ = f128::is_nan(x); +LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -89,10 +89,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | let _ = x != f128::NAN; | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = x != f128::NAN; -LL + let _ = !f128::is_nan(x); +LL + let _ = !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -101,10 +101,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | if b != &f32::NAN {} | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if b != &f32::NAN {} -LL + if !f32::is_nan(*b) {} +LL + if !::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -113,10 +113,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | if b != { &f32::NAN } {} | ^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if b != { &f32::NAN } {} -LL + if !f32::is_nan(*b) {} +LL + if !::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -128,13 +128,13 @@ LL | | &f32::NAN LL | | }; | |_________^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - b != { LL - LL - &f32::NAN LL - }; -LL + !f32::is_nan(*b); +LL + !::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -143,10 +143,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | if b != &f64::NAN {} | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if b != &f64::NAN {} -LL + if !f64::is_nan(*b) {} +LL + if !::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -155,10 +155,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | if m != &f64::NAN { | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if m != &f64::NAN { -LL + if !f64::is_nan(*m) { +LL + if !::is_nan(*m) { | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -167,10 +167,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | if unary != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if unary != &f64::NAN {} -LL + if !f64::is_nan(*unary) {} +LL + if !::is_nan(*unary) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -179,10 +179,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | if block != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if block != &f64::NAN {} -LL + if !f64::is_nan(*block) {} +LL + if !::is_nan(*block) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -191,10 +191,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | if tuple != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if tuple != &f64::NAN {} -LL + if !f64::is_nan(*tuple) {} +LL + if !::is_nan(*tuple) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -203,10 +203,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | if arithmetic != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if arithmetic != &f64::NAN {} -LL + if !f64::is_nan(*arithmetic) {} +LL + if !::is_nan(*arithmetic) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -215,10 +215,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | if &x != &f64::NAN {} | ^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if &x != &f64::NAN {} -LL + if !f64::is_nan(x) {} +LL + if !::is_nan(*&x) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -227,10 +227,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | let _ = *p == f64::NAN; | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = *p == f64::NAN; -LL + let _ = f64::is_nan(*p); +LL + let _ = ::is_nan(*p); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -239,143 +239,299 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | let _ = -x == f64::NAN; | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = -x == f64::NAN; -LL + let _ = f64::is_nan(-x); +LL + let _ = ::is_nan(-x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:87:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:89:13 | LL | let _ = 3.3 == f64::NAN; | ^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = 3.3 == f64::NAN; -LL + let _ = f64::is_nan(3.3); +LL + let _ = ::is_nan(3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:90:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:92:13 | LL | let _ = &3.3 != &f64::NAN; | ^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = &3.3 != &f64::NAN; -LL + let _ = !f64::is_nan(3.3); +LL + let _ = !::is_nan(*&3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:95:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:97:13 | LL | let _ = copied != f64::NAN; | ^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = copied != f64::NAN; -LL + let _ = !f64::is_nan(copied); +LL + let _ = !::is_nan(copied); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:99:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:101:13 | LL | let _ = ascribed != f64::NAN; | ^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = ascribed != f64::NAN; -LL + let _ = !f64::is_nan(ascribed); +LL + let _ = !::is_nan(ascribed); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:104:22 + --> $DIR/invalid-nan-comparison-suggestion.rs:106:22 | LL | let _ = |x: f64| x != f64::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = |x: f64| x != f64::NAN; -LL + let _ = |x: f64| !f64::is_nan(x); +LL + let _ = |x: f64| !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:110:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:112:13 | LL | let _ = nan!() == number!(); | ^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = nan!() == number!(); -LL + let _ = f32::is_nan(number!()); +LL + let _ = ::std::primitive::f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:112:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:114:13 | LL | let _ = number!() != nan!(); | ^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = number!() != nan!(); -LL + let _ = !f32::is_nan(number!()); +LL + let _ = !::std::primitive::f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:117:13 + --> $DIR/invalid-nan-comparison-suggestion.rs:119:13 | LL | let _ = x == f64::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - let _ = x == f64::NAN; -LL + let _ = f64::is_nan(x); +LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:122:8 + --> $DIR/invalid-nan-comparison-suggestion.rs:124:8 | LL | if x != &f32::NAN {} | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if x != &f32::NAN {} -LL + if !f32::is_nan(*x) {} +LL + if !::is_nan(*x) {} + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:130:13 + | +LL | let _ = &x as &f64 == &f64::NAN; + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = &x as &f64 == &f64::NAN; +LL + let _ = ::is_nan(*(&x as &f64)); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:132:13 + | +LL | let _ = { &{ 3.3 } } != &f64::NAN; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = { &{ 3.3 } } != &f64::NAN; +LL + let _ = !::is_nan(*{ &{ 3.3 } }); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:133:12 + --> $DIR/invalid-nan-comparison-suggestion.rs:134:13 + | +LL | let _ = &&&3.3 == &&&f64::NAN; + | ^^^^^^^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = &&&3.3 == &&&f64::NAN; +LL + let _ = ::is_nan(***&&&3.3); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:137:13 + | +LL | let _ = &&&f64::NAN != b; + | ^^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = &&&f64::NAN != b; +LL + let _ = !::is_nan(***b); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:140:13 + | +LL | let _ = b != &f64::NAN; + | ^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = b != &f64::NAN; +LL + let _ = !::is_nan(*b); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:143:13 + | +LL | let _ = &(::NAN) == b; + | ^^^^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = &(::NAN) == b; +LL + let _ = ::is_nan(*b); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:146:13 + | +LL | let _ = b != &{ f64::NAN }; + | ^^^^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = b != &{ f64::NAN }; +LL + let _ = !::is_nan(*b); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:149:13 + | +LL | let _ = &{ &{ f64::NAN } } == b; + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = &{ &{ f64::NAN } } == b; +LL + let _ = ::is_nan(**b); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:153:13 + | +LL | let _ = b != &f16::NAN; + | ^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = b != &f16::NAN; +LL + let _ = !::is_nan(*b); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:156:13 + | +LL | let _ = b == &f128::NAN; + | ^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = b == &f128::NAN; +LL + let _ = ::is_nan(*b); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:160:13 + | +LL | let _ = borrowed!() != &f64::NAN; + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = borrowed!() != &f64::NAN; +LL + let _ = !::is_nan(*borrowed!()); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:170:13 + | +LL | let _ = b != &nan!(); + | ^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _ = b != &nan!(); +LL + let _ = !::std::primitive::f64::is_nan(*b); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:181:12 | LL | if b != &::core::primitive::f64::NAN {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if b != &::core::primitive::f64::NAN {} -LL + if !::core::primitive::f64::is_nan(*b) {} +LL + if !<::core::primitive::f64>::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:136:12 + --> $DIR/invalid-nan-comparison-suggestion.rs:184:12 | LL | if c != &Float::NAN {} | ^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - if c != &Float::NAN {} -LL + if !Float::is_nan(*c) {} +LL + if !::is_nan(*c) {} + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/invalid-nan-comparison-suggestion.rs:187:12 + | +LL | if d != &Float::r#NAN {} + | ^^^^^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - if d != &Float::r#NAN {} +LL + if !::is_nan(*d) {} | -warning: 31 warnings emitted +warning: 44 warnings emitted diff --git a/tests/ui/lint/invalid-nan-comparison.stderr b/tests/ui/lint/invalid-nan-comparison.stderr index 4b31abf84ff54..3ee29a9fc4570 100644 --- a/tests/ui/lint/invalid-nan-comparison.stderr +++ b/tests/ui/lint/invalid-nan-comparison.stderr @@ -5,10 +5,10 @@ LL | const TEST: bool = 5f32 == f32::NAN; | ^^^^^^^^^^^^^^^^ | = note: `#[warn(invalid_nan_comparisons)]` on by default -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - const TEST: bool = 5f32 == f32::NAN; -LL + const TEST: bool = f32::is_nan(5f32); +LL + const TEST: bool = ::is_nan(5f32); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -17,10 +17,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | x == f16::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - x == f16::NAN; -LL + f16::is_nan(x); +LL + ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -29,10 +29,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | x != f16::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - x != f16::NAN; -LL + !f16::is_nan(x); +LL + !::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable @@ -65,10 +65,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | number!() == f16::NAN; | ^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - number!() == f16::NAN; -LL + f16::is_nan(number!()); +LL + ::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -77,10 +77,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | f16::NAN != number!(); | ^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - f16::NAN != number!(); -LL + !f16::is_nan(number!()); +LL + !::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -89,10 +89,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | x == f32::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - x == f32::NAN; -LL + f32::is_nan(x); +LL + ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -101,10 +101,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | x != f32::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - x != f32::NAN; -LL + !f32::is_nan(x); +LL + !::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable @@ -137,10 +137,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | number!() == f32::NAN; | ^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - number!() == f32::NAN; -LL + f32::is_nan(number!()); +LL + ::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -149,10 +149,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | f32::NAN != number!(); | ^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - f32::NAN != number!(); -LL + !f32::is_nan(number!()); +LL + !::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -161,10 +161,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | x == f64::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - x == f64::NAN; -LL + f64::is_nan(x); +LL + ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -173,10 +173,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | x != f64::NAN; | ^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - x != f64::NAN; -LL + !f64::is_nan(x); +LL + !::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable @@ -209,10 +209,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | number!() == f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - number!() == f64::NAN; -LL + f64::is_nan(number!()); +LL + ::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -221,10 +221,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | f64::NAN != number!(); | ^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - f64::NAN != number!(); -LL + !f64::is_nan(number!()); +LL + !::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -233,10 +233,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | x == f128::NAN; | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - x == f128::NAN; -LL + f128::is_nan(x); +LL + ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -245,10 +245,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | x != f128::NAN; | ^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - x != f128::NAN; -LL + !f128::is_nan(x); +LL + !::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable @@ -281,10 +281,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | number!() == f128::NAN; | ^^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - number!() == f128::NAN; -LL + f128::is_nan(number!()); +LL + ::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself @@ -293,10 +293,10 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself LL | f128::NAN != number!(); | ^^^^^^^^^^^^^^^^^^^^^^ | -help: use `f32::is_nan()` or `f64::is_nan()` instead +help: use the `is_nan` method instead | LL - f128::NAN != number!(); -LL + !f128::is_nan(number!()); +LL + !::is_nan(number!()); | warning: 33 warnings emitted From 2c2a5e9c24e5078ec5d338e8087bdfdf963c9b59 Mon Sep 17 00:00:00 2001 From: Yu Date: Mon, 19 Oct 2026 22:30:17 +0800 Subject: [PATCH 5/7] test: re-organize ui test case --- .../basic.rs} | 0 .../basic.stderr} | 66 +++++++------- .../custom-partial-eq.rs} | 0 .../custom-partial-eq.stderr} | 6 +- .../no-std.fixed} | 0 .../no-std.rs} | 0 .../no-std.stderr} | 2 +- .../no-suggestion.rs} | 0 .../no-suggestion.stderr} | 10 +-- .../suggestion.fixed} | 0 .../suggestion.rs} | 0 .../suggestion.stderr} | 88 +++++++++---------- 12 files changed, 86 insertions(+), 86 deletions(-) rename tests/ui/lint/{invalid-nan-comparison.rs => invalid-nan-comparisons/basic.rs} (100%) rename tests/ui/lint/{invalid-nan-comparison.stderr => invalid-nan-comparisons/basic.stderr} (81%) rename tests/ui/lint/{invalid-nan-comparison-custom-partial-eq.rs => invalid-nan-comparisons/custom-partial-eq.rs} (100%) rename tests/ui/lint/{invalid-nan-comparison-custom-partial-eq.stderr => invalid-nan-comparisons/custom-partial-eq.stderr} (79%) rename tests/ui/lint/{invalid-nan-comparison-no-std.fixed => invalid-nan-comparisons/no-std.fixed} (100%) rename tests/ui/lint/{invalid-nan-comparison-no-std.rs => invalid-nan-comparisons/no-std.rs} (100%) rename tests/ui/lint/{invalid-nan-comparison-no-std.stderr => invalid-nan-comparisons/no-std.stderr} (88%) rename tests/ui/lint/{invalid-nan-comparison-no-suggestion.rs => invalid-nan-comparisons/no-suggestion.rs} (100%) rename tests/ui/lint/{invalid-nan-comparison-no-suggestion.stderr => invalid-nan-comparisons/no-suggestion.stderr} (81%) rename tests/ui/lint/{invalid-nan-comparison-suggestion.fixed => invalid-nan-comparisons/suggestion.fixed} (100%) rename tests/ui/lint/{invalid-nan-comparison-suggestion.rs => invalid-nan-comparisons/suggestion.rs} (100%) rename tests/ui/lint/{invalid-nan-comparison-suggestion.stderr => invalid-nan-comparisons/suggestion.stderr} (83%) diff --git a/tests/ui/lint/invalid-nan-comparison.rs b/tests/ui/lint/invalid-nan-comparisons/basic.rs similarity index 100% rename from tests/ui/lint/invalid-nan-comparison.rs rename to tests/ui/lint/invalid-nan-comparisons/basic.rs diff --git a/tests/ui/lint/invalid-nan-comparison.stderr b/tests/ui/lint/invalid-nan-comparisons/basic.stderr similarity index 81% rename from tests/ui/lint/invalid-nan-comparison.stderr rename to tests/ui/lint/invalid-nan-comparisons/basic.stderr index 3ee29a9fc4570..77cd6fc111e75 100644 --- a/tests/ui/lint/invalid-nan-comparison.stderr +++ b/tests/ui/lint/invalid-nan-comparisons/basic.stderr @@ -1,5 +1,5 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:12:20 + --> $DIR/basic.rs:12:20 | LL | const TEST: bool = 5f32 == f32::NAN; | ^^^^^^^^^^^^^^^^ @@ -12,7 +12,7 @@ LL + const TEST: bool = ::is_nan(5f32); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:18:5 + --> $DIR/basic.rs:18:5 | LL | x == f16::NAN; | ^^^^^^^^^^^^^ @@ -24,7 +24,7 @@ LL + ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:20:5 + --> $DIR/basic.rs:20:5 | LL | x != f16::NAN; | ^^^^^^^^^^^^^ @@ -36,31 +36,31 @@ LL + !::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:22:5 + --> $DIR/basic.rs:22:5 | LL | x < f16::NAN; | ^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:24:5 + --> $DIR/basic.rs:24:5 | LL | x > f16::NAN; | ^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:26:5 + --> $DIR/basic.rs:26:5 | LL | x <= f16::NAN; | ^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:28:5 + --> $DIR/basic.rs:28:5 | LL | x >= f16::NAN; | ^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:30:5 + --> $DIR/basic.rs:30:5 | LL | number!() == f16::NAN; | ^^^^^^^^^^^^^^^^^^^^^ @@ -72,7 +72,7 @@ LL + ::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:32:5 + --> $DIR/basic.rs:32:5 | LL | f16::NAN != number!(); | ^^^^^^^^^^^^^^^^^^^^^ @@ -84,7 +84,7 @@ LL + !::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:39:5 + --> $DIR/basic.rs:39:5 | LL | x == f32::NAN; | ^^^^^^^^^^^^^ @@ -96,7 +96,7 @@ LL + ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:41:5 + --> $DIR/basic.rs:41:5 | LL | x != f32::NAN; | ^^^^^^^^^^^^^ @@ -108,31 +108,31 @@ LL + !::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:43:5 + --> $DIR/basic.rs:43:5 | LL | x < f32::NAN; | ^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:45:5 + --> $DIR/basic.rs:45:5 | LL | x > f32::NAN; | ^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:47:5 + --> $DIR/basic.rs:47:5 | LL | x <= f32::NAN; | ^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:49:5 + --> $DIR/basic.rs:49:5 | LL | x >= f32::NAN; | ^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:51:5 + --> $DIR/basic.rs:51:5 | LL | number!() == f32::NAN; | ^^^^^^^^^^^^^^^^^^^^^ @@ -144,7 +144,7 @@ LL + ::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:53:5 + --> $DIR/basic.rs:53:5 | LL | f32::NAN != number!(); | ^^^^^^^^^^^^^^^^^^^^^ @@ -156,7 +156,7 @@ LL + !::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:60:5 + --> $DIR/basic.rs:60:5 | LL | x == f64::NAN; | ^^^^^^^^^^^^^ @@ -168,7 +168,7 @@ LL + ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:62:5 + --> $DIR/basic.rs:62:5 | LL | x != f64::NAN; | ^^^^^^^^^^^^^ @@ -180,31 +180,31 @@ LL + !::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:64:5 + --> $DIR/basic.rs:64:5 | LL | x < f64::NAN; | ^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:66:5 + --> $DIR/basic.rs:66:5 | LL | x > f64::NAN; | ^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:68:5 + --> $DIR/basic.rs:68:5 | LL | x <= f64::NAN; | ^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:70:5 + --> $DIR/basic.rs:70:5 | LL | x >= f64::NAN; | ^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:72:5 + --> $DIR/basic.rs:72:5 | LL | number!() == f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^ @@ -216,7 +216,7 @@ LL + ::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:74:5 + --> $DIR/basic.rs:74:5 | LL | f64::NAN != number!(); | ^^^^^^^^^^^^^^^^^^^^^ @@ -228,7 +228,7 @@ LL + !::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:81:5 + --> $DIR/basic.rs:81:5 | LL | x == f128::NAN; | ^^^^^^^^^^^^^^ @@ -240,7 +240,7 @@ LL + ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:83:5 + --> $DIR/basic.rs:83:5 | LL | x != f128::NAN; | ^^^^^^^^^^^^^^ @@ -252,31 +252,31 @@ LL + !::is_nan(x); | warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:85:5 + --> $DIR/basic.rs:85:5 | LL | x < f128::NAN; | ^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:87:5 + --> $DIR/basic.rs:87:5 | LL | x > f128::NAN; | ^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:89:5 + --> $DIR/basic.rs:89:5 | LL | x <= f128::NAN; | ^^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN is not orderable - --> $DIR/invalid-nan-comparison.rs:91:5 + --> $DIR/basic.rs:91:5 | LL | x >= f128::NAN; | ^^^^^^^^^^^^^^ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:93:5 + --> $DIR/basic.rs:93:5 | LL | number!() == f128::NAN; | ^^^^^^^^^^^^^^^^^^^^^^ @@ -288,7 +288,7 @@ LL + ::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison.rs:95:5 + --> $DIR/basic.rs:95:5 | LL | f128::NAN != number!(); | ^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/lint/invalid-nan-comparison-custom-partial-eq.rs b/tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.rs similarity index 100% rename from tests/ui/lint/invalid-nan-comparison-custom-partial-eq.rs rename to tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.rs diff --git a/tests/ui/lint/invalid-nan-comparison-custom-partial-eq.stderr b/tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.stderr similarity index 79% rename from tests/ui/lint/invalid-nan-comparison-custom-partial-eq.stderr rename to tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.stderr index daf91b092a3bb..f0c914dccdce6 100644 --- a/tests/ui/lint/invalid-nan-comparison-custom-partial-eq.stderr +++ b/tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.stderr @@ -1,5 +1,5 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-custom-partial-eq.rs:19:13 + --> $DIR/custom-partial-eq.rs:19:13 | LL | let _ = value == f64::NAN; | ^^^^^^^^^^^^^^^^^ @@ -8,7 +8,7 @@ LL | let _ = value == f64::NAN; = note: `#[warn(invalid_nan_comparisons)]` on by default warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-custom-partial-eq.rs:21:13 + --> $DIR/custom-partial-eq.rs:21:13 | LL | let _ = f64::NAN != value; | ^^^^^^^^^^^^^^^^^ @@ -16,7 +16,7 @@ LL | let _ = f64::NAN != value; = help: use the `is_nan` method instead warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-custom-partial-eq.rs:23:13 + --> $DIR/custom-partial-eq.rs:23:13 | LL | let _ = &value != &f64::NAN; | ^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/lint/invalid-nan-comparison-no-std.fixed b/tests/ui/lint/invalid-nan-comparisons/no-std.fixed similarity index 100% rename from tests/ui/lint/invalid-nan-comparison-no-std.fixed rename to tests/ui/lint/invalid-nan-comparisons/no-std.fixed diff --git a/tests/ui/lint/invalid-nan-comparison-no-std.rs b/tests/ui/lint/invalid-nan-comparisons/no-std.rs similarity index 100% rename from tests/ui/lint/invalid-nan-comparison-no-std.rs rename to tests/ui/lint/invalid-nan-comparisons/no-std.rs diff --git a/tests/ui/lint/invalid-nan-comparison-no-std.stderr b/tests/ui/lint/invalid-nan-comparisons/no-std.stderr similarity index 88% rename from tests/ui/lint/invalid-nan-comparison-no-std.stderr rename to tests/ui/lint/invalid-nan-comparisons/no-std.stderr index 143736b352ad9..b10909e157020 100644 --- a/tests/ui/lint/invalid-nan-comparison-no-std.stderr +++ b/tests/ui/lint/invalid-nan-comparisons/no-std.stderr @@ -1,5 +1,5 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-no-std.rs:16:13 + --> $DIR/no-std.rs:16:13 | LL | let _ = b != &nan!(); | ^^^^^^^^^^^^ diff --git a/tests/ui/lint/invalid-nan-comparison-no-suggestion.rs b/tests/ui/lint/invalid-nan-comparisons/no-suggestion.rs similarity index 100% rename from tests/ui/lint/invalid-nan-comparison-no-suggestion.rs rename to tests/ui/lint/invalid-nan-comparisons/no-suggestion.rs diff --git a/tests/ui/lint/invalid-nan-comparison-no-suggestion.stderr b/tests/ui/lint/invalid-nan-comparisons/no-suggestion.stderr similarity index 81% rename from tests/ui/lint/invalid-nan-comparison-no-suggestion.stderr rename to tests/ui/lint/invalid-nan-comparisons/no-suggestion.stderr index 659deb868b027..278f4d225aec4 100644 --- a/tests/ui/lint/invalid-nan-comparison-no-suggestion.stderr +++ b/tests/ui/lint/invalid-nan-comparisons/no-suggestion.stderr @@ -1,5 +1,5 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-no-suggestion.rs:7:13 + --> $DIR/no-suggestion.rs:7:13 | LL | let _ = 3.3 == { | _____________^ @@ -12,7 +12,7 @@ LL | | }; = note: `#[warn(invalid_nan_comparisons)]` on by default warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-no-suggestion.rs:15:13 + --> $DIR/no-suggestion.rs:15:13 | LL | let _ = 3.3 == scoped_nan!(); | ^^^^^^^^^^^^^^^^^^^^ @@ -20,7 +20,7 @@ LL | let _ = 3.3 == scoped_nan!(); = help: use the `is_nan` method instead warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-no-suggestion.rs:19:13 + --> $DIR/no-suggestion.rs:19:13 | LL | let _ = 3.3 == nan16!(); | ^^^^^^^^^^^^^^^ @@ -28,7 +28,7 @@ LL | let _ = 3.3 == nan16!(); = help: use the `is_nan` method instead warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-no-suggestion.rs:22:13 + --> $DIR/no-suggestion.rs:22:13 | LL | let _ = 3.3 != nan128!(); | ^^^^^^^^^^^^^^^^ @@ -36,7 +36,7 @@ LL | let _ = 3.3 != nan128!(); = help: use the `is_nan` method instead warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-no-suggestion.rs:25:36 + --> $DIR/no-suggestion.rs:25:36 | LL | macro_rules! compare { () => { 3.3 != f64::NAN }; } | ^^^^^^^^^^^^^^^ diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.fixed b/tests/ui/lint/invalid-nan-comparisons/suggestion.fixed similarity index 100% rename from tests/ui/lint/invalid-nan-comparison-suggestion.fixed rename to tests/ui/lint/invalid-nan-comparisons/suggestion.fixed diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.rs b/tests/ui/lint/invalid-nan-comparisons/suggestion.rs similarity index 100% rename from tests/ui/lint/invalid-nan-comparison-suggestion.rs rename to tests/ui/lint/invalid-nan-comparisons/suggestion.rs diff --git a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr b/tests/ui/lint/invalid-nan-comparisons/suggestion.stderr similarity index 83% rename from tests/ui/lint/invalid-nan-comparison-suggestion.stderr rename to tests/ui/lint/invalid-nan-comparisons/suggestion.stderr index 9c920496c3f54..467e97be03f57 100644 --- a/tests/ui/lint/invalid-nan-comparison-suggestion.stderr +++ b/tests/ui/lint/invalid-nan-comparisons/suggestion.stderr @@ -1,5 +1,5 @@ warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:10:13 + --> $DIR/suggestion.rs:10:13 | LL | let _ = x == f16::NAN; | ^^^^^^^^^^^^^ @@ -12,7 +12,7 @@ LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:12:13 + --> $DIR/suggestion.rs:12:13 | LL | let _ = x != f16::NAN; | ^^^^^^^^^^^^^ @@ -24,7 +24,7 @@ LL + let _ = !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:16:13 + --> $DIR/suggestion.rs:16:13 | LL | let _ = x == f32::NAN; | ^^^^^^^^^^^^^ @@ -36,7 +36,7 @@ LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:18:13 + --> $DIR/suggestion.rs:18:13 | LL | let _ = x != f32::NAN; | ^^^^^^^^^^^^^ @@ -48,7 +48,7 @@ LL + let _ = !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:22:13 + --> $DIR/suggestion.rs:22:13 | LL | let _ = x == f64::NAN; | ^^^^^^^^^^^^^ @@ -60,7 +60,7 @@ LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:24:13 + --> $DIR/suggestion.rs:24:13 | LL | let _ = x != f64::NAN; | ^^^^^^^^^^^^^ @@ -72,7 +72,7 @@ LL + let _ = !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:28:13 + --> $DIR/suggestion.rs:28:13 | LL | let _ = x == f128::NAN; | ^^^^^^^^^^^^^^ @@ -84,7 +84,7 @@ LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:30:13 + --> $DIR/suggestion.rs:30:13 | LL | let _ = x != f128::NAN; | ^^^^^^^^^^^^^^ @@ -96,7 +96,7 @@ LL + let _ = !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:34:8 + --> $DIR/suggestion.rs:34:8 | LL | if b != &f32::NAN {} | ^^^^^^^^^^^^^^ @@ -108,7 +108,7 @@ LL + if !::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:38:8 + --> $DIR/suggestion.rs:38:8 | LL | if b != { &f32::NAN } {} | ^^^^^^^^^^^^^^^^^^ @@ -120,7 +120,7 @@ LL + if !::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:42:9 + --> $DIR/suggestion.rs:42:9 | LL | / b != { LL | | @@ -138,7 +138,7 @@ LL + !::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:50:8 + --> $DIR/suggestion.rs:50:8 | LL | if b != &f64::NAN {} | ^^^^^^^^^^^^^^ @@ -150,7 +150,7 @@ LL + if !::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:54:8 + --> $DIR/suggestion.rs:54:8 | LL | if m != &f64::NAN { | ^^^^^^^^^^^^^^ @@ -162,7 +162,7 @@ LL + if !::is_nan(*m) { | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:61:8 + --> $DIR/suggestion.rs:61:8 | LL | if unary != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^ @@ -174,7 +174,7 @@ LL + if !::is_nan(*unary) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:65:8 + --> $DIR/suggestion.rs:65:8 | LL | if block != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^ @@ -186,7 +186,7 @@ LL + if !::is_nan(*block) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:69:8 + --> $DIR/suggestion.rs:69:8 | LL | if tuple != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^ @@ -198,7 +198,7 @@ LL + if !::is_nan(*tuple) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:73:8 + --> $DIR/suggestion.rs:73:8 | LL | if arithmetic != &f64::NAN {} | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -210,7 +210,7 @@ LL + if !::is_nan(*arithmetic) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:77:8 + --> $DIR/suggestion.rs:77:8 | LL | if &x != &f64::NAN {} | ^^^^^^^^^^^^^^^ @@ -222,7 +222,7 @@ LL + if !::is_nan(*&x) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:80:13 + --> $DIR/suggestion.rs:80:13 | LL | let _ = *p == f64::NAN; | ^^^^^^^^^^^^^^ @@ -234,7 +234,7 @@ LL + let _ = ::is_nan(*p); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:82:13 + --> $DIR/suggestion.rs:82:13 | LL | let _ = -x == f64::NAN; | ^^^^^^^^^^^^^^ @@ -246,7 +246,7 @@ LL + let _ = ::is_nan(-x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:89:13 + --> $DIR/suggestion.rs:89:13 | LL | let _ = 3.3 == f64::NAN; | ^^^^^^^^^^^^^^^ @@ -258,7 +258,7 @@ LL + let _ = ::is_nan(3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:92:13 + --> $DIR/suggestion.rs:92:13 | LL | let _ = &3.3 != &f64::NAN; | ^^^^^^^^^^^^^^^^^ @@ -270,7 +270,7 @@ LL + let _ = !::is_nan(*&3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:97:13 + --> $DIR/suggestion.rs:97:13 | LL | let _ = copied != f64::NAN; | ^^^^^^^^^^^^^^^^^^ @@ -282,7 +282,7 @@ LL + let _ = !::is_nan(copied); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:101:13 + --> $DIR/suggestion.rs:101:13 | LL | let _ = ascribed != f64::NAN; | ^^^^^^^^^^^^^^^^^^^^ @@ -294,7 +294,7 @@ LL + let _ = !::is_nan(ascribed); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:106:22 + --> $DIR/suggestion.rs:106:22 | LL | let _ = |x: f64| x != f64::NAN; | ^^^^^^^^^^^^^ @@ -306,7 +306,7 @@ LL + let _ = |x: f64| !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:112:13 + --> $DIR/suggestion.rs:112:13 | LL | let _ = nan!() == number!(); | ^^^^^^^^^^^^^^^^^^^ @@ -318,7 +318,7 @@ LL + let _ = ::std::primitive::f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:114:13 + --> $DIR/suggestion.rs:114:13 | LL | let _ = number!() != nan!(); | ^^^^^^^^^^^^^^^^^^^ @@ -330,7 +330,7 @@ LL + let _ = !::std::primitive::f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:119:13 + --> $DIR/suggestion.rs:119:13 | LL | let _ = x == f64::NAN; | ^^^^^^^^^^^^^ @@ -342,7 +342,7 @@ LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:124:8 + --> $DIR/suggestion.rs:124:8 | LL | if x != &f32::NAN {} | ^^^^^^^^^^^^^^ @@ -354,7 +354,7 @@ LL + if !::is_nan(*x) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:130:13 + --> $DIR/suggestion.rs:130:13 | LL | let _ = &x as &f64 == &f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -366,7 +366,7 @@ LL + let _ = ::is_nan(*(&x as &f64)); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:132:13 + --> $DIR/suggestion.rs:132:13 | LL | let _ = { &{ 3.3 } } != &f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -378,7 +378,7 @@ LL + let _ = !::is_nan(*{ &{ 3.3 } }); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:134:13 + --> $DIR/suggestion.rs:134:13 | LL | let _ = &&&3.3 == &&&f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^ @@ -390,7 +390,7 @@ LL + let _ = ::is_nan(***&&&3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:137:13 + --> $DIR/suggestion.rs:137:13 | LL | let _ = &&&f64::NAN != b; | ^^^^^^^^^^^^^^^^ @@ -402,7 +402,7 @@ LL + let _ = !::is_nan(***b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:140:13 + --> $DIR/suggestion.rs:140:13 | LL | let _ = b != &f64::NAN; | ^^^^^^^^^^^^^^ @@ -414,7 +414,7 @@ LL + let _ = !::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:143:13 + --> $DIR/suggestion.rs:143:13 | LL | let _ = &(::NAN) == b; | ^^^^^^^^^^^^^^^^^^ @@ -426,7 +426,7 @@ LL + let _ = ::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:146:13 + --> $DIR/suggestion.rs:146:13 | LL | let _ = b != &{ f64::NAN }; | ^^^^^^^^^^^^^^^^^^ @@ -438,7 +438,7 @@ LL + let _ = !::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:149:13 + --> $DIR/suggestion.rs:149:13 | LL | let _ = &{ &{ f64::NAN } } == b; | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -450,7 +450,7 @@ LL + let _ = ::is_nan(**b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:153:13 + --> $DIR/suggestion.rs:153:13 | LL | let _ = b != &f16::NAN; | ^^^^^^^^^^^^^^ @@ -462,7 +462,7 @@ LL + let _ = !::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:156:13 + --> $DIR/suggestion.rs:156:13 | LL | let _ = b == &f128::NAN; | ^^^^^^^^^^^^^^^ @@ -474,7 +474,7 @@ LL + let _ = ::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:160:13 + --> $DIR/suggestion.rs:160:13 | LL | let _ = borrowed!() != &f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -486,7 +486,7 @@ LL + let _ = !::is_nan(*borrowed!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:170:13 + --> $DIR/suggestion.rs:170:13 | LL | let _ = b != &nan!(); | ^^^^^^^^^^^^ @@ -498,7 +498,7 @@ LL + let _ = !::std::primitive::f64::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:181:12 + --> $DIR/suggestion.rs:181:12 | LL | if b != &::core::primitive::f64::NAN {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -510,7 +510,7 @@ LL + if !<::core::primitive::f64>::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:184:12 + --> $DIR/suggestion.rs:184:12 | LL | if c != &Float::NAN {} | ^^^^^^^^^^^^^^^^ @@ -522,7 +522,7 @@ LL + if !::is_nan(*c) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/invalid-nan-comparison-suggestion.rs:187:12 + --> $DIR/suggestion.rs:187:12 | LL | if d != &Float::r#NAN {} | ^^^^^^^^^^^^^^^^^^ From d201932dcbf01ba8217d4fd99860dab878a7887c Mon Sep 17 00:00:00 2001 From: Yu Date: Mon, 19 Oct 2026 22:38:19 +0800 Subject: [PATCH 6/7] test: cover shadowing in qualified NaN suggestions --- .../invalid-nan-comparisons/suggestion.fixed | 28 +++++++ .../invalid-nan-comparisons/suggestion.rs | 28 +++++++ .../invalid-nan-comparisons/suggestion.stderr | 74 ++++++++++++------- 3 files changed, 105 insertions(+), 25 deletions(-) diff --git a/tests/ui/lint/invalid-nan-comparisons/suggestion.fixed b/tests/ui/lint/invalid-nan-comparisons/suggestion.fixed index d3043e3f72516..be5f477e67992 100644 --- a/tests/ui/lint/invalid-nan-comparisons/suggestion.fixed +++ b/tests/ui/lint/invalid-nan-comparisons/suggestion.fixed @@ -82,6 +82,7 @@ fn main() { shadowed::check(); nested_operands(); macro_paths(); + method_resolution::check(); let _ = ::is_nan(3.3); //~^ WARN incorrect NaN comparison @@ -185,3 +186,30 @@ mod shadowed { //~^ WARN incorrect NaN comparison } } + +mod method_resolution { + mod f64 { + pub fn is_nan(_: ::core::primitive::f64) {} + } + + trait IsNan { + fn is_nan(self); + } + + impl IsNan for &f64 { + fn is_nan(self) {} + } + + pub fn check() { + let _: () = f64::is_nan(5.0); + let _: () = (&5f64).is_nan(); + + let x = 5f64; + let _: bool = ::is_nan(x); + //~^ WARN incorrect NaN comparison + + let x = &5f64; + let _: bool = ::is_nan(*x); + //~^ WARN incorrect NaN comparison + } +} diff --git a/tests/ui/lint/invalid-nan-comparisons/suggestion.rs b/tests/ui/lint/invalid-nan-comparisons/suggestion.rs index d5376db904453..96b7618e921e9 100644 --- a/tests/ui/lint/invalid-nan-comparisons/suggestion.rs +++ b/tests/ui/lint/invalid-nan-comparisons/suggestion.rs @@ -85,6 +85,7 @@ fn main() { shadowed::check(); nested_operands(); macro_paths(); + method_resolution::check(); let _ = 3.3 == f64::NAN; //~^ WARN incorrect NaN comparison @@ -188,3 +189,30 @@ mod shadowed { //~^ WARN incorrect NaN comparison } } + +mod method_resolution { + mod f64 { + pub fn is_nan(_: ::core::primitive::f64) {} + } + + trait IsNan { + fn is_nan(self); + } + + impl IsNan for &f64 { + fn is_nan(self) {} + } + + pub fn check() { + let _: () = f64::is_nan(5.0); + let _: () = (&5f64).is_nan(); + + let x = 5f64; + let _: bool = x == f64::NAN; + //~^ WARN incorrect NaN comparison + + let x = &5f64; + let _: bool = x == &f64::NAN; + //~^ WARN incorrect NaN comparison + } +} diff --git a/tests/ui/lint/invalid-nan-comparisons/suggestion.stderr b/tests/ui/lint/invalid-nan-comparisons/suggestion.stderr index 467e97be03f57..50d5684dbb2c1 100644 --- a/tests/ui/lint/invalid-nan-comparisons/suggestion.stderr +++ b/tests/ui/lint/invalid-nan-comparisons/suggestion.stderr @@ -246,7 +246,7 @@ LL + let _ = ::is_nan(-x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:89:13 + --> $DIR/suggestion.rs:90:13 | LL | let _ = 3.3 == f64::NAN; | ^^^^^^^^^^^^^^^ @@ -258,7 +258,7 @@ LL + let _ = ::is_nan(3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:92:13 + --> $DIR/suggestion.rs:93:13 | LL | let _ = &3.3 != &f64::NAN; | ^^^^^^^^^^^^^^^^^ @@ -270,7 +270,7 @@ LL + let _ = !::is_nan(*&3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:97:13 + --> $DIR/suggestion.rs:98:13 | LL | let _ = copied != f64::NAN; | ^^^^^^^^^^^^^^^^^^ @@ -282,7 +282,7 @@ LL + let _ = !::is_nan(copied); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:101:13 + --> $DIR/suggestion.rs:102:13 | LL | let _ = ascribed != f64::NAN; | ^^^^^^^^^^^^^^^^^^^^ @@ -294,7 +294,7 @@ LL + let _ = !::is_nan(ascribed); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:106:22 + --> $DIR/suggestion.rs:107:22 | LL | let _ = |x: f64| x != f64::NAN; | ^^^^^^^^^^^^^ @@ -306,7 +306,7 @@ LL + let _ = |x: f64| !::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:112:13 + --> $DIR/suggestion.rs:113:13 | LL | let _ = nan!() == number!(); | ^^^^^^^^^^^^^^^^^^^ @@ -318,7 +318,7 @@ LL + let _ = ::std::primitive::f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:114:13 + --> $DIR/suggestion.rs:115:13 | LL | let _ = number!() != nan!(); | ^^^^^^^^^^^^^^^^^^^ @@ -330,7 +330,7 @@ LL + let _ = !::std::primitive::f32::is_nan(number!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:119:13 + --> $DIR/suggestion.rs:120:13 | LL | let _ = x == f64::NAN; | ^^^^^^^^^^^^^ @@ -342,7 +342,7 @@ LL + let _ = ::is_nan(x); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:124:8 + --> $DIR/suggestion.rs:125:8 | LL | if x != &f32::NAN {} | ^^^^^^^^^^^^^^ @@ -354,7 +354,7 @@ LL + if !::is_nan(*x) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:130:13 + --> $DIR/suggestion.rs:131:13 | LL | let _ = &x as &f64 == &f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -366,7 +366,7 @@ LL + let _ = ::is_nan(*(&x as &f64)); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:132:13 + --> $DIR/suggestion.rs:133:13 | LL | let _ = { &{ 3.3 } } != &f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -378,7 +378,7 @@ LL + let _ = !::is_nan(*{ &{ 3.3 } }); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:134:13 + --> $DIR/suggestion.rs:135:13 | LL | let _ = &&&3.3 == &&&f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^ @@ -390,7 +390,7 @@ LL + let _ = ::is_nan(***&&&3.3); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:137:13 + --> $DIR/suggestion.rs:138:13 | LL | let _ = &&&f64::NAN != b; | ^^^^^^^^^^^^^^^^ @@ -402,7 +402,7 @@ LL + let _ = !::is_nan(***b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:140:13 + --> $DIR/suggestion.rs:141:13 | LL | let _ = b != &f64::NAN; | ^^^^^^^^^^^^^^ @@ -414,7 +414,7 @@ LL + let _ = !::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:143:13 + --> $DIR/suggestion.rs:144:13 | LL | let _ = &(::NAN) == b; | ^^^^^^^^^^^^^^^^^^ @@ -426,7 +426,7 @@ LL + let _ = ::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:146:13 + --> $DIR/suggestion.rs:147:13 | LL | let _ = b != &{ f64::NAN }; | ^^^^^^^^^^^^^^^^^^ @@ -438,7 +438,7 @@ LL + let _ = !::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:149:13 + --> $DIR/suggestion.rs:150:13 | LL | let _ = &{ &{ f64::NAN } } == b; | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -450,7 +450,7 @@ LL + let _ = ::is_nan(**b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:153:13 + --> $DIR/suggestion.rs:154:13 | LL | let _ = b != &f16::NAN; | ^^^^^^^^^^^^^^ @@ -462,7 +462,7 @@ LL + let _ = !::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:156:13 + --> $DIR/suggestion.rs:157:13 | LL | let _ = b == &f128::NAN; | ^^^^^^^^^^^^^^^ @@ -474,7 +474,7 @@ LL + let _ = ::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:160:13 + --> $DIR/suggestion.rs:161:13 | LL | let _ = borrowed!() != &f64::NAN; | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -486,7 +486,7 @@ LL + let _ = !::is_nan(*borrowed!()); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:170:13 + --> $DIR/suggestion.rs:171:13 | LL | let _ = b != &nan!(); | ^^^^^^^^^^^^ @@ -498,7 +498,7 @@ LL + let _ = !::std::primitive::f64::is_nan(*b); | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:181:12 + --> $DIR/suggestion.rs:182:12 | LL | if b != &::core::primitive::f64::NAN {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -510,7 +510,7 @@ LL + if !<::core::primitive::f64>::is_nan(*b) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:184:12 + --> $DIR/suggestion.rs:185:12 | LL | if c != &Float::NAN {} | ^^^^^^^^^^^^^^^^ @@ -522,7 +522,7 @@ LL + if !::is_nan(*c) {} | warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/suggestion.rs:187:12 + --> $DIR/suggestion.rs:188:12 | LL | if d != &Float::r#NAN {} | ^^^^^^^^^^^^^^^^^^ @@ -533,5 +533,29 @@ LL - if d != &Float::r#NAN {} LL + if !::is_nan(*d) {} | -warning: 44 warnings emitted +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/suggestion.rs:211:23 + | +LL | let _: bool = x == f64::NAN; + | ^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _: bool = x == f64::NAN; +LL + let _: bool = ::is_nan(x); + | + +warning: incorrect NaN comparison, NaN cannot be directly compared to itself + --> $DIR/suggestion.rs:215:23 + | +LL | let _: bool = x == &f64::NAN; + | ^^^^^^^^^^^^^^ + | +help: use the `is_nan` method instead + | +LL - let _: bool = x == &f64::NAN; +LL + let _: bool = ::is_nan(*x); + | + +warning: 46 warnings emitted From 3b9350aa2ca884c952656a084dd686b62483170c Mon Sep 17 00:00:00 2001 From: Yu Date: Mon, 19 Oct 2026 23:05:31 +0800 Subject: [PATCH 7/7] lint: skip NaN diagnostics for custom comparisons --- compiler/rustc_lint/src/types.rs | 39 +++++----- .../custom-comparisons.rs | 73 +++++++++++++++++++ .../custom-partial-eq.rs | 25 ------- .../custom-partial-eq.stderr | 27 ------- 4 files changed, 92 insertions(+), 72 deletions(-) create mode 100644 tests/ui/lint/invalid-nan-comparisons/custom-comparisons.rs delete mode 100644 tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.rs delete mode 100644 tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.stderr diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index c38e77c5fe6c7..1b38860aef443 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -310,9 +310,6 @@ fn lint_nan<'tcx>( deref_count += 1; ty = inner; } - if !matches!(*ty.kind(), ty::Float(fty) if fty == float_ty) { - return InvalidNanComparisonsSuggestion::Spanless; - } let neg = if is_ne { "!" } else { "" }; let Some(method_path) = nan_method_path(cx, nan, float_ty, cmp.span) else { return InvalidNanComparisonsSuggestion::Spanless; @@ -331,24 +328,26 @@ fn lint_nan<'tcx>( } } - let l_nan = nan_ty(cx, l); - let r_nan = nan_ty(cx, r); - let is_ne = binop == hir::BinOpKind::Ne; + let Some((value, nan, float_ty)) = nan_ty(cx, l) + .map(|float_ty| (r, l, float_ty)) + .or_else(|| nan_ty(cx, r).map(|float_ty| (l, r, float_ty))) + else { + return; + }; + let typeck_results = cx.typeck_results(); + // Custom comparison implementations need not follow primitive float NaN semantics. + if ![l, r].into_iter().all(|operand| { + typeck_results + .expr_ty_opt(operand) + .is_some_and(|ty| matches!(*ty.peel_refs().kind(), ty::Float(fty) if fty == float_ty)) + }) { + return; + } let lint = match binop { - hir::BinOpKind::Eq | hir::BinOpKind::Ne => { - let Some((value, nan, float_ty)) = l_nan - .map(|float_ty| (r, l, float_ty)) - .or_else(|| r_nan.map(|float_ty| (l, r, float_ty))) - else { - return; - }; - InvalidNanComparisons::EqNe { - suggestion: eq_ne_suggestion(cx, e, value, nan, float_ty, is_ne), - } - } - hir::BinOpKind::Lt | hir::BinOpKind::Le | hir::BinOpKind::Gt | hir::BinOpKind::Ge - if l_nan.is_some() || r_nan.is_some() => - { + hir::BinOpKind::Eq | hir::BinOpKind::Ne => InvalidNanComparisons::EqNe { + suggestion: eq_ne_suggestion(cx, e, value, nan, float_ty, binop == hir::BinOpKind::Ne), + }, + hir::BinOpKind::Lt | hir::BinOpKind::Le | hir::BinOpKind::Gt | hir::BinOpKind::Ge => { InvalidNanComparisons::LtLeGtGe } _ => return, diff --git a/tests/ui/lint/invalid-nan-comparisons/custom-comparisons.rs b/tests/ui/lint/invalid-nan-comparisons/custom-comparisons.rs new file mode 100644 index 0000000000000..ccdb7e0d31859 --- /dev/null +++ b/tests/ui/lint/invalid-nan-comparisons/custom-comparisons.rs @@ -0,0 +1,73 @@ +//@ check-pass + +#![deny(invalid_nan_comparisons)] + +use std::cmp::Ordering; + +struct NotFloat; + +impl PartialEq for NotFloat { + fn eq(&self, other: &f64) -> bool { + other.is_nan() + } +} + +impl PartialEq for f64 { + fn eq(&self, other: &NotFloat) -> bool { + other == self + } +} + +impl PartialOrd for NotFloat { + fn partial_cmp(&self, other: &f64) -> Option { + other.is_nan().then_some(Ordering::Equal) + } +} + +impl PartialOrd for f64 { + fn partial_cmp(&self, other: &NotFloat) -> Option { + other.partial_cmp(self).map(Ordering::reverse) + } +} + +fn main() { + let value = NotFloat; + let _ = value == f64::NAN; + let _ = value != f64::NAN; + let _ = value < f64::NAN; + let _ = value <= f64::NAN; + let _ = value > f64::NAN; + let _ = value >= f64::NAN; + + let _ = f64::NAN == value; + let _ = f64::NAN != value; + let _ = f64::NAN < value; + let _ = f64::NAN <= value; + let _ = f64::NAN > value; + let _ = f64::NAN >= value; + + let _ = &value == &f64::NAN; + let _ = &value != &f64::NAN; + let _ = &value < &f64::NAN; + let _ = &value <= &f64::NAN; + let _ = &value > &f64::NAN; + let _ = &value >= &f64::NAN; + + let _ = &f64::NAN == &value; + let _ = &f64::NAN != &value; + let _ = &f64::NAN < &value; + let _ = &f64::NAN <= &value; + let _ = &f64::NAN > &value; + let _ = &f64::NAN >= &value; + + generic(value); +} + +fn generic>(value: T) { + let _ = value == f64::NAN; + let _ = value != f64::NAN; + let _ = value < f64::NAN; + let _ = value <= f64::NAN; + let _ = value > f64::NAN; + let _ = value >= f64::NAN; +} diff --git a/tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.rs b/tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.rs deleted file mode 100644 index be3351916ddc4..0000000000000 --- a/tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.rs +++ /dev/null @@ -1,25 +0,0 @@ -//@ check-pass - -struct NotFloat; - -impl PartialEq for NotFloat { - fn eq(&self, _: &f64) -> bool { - true - } -} - -impl PartialEq for f64 { - fn eq(&self, _: &NotFloat) -> bool { - true - } -} - -fn main() { - let value = NotFloat; - let _ = value == f64::NAN; - //~^ WARN incorrect NaN comparison - let _ = f64::NAN != value; - //~^ WARN incorrect NaN comparison - let _ = &value != &f64::NAN; - //~^ WARN incorrect NaN comparison -} diff --git a/tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.stderr b/tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.stderr deleted file mode 100644 index f0c914dccdce6..0000000000000 --- a/tests/ui/lint/invalid-nan-comparisons/custom-partial-eq.stderr +++ /dev/null @@ -1,27 +0,0 @@ -warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/custom-partial-eq.rs:19:13 - | -LL | let _ = value == f64::NAN; - | ^^^^^^^^^^^^^^^^^ - | - = help: use the `is_nan` method instead - = note: `#[warn(invalid_nan_comparisons)]` on by default - -warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/custom-partial-eq.rs:21:13 - | -LL | let _ = f64::NAN != value; - | ^^^^^^^^^^^^^^^^^ - | - = help: use the `is_nan` method instead - -warning: incorrect NaN comparison, NaN cannot be directly compared to itself - --> $DIR/custom-partial-eq.rs:23:13 - | -LL | let _ = &value != &f64::NAN; - | ^^^^^^^^^^^^^^^^^^^ - | - = help: use the `is_nan` method instead - -warning: 3 warnings emitted -