Skip to content

Commit 4df6032

Browse files
committed
Auto merge of rust-lang#9394 - lukaslueg:issue9391, r=Jarcho
Fix missing parens in `suboptimal_flops` suggestion Fixes rust-lang#9391. The problem is simple enough, I didn't check if the same problem occurs elsewhere, though. changelog: fix missing parenthesis in `suboptimal_flops` suggestion
2 parents e1ecdb6 + 9ffc5a5 commit 4df6032

19 files changed

+146
-68
lines changed

clippy_lints/src/floating_point_arithmetic.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn check_log_base(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
172172
expr.span,
173173
"logarithm for bases 2, 10 and e can be computed more accurately",
174174
"consider using",
175-
format!("{}.{}()", Sugg::hir(cx, &args[0], ".."), method),
175+
format!("{}.{}()", Sugg::hir(cx, &args[0], "..").maybe_par(), method),
176176
Applicability::MachineApplicable,
177177
);
178178
}
@@ -263,21 +263,21 @@ fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
263263
(
264264
SUBOPTIMAL_FLOPS,
265265
"square-root of a number can be computed more efficiently and accurately",
266-
format!("{}.sqrt()", Sugg::hir(cx, &args[0], "..")),
266+
format!("{}.sqrt()", Sugg::hir(cx, &args[0], "..").maybe_par()),
267267
)
268268
} else if F32(1.0 / 3.0) == value || F64(1.0 / 3.0) == value {
269269
(
270270
IMPRECISE_FLOPS,
271271
"cube-root of a number can be computed more accurately",
272-
format!("{}.cbrt()", Sugg::hir(cx, &args[0], "..")),
272+
format!("{}.cbrt()", Sugg::hir(cx, &args[0], "..").maybe_par()),
273273
)
274274
} else if let Some(exponent) = get_integer_from_float_constant(&value) {
275275
(
276276
SUBOPTIMAL_FLOPS,
277277
"exponentiation with integer powers can be computed more efficiently",
278278
format!(
279279
"{}.powi({})",
280-
Sugg::hir(cx, &args[0], ".."),
280+
Sugg::hir(cx, &args[0], "..").maybe_par(),
281281
numeric_literal::format(&exponent.to_string(), None, false)
282282
),
283283
)
@@ -327,7 +327,7 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
327327
"consider using",
328328
format!(
329329
"{}.mul_add({}, {})",
330-
Sugg::hir(cx, &args[0], ".."),
330+
Sugg::hir(cx, &args[0], "..").maybe_par(),
331331
Sugg::hir(cx, &args[0], ".."),
332332
Sugg::hir(cx, other_addend, ".."),
333333
),
@@ -418,7 +418,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
418418
"consider using",
419419
format!(
420420
"{}.exp_m1()",
421-
Sugg::hir(cx, self_arg, "..")
421+
Sugg::hir(cx, self_arg, "..").maybe_par()
422422
),
423423
Applicability::MachineApplicable,
424424
);
@@ -550,11 +550,11 @@ fn check_custom_abs(cx: &LateContext<'_>, expr: &Expr<'_>) {
550550
then {
551551
let positive_abs_sugg = (
552552
"manual implementation of `abs` method",
553-
format!("{}.abs()", Sugg::hir(cx, body, "..")),
553+
format!("{}.abs()", Sugg::hir(cx, body, "..").maybe_par()),
554554
);
555555
let negative_abs_sugg = (
556556
"manual implementation of negation of `abs` method",
557-
format!("-{}.abs()", Sugg::hir(cx, body, "..")),
557+
format!("-{}.abs()", Sugg::hir(cx, body, "..").maybe_par()),
558558
);
559559
let sugg = if is_testing_positive(cx, cond, body) {
560560
if if_expr_positive {
@@ -621,7 +621,7 @@ fn check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>) {
621621
expr.span,
622622
"log base can be expressed more clearly",
623623
"consider using",
624-
format!("{}.log({})", Sugg::hir(cx, largs_self, ".."), Sugg::hir(cx, rargs_self, ".."),),
624+
format!("{}.log({})", Sugg::hir(cx, largs_self, "..").maybe_par(), Sugg::hir(cx, rargs_self, ".."),),
625625
Applicability::MachineApplicable,
626626
);
627627
}
@@ -651,7 +651,7 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
651651
if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&
652652
(F32(180_f32) == lvalue || F64(180_f64) == lvalue)
653653
{
654-
let mut proposal = format!("{}.to_degrees()", Sugg::hir(cx, mul_lhs, ".."));
654+
let mut proposal = format!("{}.to_degrees()", Sugg::hir(cx, mul_lhs, "..").maybe_par());
655655
if_chain! {
656656
if let ExprKind::Lit(ref literal) = mul_lhs.kind;
657657
if let ast::LitKind::Float(ref value, float_type) = literal.node;
@@ -677,7 +677,7 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
677677
(F32(180_f32) == rvalue || F64(180_f64) == rvalue) &&
678678
(F32(f32_consts::PI) == lvalue || F64(f64_consts::PI) == lvalue)
679679
{
680-
let mut proposal = format!("{}.to_radians()", Sugg::hir(cx, mul_lhs, ".."));
680+
let mut proposal = format!("{}.to_radians()", Sugg::hir(cx, mul_lhs, "..").maybe_par());
681681
if_chain! {
682682
if let ExprKind::Lit(ref literal) = mul_lhs.kind;
683683
if let ast::LitKind::Float(ref value, float_type) = literal.node;

tests/ui/floating_point_exp.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn main() {
55
let x = 2f32;
66
let _ = x.exp_m1();
77
let _ = x.exp_m1() + 2.0;
8+
let _ = (x as f32).exp_m1() + 2.0;
89
// Cases where the lint shouldn't be applied
910
let _ = x.exp() - 2.0;
1011
let _ = x.exp() - 1.0 * 2.0;

tests/ui/floating_point_exp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn main() {
55
let x = 2f32;
66
let _ = x.exp() - 1.0;
77
let _ = x.exp() - 1.0 + 2.0;
8+
let _ = (x as f32).exp() - 1.0 + 2.0;
89
// Cases where the lint shouldn't be applied
910
let _ = x.exp() - 2.0;
1011
let _ = x.exp() - 1.0 * 2.0;

tests/ui/floating_point_exp.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ LL | let _ = x.exp() - 1.0 + 2.0;
1313
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
1414

1515
error: (e.pow(x) - 1) can be computed more accurately
16-
--> $DIR/floating_point_exp.rs:13:13
16+
--> $DIR/floating_point_exp.rs:8:13
17+
|
18+
LL | let _ = (x as f32).exp() - 1.0 + 2.0;
19+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).exp_m1()`
20+
21+
error: (e.pow(x) - 1) can be computed more accurately
22+
--> $DIR/floating_point_exp.rs:14:13
1723
|
1824
LL | let _ = x.exp() - 1.0;
1925
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
2026

2127
error: (e.pow(x) - 1) can be computed more accurately
22-
--> $DIR/floating_point_exp.rs:14:13
28+
--> $DIR/floating_point_exp.rs:15:13
2329
|
2430
LL | let _ = x.exp() - 1.0 + 2.0;
2531
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
2632

27-
error: aborting due to 4 previous errors
33+
error: aborting due to 5 previous errors
2834

tests/ui/floating_point_log.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fn check_log_base() {
1212
let _ = x.ln();
1313
let _ = x.log2();
1414
let _ = x.ln();
15+
let _ = (x as f32).log2();
1516

1617
let x = 1f64;
1718
let _ = x.log2();

tests/ui/floating_point_log.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fn check_log_base() {
1212
let _ = x.log(std::f32::consts::E);
1313
let _ = x.log(TWO);
1414
let _ = x.log(E);
15+
let _ = (x as f32).log(2f32);
1516

1617
let x = 1f64;
1718
let _ = x.log(2f64);

tests/ui/floating_point_log.stderr

+30-24
Original file line numberDiff line numberDiff line change
@@ -31,144 +31,150 @@ LL | let _ = x.log(E);
3131
| ^^^^^^^^ help: consider using: `x.ln()`
3232

3333
error: logarithm for bases 2, 10 and e can be computed more accurately
34-
--> $DIR/floating_point_log.rs:17:13
34+
--> $DIR/floating_point_log.rs:15:13
35+
|
36+
LL | let _ = (x as f32).log(2f32);
37+
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).log2()`
38+
39+
error: logarithm for bases 2, 10 and e can be computed more accurately
40+
--> $DIR/floating_point_log.rs:18:13
3541
|
3642
LL | let _ = x.log(2f64);
3743
| ^^^^^^^^^^^ help: consider using: `x.log2()`
3844

3945
error: logarithm for bases 2, 10 and e can be computed more accurately
40-
--> $DIR/floating_point_log.rs:18:13
46+
--> $DIR/floating_point_log.rs:19:13
4147
|
4248
LL | let _ = x.log(10f64);
4349
| ^^^^^^^^^^^^ help: consider using: `x.log10()`
4450

4551
error: logarithm for bases 2, 10 and e can be computed more accurately
46-
--> $DIR/floating_point_log.rs:19:13
52+
--> $DIR/floating_point_log.rs:20:13
4753
|
4854
LL | let _ = x.log(std::f64::consts::E);
4955
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.ln()`
5056

5157
error: ln(1 + x) can be computed more accurately
52-
--> $DIR/floating_point_log.rs:24:13
58+
--> $DIR/floating_point_log.rs:25:13
5359
|
5460
LL | let _ = (1f32 + 2.).ln();
5561
| ^^^^^^^^^^^^^^^^ help: consider using: `2.0f32.ln_1p()`
5662
|
5763
= note: `-D clippy::imprecise-flops` implied by `-D warnings`
5864

5965
error: ln(1 + x) can be computed more accurately
60-
--> $DIR/floating_point_log.rs:25:13
66+
--> $DIR/floating_point_log.rs:26:13
6167
|
6268
LL | let _ = (1f32 + 2.0).ln();
6369
| ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f32.ln_1p()`
6470

6571
error: ln(1 + x) can be computed more accurately
66-
--> $DIR/floating_point_log.rs:26:13
72+
--> $DIR/floating_point_log.rs:27:13
6773
|
6874
LL | let _ = (1.0 + x).ln();
6975
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
7076

7177
error: ln(1 + x) can be computed more accurately
72-
--> $DIR/floating_point_log.rs:27:13
78+
--> $DIR/floating_point_log.rs:28:13
7379
|
7480
LL | let _ = (1.0 + x / 2.0).ln();
7581
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
7682

7783
error: ln(1 + x) can be computed more accurately
78-
--> $DIR/floating_point_log.rs:28:13
84+
--> $DIR/floating_point_log.rs:29:13
7985
|
8086
LL | let _ = (1.0 + x.powi(3)).ln();
8187
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
8288

8389
error: ln(1 + x) can be computed more accurately
84-
--> $DIR/floating_point_log.rs:29:13
90+
--> $DIR/floating_point_log.rs:30:13
8591
|
8692
LL | let _ = (1.0 + x.powi(3) / 2.0).ln();
8793
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x.powi(3) / 2.0).ln_1p()`
8894

8995
error: ln(1 + x) can be computed more accurately
90-
--> $DIR/floating_point_log.rs:30:13
96+
--> $DIR/floating_point_log.rs:31:13
9197
|
9298
LL | let _ = (1.0 + (std::f32::consts::E - 1.0)).ln();
9399
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(std::f32::consts::E - 1.0).ln_1p()`
94100

95101
error: ln(1 + x) can be computed more accurately
96-
--> $DIR/floating_point_log.rs:31:13
102+
--> $DIR/floating_point_log.rs:32:13
97103
|
98104
LL | let _ = (x + 1.0).ln();
99105
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
100106

101107
error: ln(1 + x) can be computed more accurately
102-
--> $DIR/floating_point_log.rs:32:13
108+
--> $DIR/floating_point_log.rs:33:13
103109
|
104110
LL | let _ = (x.powi(3) + 1.0).ln();
105111
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
106112

107113
error: ln(1 + x) can be computed more accurately
108-
--> $DIR/floating_point_log.rs:33:13
114+
--> $DIR/floating_point_log.rs:34:13
109115
|
110116
LL | let _ = (x + 2.0 + 1.0).ln();
111117
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()`
112118

113119
error: ln(1 + x) can be computed more accurately
114-
--> $DIR/floating_point_log.rs:34:13
120+
--> $DIR/floating_point_log.rs:35:13
115121
|
116122
LL | let _ = (x / 2.0 + 1.0).ln();
117123
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
118124

119125
error: ln(1 + x) can be computed more accurately
120-
--> $DIR/floating_point_log.rs:42:13
126+
--> $DIR/floating_point_log.rs:43:13
121127
|
122128
LL | let _ = (1f64 + 2.).ln();
123129
| ^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()`
124130

125131
error: ln(1 + x) can be computed more accurately
126-
--> $DIR/floating_point_log.rs:43:13
132+
--> $DIR/floating_point_log.rs:44:13
127133
|
128134
LL | let _ = (1f64 + 2.0).ln();
129135
| ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()`
130136

131137
error: ln(1 + x) can be computed more accurately
132-
--> $DIR/floating_point_log.rs:44:13
138+
--> $DIR/floating_point_log.rs:45:13
133139
|
134140
LL | let _ = (1.0 + x).ln();
135141
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
136142

137143
error: ln(1 + x) can be computed more accurately
138-
--> $DIR/floating_point_log.rs:45:13
144+
--> $DIR/floating_point_log.rs:46:13
139145
|
140146
LL | let _ = (1.0 + x / 2.0).ln();
141147
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
142148

143149
error: ln(1 + x) can be computed more accurately
144-
--> $DIR/floating_point_log.rs:46:13
150+
--> $DIR/floating_point_log.rs:47:13
145151
|
146152
LL | let _ = (1.0 + x.powi(3)).ln();
147153
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
148154

149155
error: ln(1 + x) can be computed more accurately
150-
--> $DIR/floating_point_log.rs:47:13
156+
--> $DIR/floating_point_log.rs:48:13
151157
|
152158
LL | let _ = (x + 1.0).ln();
153159
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
154160

155161
error: ln(1 + x) can be computed more accurately
156-
--> $DIR/floating_point_log.rs:48:13
162+
--> $DIR/floating_point_log.rs:49:13
157163
|
158164
LL | let _ = (x.powi(3) + 1.0).ln();
159165
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
160166

161167
error: ln(1 + x) can be computed more accurately
162-
--> $DIR/floating_point_log.rs:49:13
168+
--> $DIR/floating_point_log.rs:50:13
163169
|
164170
LL | let _ = (x + 2.0 + 1.0).ln();
165171
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()`
166172

167173
error: ln(1 + x) can be computed more accurately
168-
--> $DIR/floating_point_log.rs:50:13
174+
--> $DIR/floating_point_log.rs:51:13
169175
|
170176
LL | let _ = (x / 2.0 + 1.0).ln();
171177
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
172178

173-
error: aborting due to 28 previous errors
179+
error: aborting due to 29 previous errors
174180

tests/ui/floating_point_logbase.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn main() {
55
let x = 3f32;
66
let y = 5f32;
77
let _ = x.log(y);
8+
let _ = (x as f32).log(y);
89
let _ = x.log(y);
910
let _ = x.log(y);
1011
let _ = x.log(y);

tests/ui/floating_point_logbase.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn main() {
55
let x = 3f32;
66
let y = 5f32;
77
let _ = x.ln() / y.ln();
8+
let _ = (x as f32).ln() / y.ln();
89
let _ = x.log2() / y.log2();
910
let _ = x.log10() / y.log10();
1011
let _ = x.log(5f32) / y.log(5f32);

tests/ui/floating_point_logbase.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@ LL | let _ = x.ln() / y.ln();
99
error: log base can be expressed more clearly
1010
--> $DIR/floating_point_logbase.rs:8:13
1111
|
12+
LL | let _ = (x as f32).ln() / y.ln();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).log(y)`
14+
15+
error: log base can be expressed more clearly
16+
--> $DIR/floating_point_logbase.rs:9:13
17+
|
1218
LL | let _ = x.log2() / y.log2();
1319
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
1420

1521
error: log base can be expressed more clearly
16-
--> $DIR/floating_point_logbase.rs:9:13
22+
--> $DIR/floating_point_logbase.rs:10:13
1723
|
1824
LL | let _ = x.log10() / y.log10();
1925
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
2026

2127
error: log base can be expressed more clearly
22-
--> $DIR/floating_point_logbase.rs:10:13
28+
--> $DIR/floating_point_logbase.rs:11:13
2329
|
2430
LL | let _ = x.log(5f32) / y.log(5f32);
2531
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
2632

27-
error: aborting due to 4 previous errors
33+
error: aborting due to 5 previous errors
2834

tests/ui/floating_point_powf.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ fn main() {
1111
let _ = (-3.1f32).exp();
1212
let _ = x.sqrt();
1313
let _ = x.cbrt();
14+
let _ = (x as f32).cbrt();
1415
let _ = x.powi(3);
1516
let _ = x.powi(-2);
1617
let _ = x.powi(16_777_215);
1718
let _ = x.powi(-16_777_215);
19+
let _ = (x as f32).powi(-16_777_215);
20+
let _ = (x as f32).powi(3);
1821
// Cases where the lint shouldn't be applied
1922
let _ = x.powf(2.1);
2023
let _ = x.powf(-2.1);

tests/ui/floating_point_powf.rs

+3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ fn main() {
1111
let _ = std::f32::consts::E.powf(-3.1);
1212
let _ = x.powf(1.0 / 2.0);
1313
let _ = x.powf(1.0 / 3.0);
14+
let _ = (x as f32).powf(1.0 / 3.0);
1415
let _ = x.powf(3.0);
1516
let _ = x.powf(-2.0);
1617
let _ = x.powf(16_777_215.0);
1718
let _ = x.powf(-16_777_215.0);
19+
let _ = (x as f32).powf(-16_777_215.0);
20+
let _ = (x as f32).powf(3.0);
1821
// Cases where the lint shouldn't be applied
1922
let _ = x.powf(2.1);
2023
let _ = x.powf(-2.1);

0 commit comments

Comments
 (0)