Skip to content

Commit d98e714

Browse files
committed
Auto merge of rust-lang#10113 - EricWu2003:suboptimal_flops_incorrect_suggestion, r=Jarcho
fix incorrect suggestion in `suboptimal_flops` fixes rust-lang#10003 There was an error when trying to negate an expression like `x - 1.0`. We used to format it as `-x - 1.0` whereas a proper negation would be `-(x - 1.0)`. Therefore, we add parentheses around the expression when it is `ExprKind::Binary`. We also add parentheses around multiply and divide expressions, even though this is not strictly necessary. changelog: [`suboptimal_flops`]: fix incorrect suggestion caused by an incorrect negation of floating point expressions.
2 parents 4f1bae0 + 6bb6dd6 commit d98e714

4 files changed

+62
-2
lines changed

clippy_lints/src/floating_point_arithmetic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
324324
let maybe_neg_sugg = |expr, hir_id| {
325325
let sugg = Sugg::hir(cx, expr, "..");
326326
if matches!(op, BinOpKind::Sub) && hir_id == rhs.hir_id {
327-
format!("-{sugg}")
327+
format!("-{}", sugg.maybe_par())
328328
} else {
329329
sugg.to_string()
330330
}

tests/ui/floating_point_powi.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ fn main() {
1414
let _ = (y as f32).mul_add(y as f32, x);
1515
let _ = x.mul_add(x, y).sqrt();
1616
let _ = y.mul_add(y, x).sqrt();
17+
18+
let _ = (x - 1.0).mul_add(x - 1.0, -y);
19+
let _ = (x - 1.0).mul_add(x - 1.0, -y) + 3.0;
20+
let _ = (x - 1.0).mul_add(x - 1.0, -(y + 3.0));
21+
let _ = (y + 1.0).mul_add(-(y + 1.0), x);
22+
let _ = (3.0 * y).mul_add(-(3.0 * y), x);
23+
let _ = (y + 1.0 + x).mul_add(-(y + 1.0 + x), x);
24+
let _ = (y + 1.0 + 2.0).mul_add(-(y + 1.0 + 2.0), x);
25+
1726
// Cases where the lint shouldn't be applied
1827
let _ = x.powi(2);
1928
let _ = x.powi(1 + 1);

tests/ui/floating_point_powi.rs

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ fn main() {
1414
let _ = x + (y as f32).powi(2);
1515
let _ = (x.powi(2) + y).sqrt();
1616
let _ = (x + y.powi(2)).sqrt();
17+
18+
let _ = (x - 1.0).powi(2) - y;
19+
let _ = (x - 1.0).powi(2) - y + 3.0;
20+
let _ = (x - 1.0).powi(2) - (y + 3.0);
21+
let _ = x - (y + 1.0).powi(2);
22+
let _ = x - (3.0 * y).powi(2);
23+
let _ = x - (y + 1.0 + x).powi(2);
24+
let _ = x - (y + 1.0 + 2.0).powi(2);
25+
1726
// Cases where the lint shouldn't be applied
1827
let _ = x.powi(2);
1928
let _ = x.powi(1 + 1);

tests/ui/floating_point_powi.stderr

+43-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,47 @@ error: multiply and add expressions can be calculated more efficiently and accur
4242
LL | let _ = (x + y.powi(2)).sqrt();
4343
| ^^^^^^^^^^^^^^^ help: consider using: `y.mul_add(y, x)`
4444

45-
error: aborting due to 7 previous errors
45+
error: multiply and add expressions can be calculated more efficiently and accurately
46+
--> $DIR/floating_point_powi.rs:18:13
47+
|
48+
LL | let _ = (x - 1.0).powi(2) - y;
49+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x - 1.0).mul_add(x - 1.0, -y)`
50+
51+
error: multiply and add expressions can be calculated more efficiently and accurately
52+
--> $DIR/floating_point_powi.rs:19:13
53+
|
54+
LL | let _ = (x - 1.0).powi(2) - y + 3.0;
55+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x - 1.0).mul_add(x - 1.0, -y)`
56+
57+
error: multiply and add expressions can be calculated more efficiently and accurately
58+
--> $DIR/floating_point_powi.rs:20:13
59+
|
60+
LL | let _ = (x - 1.0).powi(2) - (y + 3.0);
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x - 1.0).mul_add(x - 1.0, -(y + 3.0))`
62+
63+
error: multiply and add expressions can be calculated more efficiently and accurately
64+
--> $DIR/floating_point_powi.rs:21:13
65+
|
66+
LL | let _ = x - (y + 1.0).powi(2);
67+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(y + 1.0).mul_add(-(y + 1.0), x)`
68+
69+
error: multiply and add expressions can be calculated more efficiently and accurately
70+
--> $DIR/floating_point_powi.rs:22:13
71+
|
72+
LL | let _ = x - (3.0 * y).powi(2);
73+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(3.0 * y).mul_add(-(3.0 * y), x)`
74+
75+
error: multiply and add expressions can be calculated more efficiently and accurately
76+
--> $DIR/floating_point_powi.rs:23:13
77+
|
78+
LL | let _ = x - (y + 1.0 + x).powi(2);
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(y + 1.0 + x).mul_add(-(y + 1.0 + x), x)`
80+
81+
error: multiply and add expressions can be calculated more efficiently and accurately
82+
--> $DIR/floating_point_powi.rs:24:13
83+
|
84+
LL | let _ = x - (y + 1.0 + 2.0).powi(2);
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(y + 1.0 + 2.0).mul_add(-(y + 1.0 + 2.0), x)`
86+
87+
error: aborting due to 14 previous errors
4688

0 commit comments

Comments
 (0)