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