]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/float_cmp.rs
don't lint on comparing `*const f32`s
[rust.git] / tests / compile-fail / 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; //~ERROR ==-comparison of f32 or f64
44     ONE == (1.0 + 0.0); //~ERROR ==-comparison of f32 or f64
45
46     ONE + ONE == (ZERO + ONE + ONE); //~ERROR ==-comparison of f32 or f64
47
48     ONE != 2.0; //~ERROR !=-comparison of f32 or f64
49     ONE != 0.0; // no error, comparison with zero is ok
50     twice(ONE) != ONE; //~ERROR !=-comparison of f32 or f64
51     ONE as f64 != 2.0; //~ERROR !=-comparison of f32 or f64
52     ONE as f64 != 0.0; // no error, comparison with zero is ok
53
54     let x : f64 = 1.0;
55
56     x == 1.0; //~ERROR ==-comparison of f32 or f64
57     x != 0f64; // no error, comparison with zero is ok
58
59     twice(x) != twice(ONE as f64); //~ERROR !=-comparison of f32 or f64
60
61
62     x < 0.0; // no errors, lower or greater comparisons need no fuzzyness
63     x > 0.0;
64     x <= 0.0;
65     x >= 0.0;
66
67     let xs : [f32; 1] = [0.0];
68     let a: *const f32 = xs.as_ptr();
69     let b: *const f32 = xs.as_ptr();
70
71     assert!(a == b); // no errors
72 }