Skip to content

Commit 3c738b2

Browse files
committed
Add float cmp tests for arrays
1 parent d3167c6 commit 3c738b2

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

tests/ui/float_cmp.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#![warn(clippy::float_cmp)]
2-
#![allow(unused, clippy::no_effect, clippy::unnecessary_operation, clippy::cast_lossless)]
2+
#![allow(
3+
unused,
4+
clippy::no_effect,
5+
clippy::unnecessary_operation,
6+
clippy::cast_lossless,
7+
clippy::many_single_char_names
8+
)]
39

410
use std::ops::Add;
511

@@ -77,12 +83,20 @@ fn main() {
7783

7884
assert_eq!(a, b); // no errors
7985

86+
const ZERO_ARRAY: [f32; 2] = [0.0, 0.0];
87+
const NON_ZERO_ARRAY: [f32; 2] = [0.0, 0.1];
88+
89+
let i = 0;
90+
let j = 1;
91+
92+
ZERO_ARRAY[i] == NON_ZERO_ARRAY[j]; // ok, because lhs is zero regardless of i
93+
NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
94+
8095
let a1: [f32; 1] = [0.0];
8196
let a2: [f32; 1] = [1.1];
8297

83-
assert_eq!(a1[0], a2[0]);
84-
85-
assert_eq!(&a1[0], &a2[0]);
98+
a1 == a2;
99+
a1[0] == a2[0];
86100

87101
// no errors - comparing signums is ok
88102
let x32 = 3.21f32;

tests/ui/float_cmp.stderr

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,75 @@
11
error: strict comparison of `f32` or `f64`
2-
--> $DIR/float_cmp.rs:59:5
2+
--> $DIR/float_cmp.rs:65:5
33
|
44
LL | ONE as f64 != 2.0;
55
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(ONE as f64 - 2.0).abs() > error`
66
|
77
= note: `-D clippy::float-cmp` implied by `-D warnings`
88
note: `f32::EPSILON` and `f64::EPSILON` are available.
9-
--> $DIR/float_cmp.rs:59:5
9+
--> $DIR/float_cmp.rs:65:5
1010
|
1111
LL | ONE as f64 != 2.0;
1212
| ^^^^^^^^^^^^^^^^^
1313

1414
error: strict comparison of `f32` or `f64`
15-
--> $DIR/float_cmp.rs:64:5
15+
--> $DIR/float_cmp.rs:70:5
1616
|
1717
LL | x == 1.0;
1818
| ^^^^^^^^ help: consider comparing them within some error: `(x - 1.0).abs() < error`
1919
|
2020
note: `f32::EPSILON` and `f64::EPSILON` are available.
21-
--> $DIR/float_cmp.rs:64:5
21+
--> $DIR/float_cmp.rs:70:5
2222
|
2323
LL | x == 1.0;
2424
| ^^^^^^^^
2525

2626
error: strict comparison of `f32` or `f64`
27-
--> $DIR/float_cmp.rs:67:5
27+
--> $DIR/float_cmp.rs:73:5
2828
|
2929
LL | twice(x) != twice(ONE as f64);
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(twice(x) - twice(ONE as f64)).abs() > error`
3131
|
3232
note: `f32::EPSILON` and `f64::EPSILON` are available.
33-
--> $DIR/float_cmp.rs:67:5
33+
--> $DIR/float_cmp.rs:73:5
3434
|
3535
LL | twice(x) != twice(ONE as f64);
3636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3737

3838
error: strict comparison of `f32` or `f64`
39-
--> $DIR/float_cmp.rs:83:5
39+
--> $DIR/float_cmp.rs:93:5
4040
|
41-
LL | assert_eq!(a1[0], a2[0]);
42-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
41+
LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(NON_ZERO_ARRAY[i] - NON_ZERO_ARRAY[j]).abs() < error`
4343
|
44-
note: `std::f32::EPSILON` and `std::f64::EPSILON` are available.
45-
--> $DIR/float_cmp.rs:83:5
44+
note: `f32::EPSILON` and `f64::EPSILON` are available.
45+
--> $DIR/float_cmp.rs:93:5
46+
|
47+
LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
48+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
49+
50+
error: strict comparison of `f32` or `f64`
51+
--> $DIR/float_cmp.rs:98:5
52+
|
53+
LL | a1 == a2;
54+
| ^^^^^^^^
55+
|
56+
note: `f32::EPSILON` and `f64::EPSILON` are available.
57+
--> $DIR/float_cmp.rs:98:5
4658
|
47-
LL | assert_eq!(a1[0], a2[0]);
48-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
49-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
59+
LL | a1 == a2;
60+
| ^^^^^^^^
5061

5162
error: strict comparison of `f32` or `f64`
52-
--> $DIR/float_cmp.rs:85:5
63+
--> $DIR/float_cmp.rs:99:5
5364
|
54-
LL | assert_eq!(&a1[0], &a2[0]);
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
65+
LL | a1[0] == a2[0];
66+
| ^^^^^^^^^^^^^^ help: consider comparing them within some error: `(a1[0] - a2[0]).abs() < error`
5667
|
57-
note: `std::f32::EPSILON` and `std::f64::EPSILON` are available.
58-
--> $DIR/float_cmp.rs:85:5
68+
note: `f32::EPSILON` and `f64::EPSILON` are available.
69+
--> $DIR/float_cmp.rs:99:5
5970
|
60-
LL | assert_eq!(&a1[0], &a2[0]);
61-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
62-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
71+
LL | a1[0] == a2[0];
72+
| ^^^^^^^^^^^^^^
6373

64-
error: aborting due to 5 previous errors
74+
error: aborting due to 6 previous errors
6575

0 commit comments

Comments
 (0)