Fix missing parenthesis in suboptimal floating point help

This commit is contained in:
Andre Bogus 2023-10-27 16:28:10 +02:00
parent 2f0f4ddcf7
commit 1ed1001440
5 changed files with 22 additions and 19 deletions

View File

@ -323,9 +323,9 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
let maybe_neg_sugg = |expr, hir_id| { let maybe_neg_sugg = |expr, hir_id| {
let sugg = Sugg::hir(cx, expr, ".."); let sugg = Sugg::hir(cx, expr, "..");
if matches!(op, BinOpKind::Sub) && hir_id == rhs.hir_id { if matches!(op, BinOpKind::Sub) && hir_id == rhs.hir_id {
format!("-{}", sugg.maybe_par()) -sugg
} else { } else {
sugg.to_string() sugg
} }
}; };
@ -470,25 +470,13 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
let maybe_neg_sugg = |expr| { let maybe_neg_sugg = |expr| {
let sugg = Sugg::hir(cx, expr, ".."); let sugg = Sugg::hir(cx, expr, "..");
if let BinOpKind::Sub = op { if let BinOpKind::Sub = op { -sugg } else { sugg }
format!("-{sugg}")
} else {
sugg.to_string()
}
}; };
let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) { let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
( (inner_lhs, Sugg::hir(cx, inner_rhs, ".."), maybe_neg_sugg(rhs))
inner_lhs,
Sugg::hir(cx, inner_rhs, "..").to_string(),
maybe_neg_sugg(rhs),
)
} else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, 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, ".."))
inner_lhs,
maybe_neg_sugg(inner_rhs),
Sugg::hir(cx, lhs, "..").to_string(),
)
} else { } else {
return; return;
}; };

View File

@ -465,7 +465,10 @@ fn sub(self, rhs: &Sugg<'_>) -> Sugg<'static> {
impl Neg for Sugg<'_> { impl Neg for Sugg<'_> {
type Output = Sugg<'static>; type Output = Sugg<'static>;
fn neg(self) -> Sugg<'static> { fn neg(self) -> Sugg<'static> {
make_unop("-", self) match &self {
Self::BinOp(AssocOp::As, ..) => Sugg::MaybeParen(format!("-({self})").into()),
_ => make_unop("-", self),
}
} }
} }

View File

@ -33,6 +33,9 @@ fn main() {
let _ = a.mul_add(a, b).sqrt(); 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 // Cases where the lint shouldn't be applied
let _ = (a * a + b * b).sqrt(); let _ = (a * a + b * b).sqrt();
} }

View File

@ -33,6 +33,9 @@ fn main() {
let _ = (a * a + b).sqrt(); let _ = (a * a + b).sqrt();
let u = 1usize;
let _ = a - (b * u as f64);
// Cases where the lint shouldn't be applied // Cases where the lint shouldn't be applied
let _ = (a * a + b * b).sqrt(); let _ = (a * a + b * b).sqrt();
} }

View File

@ -73,5 +73,11 @@ error: multiply and add expressions can be calculated more efficiently and accur
LL | let _ = (a * a + b).sqrt(); LL | let _ = (a * a + b).sqrt();
| ^^^^^^^^^^^ help: consider using: `a.mul_add(a, b)` | ^^^^^^^^^^^ 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