@@ -311,14 +311,25 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
311
311
312
312
if let ExprKind :: Binary (
313
313
Spanned {
314
- node : BinOpKind :: Add , ..
314
+ node : op @ ( BinOpKind :: Add | BinOpKind :: Sub ) ,
315
+ ..
315
316
} ,
316
317
lhs,
317
318
rhs,
318
319
) = parent. kind
319
320
{
320
321
let other_addend = if lhs. hir_id == expr. hir_id { rhs } else { lhs } ;
321
322
323
+ // Negate expr if original code has subtraction and expr is on the right side
324
+ let maybe_neg_sugg = |expr, hir_id| {
325
+ let sugg = Sugg :: hir ( cx, expr, ".." ) ;
326
+ if matches ! ( op, BinOpKind :: Sub ) && hir_id == rhs. hir_id {
327
+ format ! ( "-{sugg}" )
328
+ } else {
329
+ sugg. to_string ( )
330
+ }
331
+ } ;
332
+
322
333
span_lint_and_sugg (
323
334
cx,
324
335
SUBOPTIMAL_FLOPS ,
@@ -328,8 +339,8 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
328
339
format ! (
329
340
"{}.mul_add({}, {})" ,
330
341
Sugg :: hir( cx, receiver, ".." ) . maybe_par( ) ,
331
- Sugg :: hir ( cx , receiver, ".." ) ,
332
- Sugg :: hir ( cx , other_addend, ".." ) ,
342
+ maybe_neg_sugg ( receiver, expr . hir_id ) ,
343
+ maybe_neg_sugg ( other_addend, other_addend . hir_id ) ,
333
344
) ,
334
345
Applicability :: MachineApplicable ,
335
346
) ;
@@ -443,7 +454,8 @@ fn is_float_mul_expr<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(&'
443
454
fn check_mul_add ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
444
455
if let ExprKind :: Binary (
445
456
Spanned {
446
- node : BinOpKind :: Add , ..
457
+ node : op @ ( BinOpKind :: Add | BinOpKind :: Sub ) ,
458
+ ..
447
459
} ,
448
460
lhs,
449
461
rhs,
@@ -457,10 +469,27 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
457
469
}
458
470
}
459
471
472
+ let maybe_neg_sugg = |expr| {
473
+ let sugg = Sugg :: hir ( cx, expr, ".." ) ;
474
+ if let BinOpKind :: Sub = op {
475
+ format ! ( "-{sugg}" )
476
+ } else {
477
+ sugg. to_string ( )
478
+ }
479
+ } ;
480
+
460
481
let ( recv, arg1, arg2) = if let Some ( ( inner_lhs, inner_rhs) ) = is_float_mul_expr ( cx, lhs) {
461
- ( inner_lhs, inner_rhs, rhs)
482
+ (
483
+ inner_lhs,
484
+ Sugg :: hir ( cx, inner_rhs, ".." ) . to_string ( ) ,
485
+ maybe_neg_sugg ( rhs) ,
486
+ )
462
487
} else if let Some ( ( inner_lhs, inner_rhs) ) = is_float_mul_expr ( cx, rhs) {
463
- ( inner_lhs, inner_rhs, lhs)
488
+ (
489
+ inner_lhs,
490
+ maybe_neg_sugg ( inner_rhs) ,
491
+ Sugg :: hir ( cx, lhs, ".." ) . to_string ( ) ,
492
+ )
464
493
} else {
465
494
return ;
466
495
} ;
@@ -471,12 +500,7 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
471
500
expr. span ,
472
501
"multiply and add expressions can be calculated more efficiently and accurately" ,
473
502
"consider using" ,
474
- format ! (
475
- "{}.mul_add({}, {})" ,
476
- prepare_receiver_sugg( cx, recv) ,
477
- Sugg :: hir( cx, arg1, ".." ) ,
478
- Sugg :: hir( cx, arg2, ".." ) ,
479
- ) ,
503
+ format ! ( "{}.mul_add({arg1}, {arg2})" , prepare_receiver_sugg( cx, recv) ) ,
480
504
Applicability :: MachineApplicable ,
481
505
) ;
482
506
}
0 commit comments