summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/float_equality_without_abs.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/tools/clippy/tests/ui/float_equality_without_abs.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/ui/float_equality_without_abs.rs')
-rw-r--r--src/tools/clippy/tests/ui/float_equality_without_abs.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/float_equality_without_abs.rs b/src/tools/clippy/tests/ui/float_equality_without_abs.rs
new file mode 100644
index 000000000..d40fa00c3
--- /dev/null
+++ b/src/tools/clippy/tests/ui/float_equality_without_abs.rs
@@ -0,0 +1,31 @@
+#![warn(clippy::float_equality_without_abs)]
+
+pub fn is_roughly_equal(a: f32, b: f32) -> bool {
+ (a - b) < f32::EPSILON
+}
+
+pub fn main() {
+ // all errors
+ is_roughly_equal(1.0, 2.0);
+ let a = 0.05;
+ let b = 0.0500001;
+
+ let _ = (a - b) < f32::EPSILON;
+ let _ = a - b < f32::EPSILON;
+ let _ = a - b.abs() < f32::EPSILON;
+ let _ = (a as f64 - b as f64) < f64::EPSILON;
+ let _ = 1.0 - 2.0 < f32::EPSILON;
+
+ let _ = f32::EPSILON > (a - b);
+ let _ = f32::EPSILON > a - b;
+ let _ = f32::EPSILON > a - b.abs();
+ let _ = f64::EPSILON > (a as f64 - b as f64);
+ let _ = f32::EPSILON > 1.0 - 2.0;
+
+ // those are correct
+ let _ = (a - b).abs() < f32::EPSILON;
+ let _ = (a as f64 - b as f64).abs() < f64::EPSILON;
+
+ let _ = f32::EPSILON > (a - b).abs();
+ let _ = f64::EPSILON > (a as f64 - b as f64).abs();
+}