about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorThiago Arrais <thiago.arrais@gmail.com>2020-05-25 13:54:39 -0300
committerThiago Arrais <thiago.arrais@gmail.com>2020-07-06 13:23:17 -0300
commitf62798454c8a7f9f2c3e87e0a913b3bd79b6d2ed (patch)
tree1a3ef12b5dc8864c90279423ded8552070cac332 /tests
parenta6f1af75d71fcf8e029b78142370e7563798c503 (diff)
downloadrust-f62798454c8a7f9f2c3e87e0a913b3bd79b6d2ed.tar.gz
rust-f62798454c8a7f9f2c3e87e0a913b3bd79b6d2ed.zip
Lint (x * x + y * y).sqrt() => x.hypot(y)
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/floating_point_hypot.fixed13
-rw-r--r--tests/ui/floating_point_hypot.rs13
-rw-r--r--tests/ui/floating_point_hypot.stderr30
-rw-r--r--tests/ui/floating_point_mul_add.fixed5
-rw-r--r--tests/ui/floating_point_mul_add.rs5
-rw-r--r--tests/ui/floating_point_mul_add.stderr8
-rw-r--r--tests/ui/floating_point_powi.fixed7
-rw-r--r--tests/ui/floating_point_powi.rs7
-rw-r--r--tests/ui/floating_point_powi.stderr20
9 files changed, 104 insertions, 4 deletions
diff --git a/tests/ui/floating_point_hypot.fixed b/tests/ui/floating_point_hypot.fixed
new file mode 100644
index 00000000000..f90695bc3fe
--- /dev/null
+++ b/tests/ui/floating_point_hypot.fixed
@@ -0,0 +1,13 @@
+// run-rustfix
+#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
+
+fn main() {
+    let x = 3f32;
+    let y = 4f32;
+    let _ = x.hypot(y);
+    let _ = (x + 1f32).hypot(y);
+    let _ = x.hypot(y);
+    // Cases where the lint shouldn't be applied
+    let _ = x.mul_add(x, y * y).sqrt();
+    let _ = x.mul_add(4f32, y * y).sqrt();
+}
diff --git a/tests/ui/floating_point_hypot.rs b/tests/ui/floating_point_hypot.rs
new file mode 100644
index 00000000000..e7b048e262f
--- /dev/null
+++ b/tests/ui/floating_point_hypot.rs
@@ -0,0 +1,13 @@
+// run-rustfix
+#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
+
+fn main() {
+    let x = 3f32;
+    let y = 4f32;
+    let _ = (x * x + y * y).sqrt();
+    let _ = ((x + 1f32) * (x + 1f32) + y * y).sqrt();
+    let _ = (x.powi(2) + y.powi(2)).sqrt();
+    // Cases where the lint shouldn't be applied
+    let _ = x.mul_add(x, y * y).sqrt();
+    let _ = (x * 4f32 + y * y).sqrt();
+}
diff --git a/tests/ui/floating_point_hypot.stderr b/tests/ui/floating_point_hypot.stderr
new file mode 100644
index 00000000000..fe1dfc7a451
--- /dev/null
+++ b/tests/ui/floating_point_hypot.stderr
@@ -0,0 +1,30 @@
+error: hypotenuse can be computed more accurately
+  --> $DIR/floating_point_hypot.rs:7:13
+   |
+LL |     let _ = (x * x + y * y).sqrt();
+   |             ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.hypot(y)`
+   |
+   = note: `-D clippy::imprecise-flops` implied by `-D warnings`
+
+error: hypotenuse can be computed more accurately
+  --> $DIR/floating_point_hypot.rs:8:13
+   |
+LL |     let _ = ((x + 1f32) * (x + 1f32) + y * y).sqrt();
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 1f32).hypot(y)`
+
+error: hypotenuse can be computed more accurately
+  --> $DIR/floating_point_hypot.rs:9:13
+   |
+LL |     let _ = (x.powi(2) + y.powi(2)).sqrt();
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.hypot(y)`
+
+error: multiply and add expressions can be calculated more efficiently and accurately
+  --> $DIR/floating_point_hypot.rs:12:13
+   |
+LL |     let _ = (x * 4f32 + y * y).sqrt();
+   |             ^^^^^^^^^^^^^^^^^^ help: consider using: `x.mul_add(4f32, y * y)`
+   |
+   = note: `-D clippy::suboptimal-flops` implied by `-D warnings`
+
+error: aborting due to 4 previous errors
+
diff --git a/tests/ui/floating_point_mul_add.fixed b/tests/ui/floating_point_mul_add.fixed
index e343c37740d..911700bab00 100644
--- a/tests/ui/floating_point_mul_add.fixed
+++ b/tests/ui/floating_point_mul_add.fixed
@@ -18,4 +18,9 @@ fn main() {
 
     let _ = a.mul_add(b, c).mul_add(a.mul_add(b, c), a.mul_add(b, c)) + c;
     let _ = 1234.567_f64.mul_add(45.67834_f64, 0.0004_f64);
+
+    let _ = a.mul_add(a, b).sqrt();
+
+    // Cases where the lint shouldn't be applied
+    let _ = (a * a + b * b).sqrt();
 }
diff --git a/tests/ui/floating_point_mul_add.rs b/tests/ui/floating_point_mul_add.rs
index 810f929c856..d202385fc8a 100644
--- a/tests/ui/floating_point_mul_add.rs
+++ b/tests/ui/floating_point_mul_add.rs
@@ -18,4 +18,9 @@ fn main() {
 
     let _ = a.mul_add(b, c) * a.mul_add(b, c) + a.mul_add(b, c) + c;
     let _ = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
+
+    let _ = (a * a + b).sqrt();
+
+    // Cases where the lint shouldn't be applied
+    let _ = (a * a + b * b).sqrt();
 }
diff --git a/tests/ui/floating_point_mul_add.stderr b/tests/ui/floating_point_mul_add.stderr
index 2dfbf562d15..ac8d0c0cae0 100644
--- a/tests/ui/floating_point_mul_add.stderr
+++ b/tests/ui/floating_point_mul_add.stderr
@@ -54,5 +54,11 @@ error: multiply and add expressions can be calculated more efficiently and accur
 LL |     let _ = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1234.567_f64.mul_add(45.67834_f64, 0.0004_f64)`
 
-error: aborting due to 9 previous errors
+error: multiply and add expressions can be calculated more efficiently and accurately
+  --> $DIR/floating_point_mul_add.rs:22:13
+   |
+LL |     let _ = (a * a + b).sqrt();
+   |             ^^^^^^^^^^^ help: consider using: `a.mul_add(a, b)`
+
+error: aborting due to 10 previous errors
 
diff --git a/tests/ui/floating_point_powi.fixed b/tests/ui/floating_point_powi.fixed
index 0ce6f72535d..98766e68aaf 100644
--- a/tests/ui/floating_point_powi.fixed
+++ b/tests/ui/floating_point_powi.fixed
@@ -1,12 +1,17 @@
 // run-rustfix
-#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
+#![warn(clippy::imprecise_flops)]
 
 fn main() {
     let one = 1;
     let x = 3f32;
     let _ = x * x;
     let _ = x * x;
+
+    let y = 4f32;
+    let _ = (x * x + y).sqrt();
+    let _ = (x + y * y).sqrt();
     // Cases where the lint shouldn't be applied
     let _ = x.powi(3);
     let _ = x.powi(one + 1);
+    let _ = x.hypot(y);
 }
diff --git a/tests/ui/floating_point_powi.rs b/tests/ui/floating_point_powi.rs
index c87e836bedd..3c4b636a3d8 100644
--- a/tests/ui/floating_point_powi.rs
+++ b/tests/ui/floating_point_powi.rs
@@ -1,12 +1,17 @@
 // run-rustfix
-#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
+#![warn(clippy::imprecise_flops)]
 
 fn main() {
     let one = 1;
     let x = 3f32;
     let _ = x.powi(2);
     let _ = x.powi(1 + 1);
+
+    let y = 4f32;
+    let _ = (x.powi(2) + y).sqrt();
+    let _ = (x + y.powi(2)).sqrt();
     // Cases where the lint shouldn't be applied
     let _ = x.powi(3);
     let _ = x.powi(one + 1);
+    let _ = (x.powi(2) + y.powi(2)).sqrt();
 }
diff --git a/tests/ui/floating_point_powi.stderr b/tests/ui/floating_point_powi.stderr
index ae7bbaa4473..f370e24bf05 100644
--- a/tests/ui/floating_point_powi.stderr
+++ b/tests/ui/floating_point_powi.stderr
@@ -12,5 +12,23 @@ error: square can be computed more accurately
 LL |     let _ = x.powi(1 + 1);
    |             ^^^^^^^^^^^^^ help: consider using: `x * x`
 
-error: aborting due to 2 previous errors
+error: square can be computed more accurately
+  --> $DIR/floating_point_powi.rs:11:14
+   |
+LL |     let _ = (x.powi(2) + y).sqrt();
+   |              ^^^^^^^^^ help: consider using: `x * x`
+
+error: square can be computed more accurately
+  --> $DIR/floating_point_powi.rs:12:18
+   |
+LL |     let _ = (x + y.powi(2)).sqrt();
+   |                  ^^^^^^^^^ help: consider using: `y * y`
+
+error: hypotenuse can be computed more accurately
+  --> $DIR/floating_point_powi.rs:16:13
+   |
+LL |     let _ = (x.powi(2) + y.powi(2)).sqrt();
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.hypot(y)`
+
+error: aborting due to 5 previous errors