Skip to content

Commit

Permalink
Rollup merge of rust-lang#132439 - tgross35:f16-f128-nan-lint, r=jiey…
Browse files Browse the repository at this point in the history
…ouxu

Add `f16` and `f128` to `invalid_nan_comparison`

Currently `f32_nan` and `f64_nan` are used to provide the `invalid_nan_comparison` lint. Since we have `f16_nan` and `f128_nan`, hook these up so the new float types get the same lints.
  • Loading branch information
jieyouxu authored Nov 1, 2024
2 parents e9fa793 + 3afbe4f commit 52a9a1b
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 30 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ fn lint_nan<'tcx>(
return false;
};

matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::f32_nan | sym::f64_nan))
matches!(
cx.tcx.get_diagnostic_name(def_id),
Some(sym::f16_nan | sym::f32_nan | sym::f64_nan | sym::f128_nan)
)
}
_ => false,
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/lint/invalid-nan-comparison-suggestion.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
//@ check-pass
//@ run-rustfix

#![feature(f16, f128)]

fn main() {
let x = 5f16;
let _ = x.is_nan();
//~^ WARN incorrect NaN comparison
let _ = !x.is_nan();
//~^ WARN incorrect NaN comparison

let x = 5f32;
let _ = x.is_nan();
//~^ WARN incorrect NaN comparison
Expand All @@ -14,6 +22,12 @@ fn main() {
let _ = !x.is_nan();
//~^ WARN incorrect NaN comparison

let x = 5f128;
let _ = x.is_nan();
//~^ WARN incorrect NaN comparison
let _ = !x.is_nan();
//~^ WARN incorrect NaN comparison

let b = &2.3f32;
if !b.is_nan() {}
//~^ WARN incorrect NaN comparison
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/lint/invalid-nan-comparison-suggestion.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
//@ check-pass
//@ run-rustfix

#![feature(f16, f128)]

fn main() {
let x = 5f16;
let _ = x == f16::NAN;
//~^ WARN incorrect NaN comparison
let _ = x != f16::NAN;
//~^ WARN incorrect NaN comparison

let x = 5f32;
let _ = x == f32::NAN;
//~^ WARN incorrect NaN comparison
Expand All @@ -14,6 +22,12 @@ fn main() {
let _ = x != f64::NAN;
//~^ WARN incorrect NaN comparison

let x = 5f128;
let _ = x == f128::NAN;
//~^ WARN incorrect NaN comparison
let _ = x != f128::NAN;
//~^ WARN incorrect NaN comparison

let b = &2.3f32;
if b != &f32::NAN {}
//~^ WARN incorrect NaN comparison
Expand Down
70 changes: 59 additions & 11 deletions tests/ui/lint/invalid-nan-comparison-suggestion.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:6:13
--> $DIR/invalid-nan-comparison-suggestion.rs:8:13
|
LL | let _ = x == f32::NAN;
LL | let _ = x == f16::NAN;
| ^^^^^^^^^^^^^
|
= note: `#[warn(invalid_nan_comparisons)]` on by default
help: use `f32::is_nan()` or `f64::is_nan()` instead
|
LL - let _ = x == f16::NAN;
LL + let _ = x.is_nan();
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:10:13
|
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();
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:14:13
|
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();
|

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:16:13
|
LL | let _ = x != f32::NAN;
| ^^^^^^^^^^^^^
Expand All @@ -24,7 +48,7 @@ LL + let _ = !x.is_nan();
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:12:13
--> $DIR/invalid-nan-comparison-suggestion.rs:20:13
|
LL | let _ = x == f64::NAN;
| ^^^^^^^^^^^^^
Expand All @@ -36,7 +60,7 @@ LL + let _ = x.is_nan();
|

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:22:13
|
LL | let _ = x != f64::NAN;
| ^^^^^^^^^^^^^
Expand All @@ -48,7 +72,31 @@ LL + let _ = !x.is_nan();
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:18:8
--> $DIR/invalid-nan-comparison-suggestion.rs:26:13
|
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();
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:28:13
|
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();
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:32:8
|
LL | if b != &f32::NAN {}
| ^^^^^^^^^^^^^^
Expand All @@ -60,7 +108,7 @@ LL + if !b.is_nan() {}
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:22:8
--> $DIR/invalid-nan-comparison-suggestion.rs:36:8
|
LL | if b != { &f32::NAN } {}
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -72,7 +120,7 @@ LL + if !b.is_nan() {}
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:26:9
--> $DIR/invalid-nan-comparison-suggestion.rs:40:9
|
LL | / b != {
LL | |
Expand All @@ -87,7 +135,7 @@ LL + !b.is_nan();
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:35:13
--> $DIR/invalid-nan-comparison-suggestion.rs:49:13
|
LL | let _ = nan!() == number!();
| ^^^^^^^^^^^^^^^^^^^
Expand All @@ -99,7 +147,7 @@ LL + let _ = number!().is_nan();
|

warning: incorrect NaN comparison, NaN cannot be directly compared to itself
--> $DIR/invalid-nan-comparison-suggestion.rs:37:13
--> $DIR/invalid-nan-comparison-suggestion.rs:51:13
|
LL | let _ = number!() != nan!();
| ^^^^^^^^^^^^^^^^^^^
Expand All @@ -110,5 +158,5 @@ LL - let _ = number!() != nan!();
LL + let _ = !number!().is_nan();
|

warning: 9 warnings emitted
warning: 13 warnings emitted

46 changes: 46 additions & 0 deletions tests/ui/lint/invalid-nan-comparison.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
//@ check-pass

#![feature(f16, f128)]

fn main() {
f16();
f32();
f64();
f128();
}

const TEST: bool = 5f32 == f32::NAN;
//~^ WARN incorrect NaN comparison

fn f16() {
macro_rules! number { () => { 5f16 }; }
let x = number!();
x == f16::NAN;
//~^ WARN incorrect NaN comparison
x != f16::NAN;
//~^ WARN incorrect NaN comparison
x < f16::NAN;
//~^ WARN incorrect NaN comparison
x > f16::NAN;
//~^ WARN incorrect NaN comparison
x <= f16::NAN;
//~^ WARN incorrect NaN comparison
x >= f16::NAN;
//~^ WARN incorrect NaN comparison
number!() == f16::NAN;
//~^ WARN incorrect NaN comparison
f16::NAN != number!();
//~^ WARN incorrect NaN comparison
}

fn f32() {
macro_rules! number { () => { 5f32 }; }
let x = number!();
Expand Down Expand Up @@ -49,3 +74,24 @@ fn f64() {
f64::NAN != number!();
//~^ WARN incorrect NaN comparison
}

fn f128() {
macro_rules! number { () => { 5f128 }; }
let x = number!();
x == f128::NAN;
//~^ WARN incorrect NaN comparison
x != f128::NAN;
//~^ WARN incorrect NaN comparison
x < f128::NAN;
//~^ WARN incorrect NaN comparison
x > f128::NAN;
//~^ WARN incorrect NaN comparison
x <= f128::NAN;
//~^ WARN incorrect NaN comparison
x >= f128::NAN;
//~^ WARN incorrect NaN comparison
number!() == f128::NAN;
//~^ WARN incorrect NaN comparison
f128::NAN != number!();
//~^ WARN incorrect NaN comparison
}
Loading

0 comments on commit 52a9a1b

Please sign in to comment.