@@ -13,11 +13,12 @@ use std::f32::consts as f32_consts;
13
13
use std:: f64:: consts as f64_consts;
14
14
15
15
declare_clippy_lint ! {
16
- /// **What it does:** Looks for numerically unstable floating point
17
- /// computations and suggests better alternatives.
16
+ /// **What it does:** Looks for floating-point expressions that
17
+ /// can be expressed using built-in methods to improve accuracy,
18
+ /// performance and/or succinctness.
18
19
///
19
- /// **Why is this bad?** Numerically unstable floating point computations
20
- /// cause rounding errors to magnify and distorts the results strongly .
20
+ /// **Why is this bad?** Negatively affects accuracy, performance
21
+ /// and/or readability .
21
22
///
22
23
/// **Known problems:** None
23
24
///
@@ -26,59 +27,43 @@ declare_clippy_lint! {
26
27
/// ```rust
27
28
/// use std::f32::consts::E;
28
29
///
29
- /// let a = 1f32.log(2.0);
30
- /// let b = 1f32.log(10.0);
31
- /// let c = 1f32.log(E);
30
+ /// let a = 3f32;
31
+ /// let _ = (2f32).powf(a);
32
+ /// let _ = E.powf(a);
33
+ /// let _ = a.powf(1.0 / 2.0);
34
+ /// let _ = a.powf(1.0 / 3.0);
35
+ /// let _ = a.log(2.0);
36
+ /// let _ = a.log(10.0);
37
+ /// let _ = a.log(E);
38
+ /// let _ = (1.0 + a).ln();
39
+ /// let _ = a.exp() - 1.0;
32
40
/// ```
33
41
///
34
42
/// is better expressed as
35
43
///
36
44
/// ```rust
37
- /// let a = 1f32.log2();
38
- /// let b = 1f32.log10();
39
- /// let c = 1f32.ln();
40
- /// ```
41
- pub INACCURATE_FLOATING_POINT_COMPUTATION ,
42
- nursery,
43
- "checks for numerically unstable floating point computations"
44
- }
45
-
46
- declare_clippy_lint ! {
47
- /// **What it does:** Looks for inefficient floating point computations
48
- /// and suggests faster alternatives.
49
- ///
50
- /// **Why is this bad?** Lower performance.
51
- ///
52
- /// **Known problems:** None
53
- ///
54
- /// **Example:**
55
- ///
56
- /// ```rust
57
45
/// use std::f32::consts::E;
58
46
///
59
- /// let a = (2f32).powf(3.0);
60
- /// let c = E.powf(3.0);
61
- /// ```
62
- ///
63
- /// is better expressed as
64
- ///
65
- /// ```rust
66
- /// let a = (3f32).exp2();
67
- /// let b = (3f32).exp();
47
+ /// let a = 3f32;
48
+ /// let _ = a.exp2();
49
+ /// let _ = a.exp();
50
+ /// let _ = a.sqrt();
51
+ /// let _ = a.cbrt();
52
+ /// let _ = a.log2();
53
+ /// let _ = a.log10();
54
+ /// let _ = a.ln();
55
+ /// let _ = a.ln_1p();
56
+ /// let _ = a.exp_m1();
68
57
/// ```
69
- pub SLOW_FLOATING_POINT_COMPUTATION ,
58
+ pub FLOATING_POINT_IMPROVEMENTS ,
70
59
nursery,
71
- "checks for inefficient floating point computations "
60
+ "looks for improvements to floating- point expressions "
72
61
}
73
62
74
- declare_lint_pass ! ( FloatingPointArithmetic => [
75
- INACCURATE_FLOATING_POINT_COMPUTATION ,
76
- SLOW_FLOATING_POINT_COMPUTATION
77
- ] ) ;
63
+ declare_lint_pass ! ( FloatingPointArithmetic => [ FLOATING_POINT_IMPROVEMENTS ] ) ;
78
64
79
65
fn check_log_base ( cx : & LateContext < ' _ , ' _ > , expr : & Expr , args : & HirVec < Expr > ) {
80
- let recv = & args[ 0 ] ;
81
- let arg = sugg:: Sugg :: hir ( cx, recv, ".." ) . maybe_par ( ) ;
66
+ let arg = sugg:: Sugg :: hir ( cx, & args[ 0 ] , ".." ) . maybe_par ( ) ;
82
67
83
68
if let Some ( ( value, _) ) = constant ( cx, cx. tables , & args[ 1 ] ) {
84
69
let method;
@@ -95,7 +80,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
95
80
96
81
span_lint_and_sugg (
97
82
cx,
98
- INACCURATE_FLOATING_POINT_COMPUTATION ,
83
+ FLOATING_POINT_IMPROVEMENTS ,
99
84
expr. span ,
100
85
"logarithm for bases 2, 10 and e can be computed more accurately" ,
101
86
"consider using" ,
@@ -118,7 +103,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
118
103
119
104
span_lint_and_sugg(
120
105
cx,
121
- INACCURATE_FLOATING_POINT_COMPUTATION ,
106
+ FLOATING_POINT_IMPROVEMENTS ,
122
107
expr. span,
123
108
"ln(1 + x) can be computed more accurately" ,
124
109
"consider using" ,
@@ -144,9 +129,9 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
144
129
145
130
span_lint_and_sugg (
146
131
cx,
147
- SLOW_FLOATING_POINT_COMPUTATION ,
132
+ FLOATING_POINT_IMPROVEMENTS ,
148
133
expr. span ,
149
- "exponent for bases 2 and e can be computed more efficiently " ,
134
+ "exponent for bases 2 and e can be computed more accurately " ,
150
135
"consider using" ,
151
136
format ! ( "{}.{}()" , sugg:: Sugg :: hir( cx, & args[ 1 ] , ".." ) . maybe_par( ) , method) ,
152
137
Applicability :: MachineApplicable ,
@@ -159,18 +144,18 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
159
144
let method;
160
145
161
146
if F32 ( 1.0 / 2.0 ) == value || F64 ( 1.0 / 2.0 ) == value {
162
- help = "square-root of a number can be computer more efficiently" ;
147
+ help = "square-root of a number can be computed more efficiently and accurately " ;
163
148
method = "sqrt" ;
164
149
} else if F32 ( 1.0 / 3.0 ) == value || F64 ( 1.0 / 3.0 ) == value {
165
- help = "cube-root of a number can be computer more efficiently " ;
150
+ help = "cube-root of a number can be computed more accurately " ;
166
151
method = "cbrt" ;
167
152
} else {
168
153
return ;
169
154
}
170
155
171
156
span_lint_and_sugg (
172
157
cx,
173
- SLOW_FLOATING_POINT_COMPUTATION ,
158
+ FLOATING_POINT_IMPROVEMENTS ,
174
159
expr. span ,
175
160
help,
176
161
"consider using" ,
@@ -194,7 +179,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
194
179
then {
195
180
span_lint_and_sugg(
196
181
cx,
197
- INACCURATE_FLOATING_POINT_COMPUTATION ,
182
+ FLOATING_POINT_IMPROVEMENTS ,
198
183
expr. span,
199
184
"(e.pow(x) - 1) can be computed more accurately" ,
200
185
"consider using" ,
0 commit comments