Skip to content
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

Remove powi, "square can be computed more efficiently" #7201

Merged
merged 1 commit into from
May 18, 2021
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
12 changes: 1 addition & 11 deletions clippy_lints/src/floating_point_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
cx,
SUBOPTIMAL_FLOPS,
parent.span,
"square can be computed more efficiently",
"multiply and add expressions can be calculated more efficiently and accurately",
"consider using",
format!(
"{}.mul_add({}, {})",
Expand All @@ -337,16 +337,6 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
return;
}
}

span_lint_and_sugg(
cx,
SUBOPTIMAL_FLOPS,
expr.span,
"square can be computed more efficiently",
"consider using",
format!("{} * {}", Sugg::hir(cx, &args[0], ".."), Sugg::hir(cx, &args[0], "..")),
Applicability::MachineApplicable,
);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/floating_point_powi.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
fn main() {
let one = 1;
let x = 3f32;
let _ = x * x;
let _ = x * x;

let y = 4f32;
let _ = x.mul_add(x, y);
let _ = y.mul_add(y, x);
let _ = x.mul_add(x, y).sqrt();
let _ = y.mul_add(y, x).sqrt();
// Cases where the lint shouldn't be applied
let _ = x.powi(2);
let _ = x.powi(1 + 1);
let _ = x.powi(3);
let _ = x.powi(4) + y;
let _ = x.powi(one + 1);
let _ = (x.powi(2) + y.powi(2)).sqrt();
}
5 changes: 3 additions & 2 deletions tests/ui/floating_point_powi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
fn main() {
let one = 1;
let x = 3f32;
let _ = x.powi(2);
let _ = x.powi(1 + 1);

let y = 4f32;
let _ = x.powi(2) + y;
let _ = x + y.powi(2);
let _ = (x.powi(2) + y).sqrt();
let _ = (x + y.powi(2)).sqrt();
// Cases where the lint shouldn't be applied
let _ = x.powi(2);
let _ = x.powi(1 + 1);
let _ = x.powi(3);
let _ = x.powi(4) + y;
let _ = x.powi(one + 1);
let _ = (x.powi(2) + y.powi(2)).sqrt();
}
34 changes: 11 additions & 23 deletions tests/ui/floating_point_powi.stderr
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
error: square can be computed more efficiently
--> $DIR/floating_point_powi.rs:7:13
|
LL | let _ = x.powi(2);
| ^^^^^^^^^ help: consider using: `x * x`
|
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`

error: square can be computed more efficiently
--> $DIR/floating_point_powi.rs:8:13
|
LL | let _ = x.powi(1 + 1);
| ^^^^^^^^^^^^^ help: consider using: `x * x`

error: square can be computed more efficiently
--> $DIR/floating_point_powi.rs:11:13
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:9:13
|
LL | let _ = x.powi(2) + y;
| ^^^^^^^^^^^^^ help: consider using: `x.mul_add(x, y)`
|
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`

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

error: square can be computed more efficiently
--> $DIR/floating_point_powi.rs:13:13
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).sqrt();
| ^^^^^^^^^^^^^^^ help: consider using: `x.mul_add(x, y)`

error: square can be computed more efficiently
--> $DIR/floating_point_powi.rs:14:13
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)).sqrt();
| ^^^^^^^^^^^^^^^ help: consider using: `y.mul_add(y, x)`

error: aborting due to 6 previous errors
error: aborting due to 4 previous errors