@@ -13,11 +13,12 @@ use std::f32::consts as f32_consts;
1313use std:: f64:: consts as f64_consts;
1414
1515declare_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.
1819 ///
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 .
2122 ///
2223 /// **Known problems:** None
2324 ///
@@ -26,59 +27,43 @@ declare_clippy_lint! {
2627 /// ```rust
2728 /// use std::f32::consts::E;
2829 ///
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;
3240 /// ```
3341 ///
3442 /// is better expressed as
3543 ///
3644 /// ```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
5745 /// use std::f32::consts::E;
5846 ///
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();
6857 /// ```
69- pub SLOW_FLOATING_POINT_COMPUTATION ,
58+ pub FLOATING_POINT_IMPROVEMENTS ,
7059 nursery,
71- "checks for inefficient floating point computations "
60+ "looks for improvements to floating- point expressions "
7261}
7362
74- declare_lint_pass ! ( FloatingPointArithmetic => [
75- INACCURATE_FLOATING_POINT_COMPUTATION ,
76- SLOW_FLOATING_POINT_COMPUTATION
77- ] ) ;
63+ declare_lint_pass ! ( FloatingPointArithmetic => [ FLOATING_POINT_IMPROVEMENTS ] ) ;
7864
7965fn 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 ( ) ;
8267
8368 if let Some ( ( value, _) ) = constant ( cx, cx. tables , & args[ 1 ] ) {
8469 let method;
@@ -95,7 +80,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
9580
9681 span_lint_and_sugg (
9782 cx,
98- INACCURATE_FLOATING_POINT_COMPUTATION ,
83+ FLOATING_POINT_IMPROVEMENTS ,
9984 expr. span ,
10085 "logarithm for bases 2, 10 and e can be computed more accurately" ,
10186 "consider using" ,
@@ -118,7 +103,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
118103
119104 span_lint_and_sugg(
120105 cx,
121- INACCURATE_FLOATING_POINT_COMPUTATION ,
106+ FLOATING_POINT_IMPROVEMENTS ,
122107 expr. span,
123108 "ln(1 + x) can be computed more accurately" ,
124109 "consider using" ,
@@ -144,9 +129,9 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
144129
145130 span_lint_and_sugg (
146131 cx,
147- SLOW_FLOATING_POINT_COMPUTATION ,
132+ FLOATING_POINT_IMPROVEMENTS ,
148133 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 " ,
150135 "consider using" ,
151136 format ! ( "{}.{}()" , sugg:: Sugg :: hir( cx, & args[ 1 ] , ".." ) . maybe_par( ) , method) ,
152137 Applicability :: MachineApplicable ,
@@ -159,18 +144,18 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
159144 let method;
160145
161146 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 " ;
163148 method = "sqrt" ;
164149 } 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 " ;
166151 method = "cbrt" ;
167152 } else {
168153 return ;
169154 }
170155
171156 span_lint_and_sugg (
172157 cx,
173- SLOW_FLOATING_POINT_COMPUTATION ,
158+ FLOATING_POINT_IMPROVEMENTS ,
174159 expr. span ,
175160 help,
176161 "consider using" ,
@@ -194,7 +179,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
194179 then {
195180 span_lint_and_sugg(
196181 cx,
197- INACCURATE_FLOATING_POINT_COMPUTATION ,
182+ FLOATING_POINT_IMPROVEMENTS ,
198183 expr. span,
199184 "(e.pow(x) - 1) can be computed more accurately" ,
200185 "consider using" ,
0 commit comments