Skip to content

Commit e94a167

Browse files
Rename mul_add test file and add general improvements
1 parent 4065ca9 commit e94a167

7 files changed

+82
-72
lines changed

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use rustc_errors::Applicability;
99
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
12+
use rustc_span::source_map::Spanned;
13+
1214
use std::f32::consts as f32_consts;
1315
use std::f64::consts as f64_consts;
1416
use sugg::{format_numeric_literal, Sugg};
@@ -138,26 +140,29 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>])
138140
// TODO: Lint expressions of the form `(x + y).ln()` where y > 1 and
139141
// suggest usage of `(x + (y - 1)).ln_1p()` instead
140142
fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
141-
if_chain! {
142-
if let ExprKind::Binary(op, ref lhs, ref rhs) = &args[0].kind;
143-
if op.node == BinOpKind::Add;
144-
then {
145-
let recv = match (constant(cx, cx.tables, lhs), constant(cx, cx.tables, rhs)) {
146-
(Some((value, _)), _) if F32(1.0) == value || F64(1.0) == value => rhs,
147-
(_, Some((value, _))) if F32(1.0) == value || F64(1.0) == value => lhs,
148-
_ => return,
149-
};
143+
if let ExprKind::Binary(
144+
Spanned {
145+
node: BinOpKind::Add, ..
146+
},
147+
lhs,
148+
rhs,
149+
) = &args[0].kind
150+
{
151+
let recv = match (constant(cx, cx.tables, lhs), constant(cx, cx.tables, rhs)) {
152+
(Some((value, _)), _) if F32(1.0) == value || F64(1.0) == value => rhs,
153+
(_, Some((value, _))) if F32(1.0) == value || F64(1.0) == value => lhs,
154+
_ => return,
155+
};
150156

151-
span_lint_and_sugg(
152-
cx,
153-
SUBOPTIMAL_FLOPS,
154-
expr.span,
155-
"ln(1 + x) can be computed more accurately",
156-
"consider using",
157-
format!("{}.ln_1p()", prepare_receiver_sugg(cx, recv)),
158-
Applicability::MachineApplicable,
159-
);
160-
}
157+
span_lint_and_sugg(
158+
cx,
159+
SUBOPTIMAL_FLOPS,
160+
expr.span,
161+
"ln(1 + x) can be computed more accurately",
162+
"consider using",
163+
format!("{}.ln_1p()", prepare_receiver_sugg(cx, recv)),
164+
Applicability::MachineApplicable,
165+
);
161166
}
162167
}
163168

@@ -249,8 +254,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
249254
// and suggest usage of `x.exp_m1() - (y - 1)` instead
250255
fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
251256
if_chain! {
252-
if let ExprKind::Binary(op, ref lhs, ref rhs) = expr.kind;
253-
if op.node == BinOpKind::Sub;
257+
if let ExprKind::Binary(Spanned { node: BinOpKind::Sub, .. }, ref lhs, ref rhs) = expr.kind;
254258
if cx.tables.expr_ty(lhs).is_floating_point();
255259
if let Some((value, _)) = constant(cx, cx.tables, rhs);
256260
if F32(1.0) == value || F64(1.0) == value;
@@ -276,8 +280,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
276280

277281
fn is_float_mul_expr<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'a>) -> Option<(&'a Expr<'a>, &'a Expr<'a>)> {
278282
if_chain! {
279-
if let ExprKind::Binary(op, ref lhs, ref rhs) = &expr.kind;
280-
if let BinOpKind::Mul = op.node;
283+
if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, ref lhs, ref rhs) = &expr.kind;
281284
if cx.tables.expr_ty(lhs).is_floating_point();
282285
if cx.tables.expr_ty(rhs).is_floating_point();
283286
then {
@@ -289,34 +292,37 @@ fn is_float_mul_expr<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'a>) -> Option
289292
}
290293

291294
// TODO: Fix rust-lang/rust-clippy#4735
292-
fn check_fma(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
293-
if_chain! {
294-
if let ExprKind::Binary(op, lhs, rhs) = &expr.kind;
295-
if let BinOpKind::Add = op.node;
296-
then {
297-
let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
298-
(inner_lhs, inner_rhs, rhs)
299-
} else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) {
300-
(inner_lhs, inner_rhs, lhs)
301-
} else {
302-
return;
303-
};
295+
fn check_mul_add(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
296+
if let ExprKind::Binary(
297+
Spanned {
298+
node: BinOpKind::Add, ..
299+
},
300+
lhs,
301+
rhs,
302+
) = &expr.kind
303+
{
304+
let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
305+
(inner_lhs, inner_rhs, rhs)
306+
} else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) {
307+
(inner_lhs, inner_rhs, lhs)
308+
} else {
309+
return;
310+
};
304311

305-
span_lint_and_sugg(
306-
cx,
307-
SUBOPTIMAL_FLOPS,
308-
expr.span,
309-
"multiply and add expressions can be calculated more efficiently and accurately",
310-
"consider using",
311-
format!(
312-
"{}.mul_add({}, {})",
313-
prepare_receiver_sugg(cx, recv),
314-
Sugg::hir(cx, arg1, ".."),
315-
Sugg::hir(cx, arg2, ".."),
316-
),
317-
Applicability::MachineApplicable,
318-
);
319-
}
312+
span_lint_and_sugg(
313+
cx,
314+
SUBOPTIMAL_FLOPS,
315+
expr.span,
316+
"multiply and add expressions can be calculated more efficiently and accurately",
317+
"consider using",
318+
format!(
319+
"{}.mul_add({}, {})",
320+
prepare_receiver_sugg(cx, recv),
321+
Sugg::hir(cx, arg1, ".."),
322+
Sugg::hir(cx, arg2, ".."),
323+
),
324+
Applicability::MachineApplicable,
325+
);
320326
}
321327
}
322328

@@ -335,7 +341,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
335341
}
336342
} else {
337343
check_expm1(cx, expr);
338-
check_fma(cx, expr);
344+
check_mul_add(cx, expr);
339345
}
340346
}
341347
}
File renamed without changes.

tests/ui/floating_point_fma.stderr renamed to tests/ui/floating_point_mul_add.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
error: multiply and add expressions can be calculated more efficiently and accurately
2-
--> $DIR/floating_point_fma.rs:10:13
2+
--> $DIR/floating_point_mul_add.rs:10:13
33
|
44
LL | let _ = a * b + c;
55
| ^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
66
|
77
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
88

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

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

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

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

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

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

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

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

tests/ui/floating_point_powf.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
let _ = x.powi(-2);
1616
let _ = x.powi(16_777_215);
1717
let _ = x.powi(-16_777_215);
18+
// Cases where the lint shouldn't be applied
1819
let _ = x.powf(2.1);
1920
let _ = x.powf(-2.1);
2021
let _ = x.powf(16_777_216.0);
@@ -33,6 +34,7 @@ fn main() {
3334
let _ = x.powi(-2);
3435
let _ = x.powi(-2_147_483_648);
3536
let _ = x.powi(2_147_483_647);
37+
// Cases where the lint shouldn't be applied
3638
let _ = x.powf(2.1);
3739
let _ = x.powf(-2.1);
3840
let _ = x.powf(-2_147_483_649.0);

tests/ui/floating_point_powf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
let _ = x.powf(-2.0);
1616
let _ = x.powf(16_777_215.0);
1717
let _ = x.powf(-16_777_215.0);
18+
// Cases where the lint shouldn't be applied
1819
let _ = x.powf(2.1);
1920
let _ = x.powf(-2.1);
2021
let _ = x.powf(16_777_216.0);
@@ -33,6 +34,7 @@ fn main() {
3334
let _ = x.powf(-2.0);
3435
let _ = x.powf(-2_147_483_648.0);
3536
let _ = x.powf(2_147_483_647.0);
37+
// Cases where the lint shouldn't be applied
3638
let _ = x.powf(2.1);
3739
let _ = x.powf(-2.1);
3840
let _ = x.powf(-2_147_483_649.0);

tests/ui/floating_point_powf.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,73 +73,73 @@ LL | let _ = x.powf(-16_777_215.0);
7373
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(-16_777_215)`
7474

7575
error: exponent for bases 2 and e can be computed more accurately
76-
--> $DIR/floating_point_powf.rs:24:13
76+
--> $DIR/floating_point_powf.rs:25:13
7777
|
7878
LL | let _ = 2f64.powf(x);
7979
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`
8080

8181
error: exponent for bases 2 and e can be computed more accurately
82-
--> $DIR/floating_point_powf.rs:25:13
82+
--> $DIR/floating_point_powf.rs:26:13
8383
|
8484
LL | let _ = 2f64.powf(3.1);
8585
| ^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp2()`
8686

8787
error: exponent for bases 2 and e can be computed more accurately
88-
--> $DIR/floating_point_powf.rs:26:13
88+
--> $DIR/floating_point_powf.rs:27:13
8989
|
9090
LL | let _ = 2f64.powf(-3.1);
9191
| ^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp2()`
9292

9393
error: exponent for bases 2 and e can be computed more accurately
94-
--> $DIR/floating_point_powf.rs:27:13
94+
--> $DIR/floating_point_powf.rs:28:13
9595
|
9696
LL | let _ = std::f64::consts::E.powf(x);
9797
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`
9898

9999
error: exponent for bases 2 and e can be computed more accurately
100-
--> $DIR/floating_point_powf.rs:28:13
100+
--> $DIR/floating_point_powf.rs:29:13
101101
|
102102
LL | let _ = std::f64::consts::E.powf(3.1);
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp()`
104104

105105
error: exponent for bases 2 and e can be computed more accurately
106-
--> $DIR/floating_point_powf.rs:29:13
106+
--> $DIR/floating_point_powf.rs:30:13
107107
|
108108
LL | let _ = std::f64::consts::E.powf(-3.1);
109109
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp()`
110110

111111
error: square-root of a number can be computed more efficiently and accurately
112-
--> $DIR/floating_point_powf.rs:30:13
112+
--> $DIR/floating_point_powf.rs:31:13
113113
|
114114
LL | let _ = x.powf(1.0 / 2.0);
115115
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`
116116

117117
error: cube-root of a number can be computed more accurately
118-
--> $DIR/floating_point_powf.rs:31:13
118+
--> $DIR/floating_point_powf.rs:32:13
119119
|
120120
LL | let _ = x.powf(1.0 / 3.0);
121121
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`
122122

123123
error: exponentiation with integer powers can be computed more efficiently
124-
--> $DIR/floating_point_powf.rs:32:13
124+
--> $DIR/floating_point_powf.rs:33:13
125125
|
126126
LL | let _ = x.powf(2.0);
127127
| ^^^^^^^^^^^ help: consider using: `x.powi(2)`
128128

129129
error: exponentiation with integer powers can be computed more efficiently
130-
--> $DIR/floating_point_powf.rs:33:13
130+
--> $DIR/floating_point_powf.rs:34:13
131131
|
132132
LL | let _ = x.powf(-2.0);
133133
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
134134

135135
error: exponentiation with integer powers can be computed more efficiently
136-
--> $DIR/floating_point_powf.rs:34:13
136+
--> $DIR/floating_point_powf.rs:35:13
137137
|
138138
LL | let _ = x.powf(-2_147_483_648.0);
139139
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(-2_147_483_648)`
140140

141141
error: exponentiation with integer powers can be computed more efficiently
142-
--> $DIR/floating_point_powf.rs:35:13
142+
--> $DIR/floating_point_powf.rs:36:13
143143
|
144144
LL | let _ = x.powf(2_147_483_647.0);
145145
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(2_147_483_647)`

0 commit comments

Comments
 (0)