]> git.lizzy.rs Git - rust.git/blob - tests/ui/float_cmp.rs
Adding try_err lint
[rust.git] / tests / ui / float_cmp.rs
1 #![warn(clippy::float_cmp)]
2 #![allow(unused, clippy::no_effect, clippy::unnecessary_operation, clippy::cast_lossless)]
3
4 use std::ops::Add;
5
6 const ZERO: f32 = 0.0;
7 const ONE: f32 = ZERO + 1.0;
8
9 fn twice<T>(x: T) -> T
10 where
11     T: Add<T, Output = T>,
12     T: Copy,
13 {
14     x + x
15 }
16
17 fn eq_fl(x: f32, y: f32) -> bool {
18     if x.is_nan() {
19         y.is_nan()
20     } else {
21         x == y
22     } // no error, inside "eq" fn
23 }
24
25 fn fl_eq(x: f32, y: f32) -> bool {
26     if x.is_nan() {
27         y.is_nan()
28     } else {
29         x == y
30     } // no error, inside "eq" fn
31 }
32
33 struct X {
34     val: f32,
35 }
36
37 impl PartialEq for X {
38     fn eq(&self, o: &X) -> bool {
39         if self.val.is_nan() {
40             o.val.is_nan()
41         } else {
42             self.val == o.val // no error, inside "eq" fn
43         }
44     }
45 }
46
47 fn main() {
48     ZERO == 0f32; //no error, comparison with zero is ok
49     1.0f32 != ::std::f32::INFINITY; // also comparison with infinity
50     1.0f32 != ::std::f32::NEG_INFINITY; // and negative infinity
51     ZERO == 0.0; //no error, comparison with zero is ok
52     ZERO + ZERO != 1.0; //no error, comparison with zero is ok
53
54     ONE == 1f32;
55     ONE == 1.0 + 0.0;
56     ONE + ONE == ZERO + ONE + ONE;
57     ONE != 2.0;
58     ONE != 0.0; // no error, comparison with zero is ok
59     twice(ONE) != ONE;
60     ONE as f64 != 2.0;
61     ONE as f64 != 0.0; // no error, comparison with zero is ok
62
63     let x: f64 = 1.0;
64
65     x == 1.0;
66     x != 0f64; // no error, comparison with zero is ok
67
68     twice(x) != twice(ONE as f64);
69
70     x < 0.0; // no errors, lower or greater comparisons need no fuzzyness
71     x > 0.0;
72     x <= 0.0;
73     x >= 0.0;
74
75     let xs: [f32; 1] = [0.0];
76     let a: *const f32 = xs.as_ptr();
77     let b: *const f32 = xs.as_ptr();
78
79     assert_eq!(a, b); // no errors
80 }