Skip to content

Commit f132376

Browse files
committed
Support f16 in float_cmp lint
1 parent a4b8e9a commit f132376

File tree

5 files changed

+79
-36
lines changed

5 files changed

+79
-36
lines changed

clippy_lints/src/operators/float_cmp.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,34 @@ fn get_lint_and_message(is_local: bool, is_comparing_arrays: bool) -> (&'static
6868
(
6969
FLOAT_CMP,
7070
if is_comparing_arrays {
71-
"strict comparison of `f32` or `f64` arrays"
71+
"strict comparison of floating point arrays"
7272
} else {
73-
"strict comparison of `f32` or `f64`"
73+
"strict comparison of floating point values"
7474
},
7575
)
7676
} else {
7777
(
7878
FLOAT_CMP_CONST,
7979
if is_comparing_arrays {
80-
"strict comparison of `f32` or `f64` constant arrays"
80+
"strict comparison of floating point constant arrays"
8181
} else {
82-
"strict comparison of `f32` or `f64` constant"
82+
"strict comparison of floating point constant"
8383
},
8484
)
8585
}
8686
}
8787

8888
fn is_allowed(val: &Constant) -> bool {
8989
match val {
90-
// FIXME(f16_f128): add when equality check is available on all platforms
90+
&Constant::F16(f) => f == 0.0 || f.is_infinite(),
9191
&Constant::F32(f) => f == 0.0 || f.is_infinite(),
9292
&Constant::F64(f) => f == 0.0 || f.is_infinite(),
93+
&Constant::F128(f) => f == 0.0 || f.is_infinite(),
9394
Constant::Vec(vec) => vec.iter().all(|f| match f {
95+
Constant::F16(f) => *f == 0.0 || (*f).is_infinite(),
9496
Constant::F32(f) => *f == 0.0 || (*f).is_infinite(),
9597
Constant::F64(f) => *f == 0.0 || (*f).is_infinite(),
98+
Constant::F128(f) => *f == 0.0 || (*f).is_infinite(),
9699
_ => false,
97100
}),
98101
_ => false,

tests/ui/float_cmp.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// FIXME(f16_f128): const casting is not yet supported for these types. Add when available.
2-
1+
#![feature(f128)]
2+
#![feature(f16)]
33
#![warn(clippy::float_cmp)]
44
#![allow(
55
unused,
@@ -71,6 +71,10 @@ fn main() {
7171
twice(ONE) != ONE;
7272
ONE as f64 != 2.0;
7373
//~^ float_cmp
74+
ONE as f16 != 2.0;
75+
//~^ float_cmp
76+
ONE as f128 != 2.0;
77+
//~^ float_cmp
7478

7579
ONE as f64 != 0.0; // no error, comparison with zero is ok
7680

tests/ui/float_cmp.stderr

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: strict comparison of `f32` or `f64`
1+
error: strict comparison of floating point values
22
--> tests/ui/float_cmp.rs:72:5
33
|
44
LL | ONE as f64 != 2.0;
@@ -7,35 +7,47 @@ LL | ONE as f64 != 2.0;
77
= note: `-D clippy::float-cmp` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::float_cmp)]`
99

10-
error: strict comparison of `f32` or `f64`
11-
--> tests/ui/float_cmp.rs:79:5
10+
error: strict comparison of floating point values
11+
--> tests/ui/float_cmp.rs:74:5
12+
|
13+
LL | ONE as f16 != 2.0;
14+
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE as f16 - 2.0).abs() > error_margin`
15+
16+
error: strict comparison of floating point values
17+
--> tests/ui/float_cmp.rs:76:5
18+
|
19+
LL | ONE as f128 != 2.0;
20+
| ^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE as f128 - 2.0).abs() > error_margin`
21+
22+
error: strict comparison of floating point values
23+
--> tests/ui/float_cmp.rs:83:5
1224
|
1325
LL | x == 1.0;
1426
| ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 1.0).abs() < error_margin`
1527

16-
error: strict comparison of `f32` or `f64`
17-
--> tests/ui/float_cmp.rs:84:5
28+
error: strict comparison of floating point values
29+
--> tests/ui/float_cmp.rs:88:5
1830
|
1931
LL | twice(x) != twice(ONE as f64);
2032
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(twice(x) - twice(ONE as f64)).abs() > error_margin`
2133

22-
error: strict comparison of `f32` or `f64`
23-
--> tests/ui/float_cmp.rs:105:5
34+
error: strict comparison of floating point values
35+
--> tests/ui/float_cmp.rs:109:5
2436
|
2537
LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
2638
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(NON_ZERO_ARRAY[i] - NON_ZERO_ARRAY[j]).abs() < error_margin`
2739

28-
error: strict comparison of `f32` or `f64` arrays
29-
--> tests/ui/float_cmp.rs:111:5
40+
error: strict comparison of floating point arrays
41+
--> tests/ui/float_cmp.rs:115:5
3042
|
3143
LL | a1 == a2;
3244
| ^^^^^^^^
3345

34-
error: strict comparison of `f32` or `f64`
35-
--> tests/ui/float_cmp.rs:114:5
46+
error: strict comparison of floating point values
47+
--> tests/ui/float_cmp.rs:118:5
3648
|
3749
LL | a1[0] == a2[0];
3850
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(a1[0] - a2[0]).abs() < error_margin`
3951

40-
error: aborting due to 6 previous errors
52+
error: aborting due to 8 previous errors
4153

tests/ui/float_cmp_const.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@no-rustfix: suggestions have an error margin placeholder
2+
#![feature(f128)]
3+
#![feature(f16)]
24
#![warn(clippy::float_cmp_const)]
35
#![allow(clippy::float_cmp)]
46
#![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]
@@ -67,4 +69,14 @@ fn main() {
6769
// has errors
6870
NON_ZERO_ARRAY == NON_ZERO_ARRAY2;
6971
//~^ float_cmp_const
72+
73+
const F16_NON_ZERO_ARRAY: [f16; 3] = [0.0, 0.1, 0.2];
74+
const F16_NON_ZERO_ARRAY2: [f16; 3] = [0.2, 0.1, 0.0];
75+
F16_NON_ZERO_ARRAY == F16_NON_ZERO_ARRAY2;
76+
//~^ float_cmp_const
77+
78+
const F128_NON_ZERO_ARRAY: [f128; 3] = [0.0, 0.1, 0.2];
79+
const F128_NON_ZERO_ARRAY2: [f128; 3] = [0.2, 0.1, 0.0];
80+
F128_NON_ZERO_ARRAY == F128_NON_ZERO_ARRAY2;
81+
//~^ float_cmp_const
7082
}

tests/ui/float_cmp_const.stderr

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,65 @@
1-
error: strict comparison of `f32` or `f64` constant
2-
--> tests/ui/float_cmp_const.rs:15:5
1+
error: strict comparison of floating point constant
2+
--> tests/ui/float_cmp_const.rs:17:5
33
|
44
LL | 1f32 == ONE;
55
| ^^^^^^^^^^^ help: consider comparing them within some margin of error: `(1f32 - ONE).abs() < error_margin`
66
|
77
= note: `-D clippy::float-cmp-const` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::float_cmp_const)]`
99

10-
error: strict comparison of `f32` or `f64` constant
11-
--> tests/ui/float_cmp_const.rs:18:5
10+
error: strict comparison of floating point constant
11+
--> tests/ui/float_cmp_const.rs:20:5
1212
|
1313
LL | TWO == ONE;
1414
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(TWO - ONE).abs() < error_margin`
1515

16-
error: strict comparison of `f32` or `f64` constant
17-
--> tests/ui/float_cmp_const.rs:21:5
16+
error: strict comparison of floating point constant
17+
--> tests/ui/float_cmp_const.rs:23:5
1818
|
1919
LL | TWO != ONE;
2020
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(TWO - ONE).abs() > error_margin`
2121

22-
error: strict comparison of `f32` or `f64` constant
23-
--> tests/ui/float_cmp_const.rs:24:5
22+
error: strict comparison of floating point constant
23+
--> tests/ui/float_cmp_const.rs:26:5
2424
|
2525
LL | ONE + ONE == TWO;
2626
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE + ONE - TWO).abs() < error_margin`
2727

28-
error: strict comparison of `f32` or `f64` constant
29-
--> tests/ui/float_cmp_const.rs:28:5
28+
error: strict comparison of floating point constant
29+
--> tests/ui/float_cmp_const.rs:30:5
3030
|
3131
LL | x as f32 == ONE;
3232
| ^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x as f32 - ONE).abs() < error_margin`
3333

34-
error: strict comparison of `f32` or `f64` constant
35-
--> tests/ui/float_cmp_const.rs:32:5
34+
error: strict comparison of floating point constant
35+
--> tests/ui/float_cmp_const.rs:34:5
3636
|
3737
LL | v == ONE;
3838
| ^^^^^^^^ help: consider comparing them within some margin of error: `(v - ONE).abs() < error_margin`
3939

40-
error: strict comparison of `f32` or `f64` constant
41-
--> tests/ui/float_cmp_const.rs:35:5
40+
error: strict comparison of floating point constant
41+
--> tests/ui/float_cmp_const.rs:37:5
4242
|
4343
LL | v != ONE;
4444
| ^^^^^^^^ help: consider comparing them within some margin of error: `(v - ONE).abs() > error_margin`
4545

46-
error: strict comparison of `f32` or `f64` constant arrays
47-
--> tests/ui/float_cmp_const.rs:68:5
46+
error: strict comparison of floating point constant arrays
47+
--> tests/ui/float_cmp_const.rs:70:5
4848
|
4949
LL | NON_ZERO_ARRAY == NON_ZERO_ARRAY2;
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5151

52-
error: aborting due to 8 previous errors
52+
error: strict comparison of floating point constant arrays
53+
--> tests/ui/float_cmp_const.rs:75:5
54+
|
55+
LL | F16_NON_ZERO_ARRAY == F16_NON_ZERO_ARRAY2;
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
57+
58+
error: strict comparison of floating point constant arrays
59+
--> tests/ui/float_cmp_const.rs:80:5
60+
|
61+
LL | F128_NON_ZERO_ARRAY == F128_NON_ZERO_ARRAY2;
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63+
64+
error: aborting due to 10 previous errors
5365

0 commit comments

Comments
 (0)