@@ -311,14 +311,25 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
311311
312312 if let ExprKind :: Binary (
313313 Spanned {
314- node : BinOpKind :: Add , ..
314+ node : op @ ( BinOpKind :: Add | BinOpKind :: Sub ) ,
315+ ..
315316 } ,
316317 lhs,
317318 rhs,
318319 ) = parent. kind
319320 {
320321 let other_addend = if lhs. hir_id == expr. hir_id { rhs } else { lhs } ;
321322
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+
322333 span_lint_and_sugg (
323334 cx,
324335 SUBOPTIMAL_FLOPS ,
@@ -328,8 +339,8 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
328339 format ! (
329340 "{}.mul_add({}, {})" ,
330341 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 ) ,
333344 ) ,
334345 Applicability :: MachineApplicable ,
335346 ) ;
@@ -443,7 +454,8 @@ fn is_float_mul_expr<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(&'
443454fn check_mul_add ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
444455 if let ExprKind :: Binary (
445456 Spanned {
446- node : BinOpKind :: Add , ..
457+ node : op @ ( BinOpKind :: Add | BinOpKind :: Sub ) ,
458+ ..
447459 } ,
448460 lhs,
449461 rhs,
@@ -457,10 +469,27 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
457469 }
458470 }
459471
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+
460481 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+ )
462487 } 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+ )
464493 } else {
465494 return ;
466495 } ;
@@ -471,12 +500,7 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
471500 expr. span ,
472501 "multiply and add expressions can be calculated more efficiently and accurately" ,
473502 "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) ) ,
480504 Applicability :: MachineApplicable ,
481505 ) ;
482506 }
0 commit comments