@@ -16,6 +16,39 @@ use std::f64::consts as f64_consts;
1616use sugg:: { format_numeric_literal, Sugg } ;
1717use syntax:: ast;
1818
19+ declare_clippy_lint ! {
20+ /// **What it does:** Looks for floating-point expressions that
21+ /// can be expressed using built-in methods to improve accuracy
22+ /// at the cost of performance.
23+ ///
24+ /// **Why is this bad?** Negatively impacts accuracy.
25+ ///
26+ /// **Known problems:** None
27+ ///
28+ /// **Example:**
29+ ///
30+ /// ```rust
31+ ///
32+ /// let a = 3f32;
33+ /// let _ = a.powf(1.0 / 3.0);
34+ /// let _ = (1.0 + a).ln();
35+ /// let _ = a.exp() - 1.0;
36+ /// ```
37+ ///
38+ /// is better expressed as
39+ ///
40+ /// ```rust
41+ ///
42+ /// let a = 3f32;
43+ /// let _ = a.cbrt();
44+ /// let _ = a.ln_1p();
45+ /// let _ = a.exp_m1();
46+ /// ```
47+ pub IMPRECISE_FLOPS ,
48+ nursery,
49+ "usage of imprecise floating point operations"
50+ }
51+
1952declare_clippy_lint ! {
2053 /// **What it does:** Looks for floating-point expressions that
2154 /// can be expressed using built-in methods to improve both
@@ -34,12 +67,9 @@ declare_clippy_lint! {
3467 /// let _ = (2f32).powf(a);
3568 /// let _ = E.powf(a);
3669 /// let _ = a.powf(1.0 / 2.0);
37- /// let _ = a.powf(1.0 / 3.0);
3870 /// let _ = a.log(2.0);
3971 /// let _ = a.log(10.0);
4072 /// let _ = a.log(E);
41- /// let _ = (1.0 + a).ln();
42- /// let _ = a.exp() - 1.0;
4373 /// let _ = a.powf(2.0);
4474 /// let _ = a * 2.0 + 4.0;
4575 /// ```
@@ -53,12 +83,9 @@ declare_clippy_lint! {
5383 /// let _ = a.exp2();
5484 /// let _ = a.exp();
5585 /// let _ = a.sqrt();
56- /// let _ = a.cbrt();
5786 /// let _ = a.log2();
5887 /// let _ = a.log10();
5988 /// let _ = a.ln();
60- /// let _ = a.ln_1p();
61- /// let _ = a.exp_m1();
6289 /// let _ = a.powi(2);
6390 /// let _ = a.mul_add(2.0, 4.0);
6491 /// ```
@@ -67,7 +94,10 @@ declare_clippy_lint! {
6794 "usage of sub-optimal floating point operations"
6895}
6996
70- declare_lint_pass ! ( FloatingPointArithmetic => [ SUBOPTIMAL_FLOPS ] ) ;
97+ declare_lint_pass ! ( FloatingPointArithmetic => [
98+ IMPRECISE_FLOPS ,
99+ SUBOPTIMAL_FLOPS
100+ ] ) ;
71101
72102// Returns the specialized log method for a given base if base is constant
73103// and is one of 2, 10 and e
@@ -156,7 +186,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
156186
157187 span_lint_and_sugg (
158188 cx,
159- SUBOPTIMAL_FLOPS ,
189+ IMPRECISE_FLOPS ,
160190 expr. span ,
161191 "ln(1 + x) can be computed more accurately" ,
162192 "consider using" ,
@@ -215,18 +245,21 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
215245
216246 // Check argument
217247 if let Some ( ( value, _) ) = constant ( cx, cx. tables , & args[ 1 ] ) {
218- let ( help, suggestion) = if F32 ( 1.0 / 2.0 ) == value || F64 ( 1.0 / 2.0 ) == value {
248+ let ( lint , help, suggestion) = if F32 ( 1.0 / 2.0 ) == value || F64 ( 1.0 / 2.0 ) == value {
219249 (
250+ SUBOPTIMAL_FLOPS ,
220251 "square-root of a number can be computed more efficiently and accurately" ,
221252 format ! ( "{}.sqrt()" , Sugg :: hir( cx, & args[ 0 ] , ".." ) ) ,
222253 )
223254 } else if F32 ( 1.0 / 3.0 ) == value || F64 ( 1.0 / 3.0 ) == value {
224255 (
256+ IMPRECISE_FLOPS ,
225257 "cube-root of a number can be computed more accurately" ,
226258 format ! ( "{}.cbrt()" , Sugg :: hir( cx, & args[ 0 ] , ".." ) ) ,
227259 )
228260 } else if let Some ( exponent) = get_integer_from_float_constant ( & value) {
229261 (
262+ SUBOPTIMAL_FLOPS ,
230263 "exponentiation with integer powers can be computed more efficiently" ,
231264 format ! (
232265 "{}.powi({})" ,
@@ -240,7 +273,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
240273
241274 span_lint_and_sugg (
242275 cx,
243- SUBOPTIMAL_FLOPS ,
276+ lint ,
244277 expr. span ,
245278 help,
246279 "consider using" ,
@@ -264,7 +297,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
264297 then {
265298 span_lint_and_sugg(
266299 cx,
267- SUBOPTIMAL_FLOPS ,
300+ IMPRECISE_FLOPS ,
268301 expr. span,
269302 "(e.pow(x) - 1) can be computed more accurately" ,
270303 "consider using" ,
0 commit comments