rust-clippy/tests/compile-fail/float_cmp.rs

65 lines
1.6 KiB
Rust
Raw Normal View History

#![feature(plugin)]
#![plugin(clippy)]
2015-09-02 08:30:11 +00:00
#![deny(float_cmp)]
#![allow(unused)]
use std::ops::Add;
const ZERO : f32 = 0.0;
const ONE : f32 = ZERO + 1.0;
fn twice<T>(x : T) -> T where T : Add<T, Output = T>, T : Copy {
x + x
}
2015-09-02 08:30:11 +00:00
fn eq_fl(x: f32, y: f32) -> bool {
if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
}
fn fl_eq(x: f32, y: f32) -> bool {
if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
}
struct X { val: f32 }
impl PartialEq for X {
fn eq(&self, o: &X) -> bool {
if self.val.is_nan() {
o.val.is_nan()
} else {
self.val == o.val // no error, inside "eq" fn
}
}
}
fn main() {
2015-08-17 10:06:56 +00:00
ZERO == 0f32; //no error, comparison with zero is ok
ZERO == 0.0; //no error, comparison with zero is ok
ZERO + ZERO != 1.0; //~ERROR !=-comparison of f32 or f64
2015-08-17 10:06:56 +00:00
ONE == 1f32; //~ERROR ==-comparison of f32 or f64
ONE == (1.0 + 0.0); //~ERROR ==-comparison of f32 or f64
ONE + ONE == (ZERO + ONE + ONE); //~ERROR ==-comparison of f32 or f64
ONE != 2.0; //~ERROR !=-comparison of f32 or f64
ONE != 0.0; // no error, comparison with zero is ok
twice(ONE) != ONE; //~ERROR !=-comparison of f32 or f64
2015-08-17 10:06:56 +00:00
ONE as f64 != 2.0; //~ERROR !=-comparison of f32 or f64
ONE as f64 != 0.0; // no error, comparison with zero is ok
let x : f64 = 1.0;
x == 1.0; //~ERROR ==-comparison of f32 or f64
2015-08-17 10:06:56 +00:00
x != 0f64; // no error, comparison with zero is ok
twice(x) != twice(ONE as f64); //~ERROR !=-comparison of f32 or f64
2015-08-17 10:06:56 +00:00
x < 0.0; // no errors, lower or greater comparisons need no fuzzyness
x > 0.0;
x <= 0.0;
x >= 0.0;
}