Skip to content

Commit 5852ca8

Browse files
committed
Auto merge of #11724 - rust-lang:fix-11559, r=blyxyas
Fix missing parenthesis in suboptimal floating point help This fixes #11559 by adding a branch in the `Neg` implementation for `Sugg` that adds parentheses to keep precedence in order, then using that in the suggestion. I also removed some needless `.to_string()`s while I was at it. --- changelog: none
2 parents f8409ef + 1ed1001 commit 5852ca8

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
323323
let maybe_neg_sugg = |expr, hir_id| {
324324
let sugg = Sugg::hir(cx, expr, "..");
325325
if matches!(op, BinOpKind::Sub) && hir_id == rhs.hir_id {
326-
format!("-{}", sugg.maybe_par())
326+
-sugg
327327
} else {
328-
sugg.to_string()
328+
sugg
329329
}
330330
};
331331

@@ -470,25 +470,13 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
470470

471471
let maybe_neg_sugg = |expr| {
472472
let sugg = Sugg::hir(cx, expr, "..");
473-
if let BinOpKind::Sub = op {
474-
format!("-{sugg}")
475-
} else {
476-
sugg.to_string()
477-
}
473+
if let BinOpKind::Sub = op { -sugg } else { sugg }
478474
};
479475

480476
let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
481-
(
482-
inner_lhs,
483-
Sugg::hir(cx, inner_rhs, "..").to_string(),
484-
maybe_neg_sugg(rhs),
485-
)
477+
(inner_lhs, Sugg::hir(cx, inner_rhs, ".."), maybe_neg_sugg(rhs))
486478
} else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) {
487-
(
488-
inner_lhs,
489-
maybe_neg_sugg(inner_rhs),
490-
Sugg::hir(cx, lhs, "..").to_string(),
491-
)
479+
(inner_lhs, maybe_neg_sugg(inner_rhs), Sugg::hir(cx, lhs, ".."))
492480
} else {
493481
return;
494482
};

clippy_utils/src/sugg.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,10 @@ forward_binop_impls_to_ref!(impl Sub, sub for Sugg<'_>, type Output = Sugg<'stat
465465
impl Neg for Sugg<'_> {
466466
type Output = Sugg<'static>;
467467
fn neg(self) -> Sugg<'static> {
468-
make_unop("-", self)
468+
match &self {
469+
Self::BinOp(AssocOp::As, ..) => Sugg::MaybeParen(format!("-({self})").into()),
470+
_ => make_unop("-", self),
471+
}
469472
}
470473
}
471474

tests/ui/floating_point_mul_add.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ fn main() {
3333

3434
let _ = a.mul_add(a, b).sqrt();
3535

36+
let u = 1usize;
37+
let _ = b.mul_add(-(u as f64), a);
38+
3639
// Cases where the lint shouldn't be applied
3740
let _ = (a * a + b * b).sqrt();
3841
}

tests/ui/floating_point_mul_add.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ fn main() {
3333

3434
let _ = (a * a + b).sqrt();
3535

36+
let u = 1usize;
37+
let _ = a - (b * u as f64);
38+
3639
// Cases where the lint shouldn't be applied
3740
let _ = (a * a + b * b).sqrt();
3841
}

tests/ui/floating_point_mul_add.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,11 @@ error: multiply and add expressions can be calculated more efficiently and accur
7373
LL | let _ = (a * a + b).sqrt();
7474
| ^^^^^^^^^^^ help: consider using: `a.mul_add(a, b)`
7575

76-
error: aborting due to 12 previous errors
76+
error: multiply and add expressions can be calculated more efficiently and accurately
77+
--> $DIR/floating_point_mul_add.rs:37:13
78+
|
79+
LL | let _ = a - (b * u as f64);
80+
| ^^^^^^^^^^^^^^^^^^ help: consider using: `b.mul_add(-(u as f64), a)`
81+
82+
error: aborting due to 13 previous errors
7783

0 commit comments

Comments
 (0)