summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/floating_point_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/floating_point_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/floating_point_abs.rs')
-rw-r--r--src/tools/clippy/tests/ui/floating_point_abs.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/floating_point_abs.rs b/src/tools/clippy/tests/ui/floating_point_abs.rs
new file mode 100644
index 000000000..e4b606574
--- /dev/null
+++ b/src/tools/clippy/tests/ui/floating_point_abs.rs
@@ -0,0 +1,84 @@
+// run-rustfix
+#![feature(const_fn_floating_point_arithmetic)]
+#![warn(clippy::suboptimal_flops)]
+
+/// Allow suboptimal ops in constant context
+pub const fn in_const_context(num: f64) -> f64 {
+ if num >= 0.0 { num } else { -num }
+}
+
+struct A {
+ a: f64,
+ b: f64,
+}
+
+fn fake_abs1(num: f64) -> f64 {
+ if num >= 0.0 { num } else { -num }
+}
+
+fn fake_abs2(num: f64) -> f64 {
+ if 0.0 < num { num } else { -num }
+}
+
+fn fake_abs3(a: A) -> f64 {
+ if a.a > 0.0 { a.a } else { -a.a }
+}
+
+fn fake_abs4(num: f64) -> f64 {
+ if 0.0 >= num { -num } else { num }
+}
+
+fn fake_abs5(a: A) -> f64 {
+ if a.a < 0.0 { -a.a } else { a.a }
+}
+
+fn fake_nabs1(num: f64) -> f64 {
+ if num < 0.0 { num } else { -num }
+}
+
+fn fake_nabs2(num: f64) -> f64 {
+ if 0.0 >= num { num } else { -num }
+}
+
+fn fake_nabs3(a: A) -> A {
+ A {
+ a: if a.a >= 0.0 { -a.a } else { a.a },
+ b: a.b,
+ }
+}
+
+fn not_fake_abs1(num: f64) -> f64 {
+ if num > 0.0 { num } else { -num - 1f64 }
+}
+
+fn not_fake_abs2(num: f64) -> f64 {
+ if num > 0.0 { num + 1.0 } else { -(num + 1.0) }
+}
+
+fn not_fake_abs3(num1: f64, num2: f64) -> f64 {
+ if num1 > 0.0 { num2 } else { -num2 }
+}
+
+fn not_fake_abs4(a: A) -> f64 {
+ if a.a > 0.0 { a.b } else { -a.b }
+}
+
+fn not_fake_abs5(a: A) -> f64 {
+ if a.a > 0.0 { a.a } else { -a.b }
+}
+
+fn main() {
+ fake_abs1(5.0);
+ fake_abs2(5.0);
+ fake_abs3(A { a: 5.0, b: 5.0 });
+ fake_abs4(5.0);
+ fake_abs5(A { a: 5.0, b: 5.0 });
+ fake_nabs1(5.0);
+ fake_nabs2(5.0);
+ fake_nabs3(A { a: 5.0, b: 5.0 });
+ not_fake_abs1(5.0);
+ not_fake_abs2(5.0);
+ not_fake_abs3(5.0, 5.0);
+ not_fake_abs4(A { a: 5.0, b: 5.0 });
+ not_fake_abs5(A { a: 5.0, b: 5.0 });
+}