about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndre Bogus <bogusandre@gmail.com>2023-10-27 16:28:10 +0200
committerAndre Bogus <bogusandre@gmail.com>2023-10-27 16:28:10 +0200
commit1ed100144028cd08dcdc3c17475ab5cfce8629d1 (patch)
tree3120519e22730f7f03cc9eca5aba1097f1751a48
parent2f0f4ddcf73a82115168d187a5c2121b413e34a2 (diff)
downloadrust-1ed100144028cd08dcdc3c17475ab5cfce8629d1.tar.gz
rust-1ed100144028cd08dcdc3c17475ab5cfce8629d1.zip
Fix missing parenthesis in suboptimal floating point help
-rw-r--r--clippy_lints/src/floating_point_arithmetic.rs22
-rw-r--r--clippy_utils/src/sugg.rs5
-rw-r--r--tests/ui/floating_point_mul_add.fixed3
-rw-r--r--tests/ui/floating_point_mul_add.rs3
-rw-r--r--tests/ui/floating_point_mul_add.stderr8
5 files changed, 22 insertions, 19 deletions
diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs
index 3d51dfc3497..09a9d9924de 100644
--- a/clippy_lints/src/floating_point_arithmetic.rs
+++ b/clippy_lints/src/floating_point_arithmetic.rs
@@ -323,9 +323,9 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
                     let maybe_neg_sugg = |expr, hir_id| {
                         let sugg = Sugg::hir(cx, expr, "..");
                         if matches!(op, BinOpKind::Sub) && hir_id == rhs.hir_id {
-                            format!("-{}", sugg.maybe_par())
+                            -sugg
                         } else {
-                            sugg.to_string()
+                            sugg
                         }
                     };
 
@@ -470,25 +470,13 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
 
         let maybe_neg_sugg = |expr| {
             let sugg = Sugg::hir(cx, expr, "..");
-            if let BinOpKind::Sub = op {
-                format!("-{sugg}")
-            } else {
-                sugg.to_string()
-            }
+            if let BinOpKind::Sub = op { -sugg } else { sugg }
         };
 
         let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
-            (
-                inner_lhs,
-                Sugg::hir(cx, inner_rhs, "..").to_string(),
-                maybe_neg_sugg(rhs),
-            )
+            (inner_lhs, Sugg::hir(cx, inner_rhs, ".."), maybe_neg_sugg(rhs))
         } else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) {
-            (
-                inner_lhs,
-                maybe_neg_sugg(inner_rhs),
-                Sugg::hir(cx, lhs, "..").to_string(),
-            )
+            (inner_lhs, maybe_neg_sugg(inner_rhs), Sugg::hir(cx, lhs, ".."))
         } else {
             return;
         };
diff --git a/clippy_utils/src/sugg.rs b/clippy_utils/src/sugg.rs
index ae8ee371ffa..f6b9fb3979b 100644
--- a/clippy_utils/src/sugg.rs
+++ b/clippy_utils/src/sugg.rs
@@ -465,7 +465,10 @@ forward_binop_impls_to_ref!(impl Sub, sub for Sugg<'_>, type Output = Sugg<'stat
 impl Neg for Sugg<'_> {
     type Output = Sugg<'static>;
     fn neg(self) -> Sugg<'static> {
-        make_unop("-", self)
+        match &self {
+            Self::BinOp(AssocOp::As, ..) => Sugg::MaybeParen(format!("-({self})").into()),
+            _ => make_unop("-", self),
+        }
     }
 }
 
diff --git a/tests/ui/floating_point_mul_add.fixed b/tests/ui/floating_point_mul_add.fixed
index c23f4d7c4d3..a4d6d49e57c 100644
--- a/tests/ui/floating_point_mul_add.fixed
+++ b/tests/ui/floating_point_mul_add.fixed
@@ -33,6 +33,9 @@ fn main() {
 
     let _ = a.mul_add(a, b).sqrt();
 
+    let u = 1usize;
+    let _ = b.mul_add(-(u as f64), a);
+
     // 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 431badc8db4..262a20f0f55 100644
--- a/tests/ui/floating_point_mul_add.rs
+++ b/tests/ui/floating_point_mul_add.rs
@@ -33,6 +33,9 @@ fn main() {
 
     let _ = (a * a + b).sqrt();
 
+    let u = 1usize;
+    let _ = a - (b * u as f64);
+
     // 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 81b7126db54..38dbefbe14c 100644
--- a/tests/ui/floating_point_mul_add.stderr
+++ b/tests/ui/floating_point_mul_add.stderr
@@ -73,5 +73,11 @@ error: multiply and add expressions can be calculated more efficiently and accur
 LL |     let _ = (a * a + b).sqrt();
    |             ^^^^^^^^^^^ help: consider using: `a.mul_add(a, b)`
 
-error: aborting due to 12 previous errors
+error: multiply and add expressions can be calculated more efficiently and accurately
+  --> $DIR/floating_point_mul_add.rs:37:13
+   |
+LL |     let _ = a - (b * u as f64);
+   |             ^^^^^^^^^^^^^^^^^^ help: consider using: `b.mul_add(-(u as f64), a)`
+
+error: aborting due to 13 previous errors