Skip to content

suboptimal_flops lint for multiply and subtract #9581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions clippy_lints/src/floating_point_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,25 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:

if let ExprKind::Binary(
Spanned {
node: BinOpKind::Add, ..
node: op @ (BinOpKind::Add | BinOpKind::Sub),
..
},
lhs,
rhs,
) = parent.kind
{
let other_addend = if lhs.hir_id == expr.hir_id { rhs } else { lhs };

// Negate expr if original code has subtraction and expr is on the right side
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}")
} else {
sugg.to_string()
}
};

span_lint_and_sugg(
cx,
SUBOPTIMAL_FLOPS,
Expand All @@ -328,8 +339,8 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
format!(
"{}.mul_add({}, {})",
Sugg::hir(cx, receiver, "..").maybe_par(),
Sugg::hir(cx, receiver, ".."),
Sugg::hir(cx, other_addend, ".."),
maybe_neg_sugg(receiver, expr.hir_id),
maybe_neg_sugg(other_addend, other_addend.hir_id),
),
Applicability::MachineApplicable,
);
Expand Down Expand Up @@ -443,7 +454,8 @@ fn is_float_mul_expr<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(&'
fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
if let ExprKind::Binary(
Spanned {
node: BinOpKind::Add, ..
node: op @ (BinOpKind::Add | BinOpKind::Sub),
..
},
lhs,
rhs,
Expand All @@ -457,10 +469,27 @@ 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()
}
};

let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
(inner_lhs, inner_rhs, 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) {
(inner_lhs, inner_rhs, lhs)
(
inner_lhs,
maybe_neg_sugg(inner_rhs),
Sugg::hir(cx, lhs, "..").to_string(),
)
} else {
return;
};
Expand All @@ -471,12 +500,7 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
expr.span,
"multiply and add expressions can be calculated more efficiently and accurately",
"consider using",
format!(
"{}.mul_add({}, {})",
prepare_receiver_sugg(cx, recv),
Sugg::hir(cx, arg1, ".."),
Sugg::hir(cx, arg2, ".."),
),
format!("{}.mul_add({arg1}, {arg2})", prepare_receiver_sugg(cx, recv)),
Applicability::MachineApplicable,
);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/floating_point_mul_add.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ fn main() {
let d: f64 = 0.0001;

let _ = a.mul_add(b, c);
let _ = a.mul_add(b, -c);
let _ = a.mul_add(b, c);
let _ = a.mul_add(-b, c);
let _ = 2.0f64.mul_add(4.0, a);
let _ = 2.0f64.mul_add(4., a);

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/floating_point_mul_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ fn main() {
let d: f64 = 0.0001;

let _ = a * b + c;
let _ = a * b - c;
let _ = c + a * b;
let _ = c - a * b;
let _ = a + 2.0 * 4.0;
let _ = a + 2. * 4.;

Expand Down
30 changes: 21 additions & 9 deletions tests/ui/floating_point_mul_add.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,68 @@ LL | let _ = a * b + c;
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:22:13
|
LL | let _ = a * b - c;
| ^^^^^^^^^ help: consider using: `a.mul_add(b, -c)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:23:13
|
LL | let _ = c + a * b;
| ^^^^^^^^^ help: consider using: `a.mul_add(b, c)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:23:13
--> $DIR/floating_point_mul_add.rs:24:13
|
LL | let _ = c - a * b;
| ^^^^^^^^^ help: consider using: `a.mul_add(-b, c)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:25:13
|
LL | let _ = a + 2.0 * 4.0;
| ^^^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(4.0, a)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:24:13
--> $DIR/floating_point_mul_add.rs:26:13
|
LL | let _ = a + 2. * 4.;
| ^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(4., a)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:26:13
--> $DIR/floating_point_mul_add.rs:28:13
|
LL | let _ = (a * b) + c;
| ^^^^^^^^^^^ help: consider using: `a.mul_add(b, c)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:27:13
--> $DIR/floating_point_mul_add.rs:29:13
|
LL | let _ = c + (a * b);
| ^^^^^^^^^^^ help: consider using: `a.mul_add(b, c)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:28:13
--> $DIR/floating_point_mul_add.rs:30:13
|
LL | let _ = a * b * c + d;
| ^^^^^^^^^^^^^ help: consider using: `(a * b).mul_add(c, d)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:30:13
--> $DIR/floating_point_mul_add.rs:32:13
|
LL | let _ = a.mul_add(b, c) * a.mul_add(b, c) + a.mul_add(b, c) + c;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `a.mul_add(b, c).mul_add(a.mul_add(b, c), a.mul_add(b, c))`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:31:13
--> $DIR/floating_point_mul_add.rs:33:13
|
LL | let _ = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1234.567_f64.mul_add(45.67834_f64, 0.0004_f64)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_mul_add.rs:33:13
--> $DIR/floating_point_mul_add.rs:35:13
|
LL | let _ = (a * a + b).sqrt();
| ^^^^^^^^^^^ help: consider using: `a.mul_add(a, b)`

error: aborting due to 10 previous errors
error: aborting due to 12 previous errors

2 changes: 2 additions & 0 deletions tests/ui/floating_point_powi.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ fn main() {

let y = 4f32;
let _ = x.mul_add(x, y);
let _ = x.mul_add(x, -y);
let _ = y.mul_add(y, x);
let _ = y.mul_add(-y, x);
let _ = (y as f32).mul_add(y as f32, x);
let _ = x.mul_add(x, y).sqrt();
let _ = y.mul_add(y, x).sqrt();
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/floating_point_powi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ fn main() {

let y = 4f32;
let _ = x.powi(2) + y;
let _ = x.powi(2) - y;
let _ = x + y.powi(2);
let _ = x - y.powi(2);
let _ = x + (y as f32).powi(2);
let _ = (x.powi(2) + y).sqrt();
let _ = (x + y.powi(2)).sqrt();
Expand Down
20 changes: 16 additions & 4 deletions tests/ui/floating_point_powi.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,38 @@ LL | let _ = x.powi(2) + y;
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:11:13
|
LL | let _ = x.powi(2) - y;
| ^^^^^^^^^^^^^ help: consider using: `x.mul_add(x, -y)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:12:13
|
LL | let _ = x + y.powi(2);
| ^^^^^^^^^^^^^ help: consider using: `y.mul_add(y, x)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:12:13
--> $DIR/floating_point_powi.rs:13:13
|
LL | let _ = x - y.powi(2);
| ^^^^^^^^^^^^^ help: consider using: `y.mul_add(-y, x)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:14:13
|
LL | let _ = x + (y as f32).powi(2);
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(y as f32).mul_add(y as f32, x)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:13:13
--> $DIR/floating_point_powi.rs:15:13
|
LL | let _ = (x.powi(2) + y).sqrt();
| ^^^^^^^^^^^^^^^ help: consider using: `x.mul_add(x, y)`

error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:14:13
--> $DIR/floating_point_powi.rs:16:13
|
LL | let _ = (x + y.powi(2)).sqrt();
| ^^^^^^^^^^^^^^^ help: consider using: `y.mul_add(y, x)`

error: aborting due to 5 previous errors
error: aborting due to 7 previous errors