]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/floating_point_arithmetic.rs
b1e258f4b160a093676822f9a98bc0beb799531c
[rust.git] / clippy_lints / src / floating_point_arithmetic.rs
1 use crate::consts::{
2     constant, constant_simple, Constant,
3     Constant::{Int, F32, F64},
4 };
5 use crate::utils::{get_parent_expr, higher, numeric_literal, span_lint_and_sugg, sugg, SpanlessEq};
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
8 use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
9 use rustc_lint::{LateContext, LateLintPass};
10 use rustc_middle::ty;
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
12 use rustc_span::source_map::Spanned;
13
14 use rustc_ast::ast;
15 use std::f32::consts as f32_consts;
16 use std::f64::consts as f64_consts;
17 use sugg::Sugg;
18
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     /// let a = 3f32;
32     /// let _ = a.powf(1.0 / 3.0);
33     /// let _ = (1.0 + a).ln();
34     /// let _ = a.exp() - 1.0;
35     /// ```
36     ///
37     /// is better expressed as
38     ///
39     /// ```rust
40     /// let a = 3f32;
41     /// let _ = a.cbrt();
42     /// let _ = a.ln_1p();
43     /// let _ = a.exp_m1();
44     /// ```
45     pub IMPRECISE_FLOPS,
46     nursery,
47     "usage of imprecise floating point operations"
48 }
49
50 declare_clippy_lint! {
51     /// **What it does:** Looks for floating-point expressions that
52     /// can be expressed using built-in methods to improve both
53     /// accuracy and performance.
54     ///
55     /// **Why is this bad?** Negatively impacts accuracy and performance.
56     ///
57     /// **Known problems:** None
58     ///
59     /// **Example:**
60     ///
61     /// ```rust
62     /// use std::f32::consts::E;
63     ///
64     /// let a = 3f32;
65     /// let _ = (2f32).powf(a);
66     /// let _ = E.powf(a);
67     /// let _ = a.powf(1.0 / 2.0);
68     /// let _ = a.log(2.0);
69     /// let _ = a.log(10.0);
70     /// let _ = a.log(E);
71     /// let _ = a.powf(2.0);
72     /// let _ = a * 2.0 + 4.0;
73     /// let _ = if a < 0.0 {
74     ///     -a
75     /// } else {
76     ///     a
77     /// };
78     /// let _ = if a < 0.0 {
79     ///     a
80     /// } else {
81     ///     -a
82     /// };
83     /// ```
84     ///
85     /// is better expressed as
86     ///
87     /// ```rust
88     /// use std::f32::consts::E;
89     ///
90     /// let a = 3f32;
91     /// let _ = a.exp2();
92     /// let _ = a.exp();
93     /// let _ = a.sqrt();
94     /// let _ = a.log2();
95     /// let _ = a.log10();
96     /// let _ = a.ln();
97     /// let _ = a.powi(2);
98     /// let _ = a.mul_add(2.0, 4.0);
99     /// let _ = a.abs();
100     /// let _ = -a.abs();
101     /// ```
102     pub SUBOPTIMAL_FLOPS,
103     nursery,
104     "usage of sub-optimal floating point operations"
105 }
106
107 declare_lint_pass!(FloatingPointArithmetic => [
108     IMPRECISE_FLOPS,
109     SUBOPTIMAL_FLOPS
110 ]);
111
112 // Returns the specialized log method for a given base if base is constant
113 // and is one of 2, 10 and e
114 fn get_specialized_log_method(cx: &LateContext<'_>, base: &Expr<'_>) -> Option<&'static str> {
115     if let Some((value, _)) = constant(cx, cx.tables(), base) {
116         if F32(2.0) == value || F64(2.0) == value {
117             return Some("log2");
118         } else if F32(10.0) == value || F64(10.0) == value {
119             return Some("log10");
120         } else if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
121             return Some("ln");
122         }
123     }
124
125     None
126 }
127
128 // Adds type suffixes and parenthesis to method receivers if necessary
129 fn prepare_receiver_sugg<'a>(cx: &LateContext<'_>, mut expr: &'a Expr<'a>) -> Sugg<'a> {
130     let mut suggestion = Sugg::hir(cx, expr, "..");
131
132     if let ExprKind::Unary(UnOp::UnNeg, inner_expr) = &expr.kind {
133         expr = &inner_expr;
134     }
135
136     if_chain! {
137         // if the expression is a float literal and it is unsuffixed then
138         // add a suffix so the suggestion is valid and unambiguous
139         if let ty::Float(float_ty) = cx.tables().expr_ty(expr).kind;
140         if let ExprKind::Lit(lit) = &expr.kind;
141         if let ast::LitKind::Float(sym, ast::LitFloatType::Unsuffixed) = lit.node;
142         then {
143             let op = format!(
144                 "{}{}{}",
145                 suggestion,
146                 // Check for float literals without numbers following the decimal
147                 // separator such as `2.` and adds a trailing zero
148                 if sym.as_str().ends_with('.') {
149                     "0"
150                 } else {
151                     ""
152                 },
153                 float_ty.name_str()
154             ).into();
155
156             suggestion = match suggestion {
157                 Sugg::MaybeParen(_) => Sugg::MaybeParen(op),
158                 _ => Sugg::NonParen(op)
159             };
160         }
161     }
162
163     suggestion.maybe_par()
164 }
165
166 fn check_log_base(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
167     if let Some(method) = get_specialized_log_method(cx, &args[1]) {
168         span_lint_and_sugg(
169             cx,
170             SUBOPTIMAL_FLOPS,
171             expr.span,
172             "logarithm for bases 2, 10 and e can be computed more accurately",
173             "consider using",
174             format!("{}.{}()", Sugg::hir(cx, &args[0], ".."), method),
175             Applicability::MachineApplicable,
176         );
177     }
178 }
179
180 // TODO: Lint expressions of the form `(x + y).ln()` where y > 1 and
181 // suggest usage of `(x + (y - 1)).ln_1p()` instead
182 fn check_ln1p(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
183     if let ExprKind::Binary(
184         Spanned {
185             node: BinOpKind::Add, ..
186         },
187         lhs,
188         rhs,
189     ) = &args[0].kind
190     {
191         let recv = match (constant(cx, cx.tables(), lhs), constant(cx, cx.tables(), rhs)) {
192             (Some((value, _)), _) if F32(1.0) == value || F64(1.0) == value => rhs,
193             (_, Some((value, _))) if F32(1.0) == value || F64(1.0) == value => lhs,
194             _ => return,
195         };
196
197         span_lint_and_sugg(
198             cx,
199             IMPRECISE_FLOPS,
200             expr.span,
201             "ln(1 + x) can be computed more accurately",
202             "consider using",
203             format!("{}.ln_1p()", prepare_receiver_sugg(cx, recv)),
204             Applicability::MachineApplicable,
205         );
206     }
207 }
208
209 // Returns an integer if the float constant is a whole number and it can be
210 // converted to an integer without loss of precision. For now we only check
211 // ranges [-16777215, 16777216) for type f32 as whole number floats outside
212 // this range are lossy and ambiguous.
213 #[allow(clippy::cast_possible_truncation)]
214 fn get_integer_from_float_constant(value: &Constant) -> Option<i32> {
215     match value {
216         F32(num) if num.fract() == 0.0 => {
217             if (-16_777_215.0..16_777_216.0).contains(num) {
218                 Some(num.round() as i32)
219             } else {
220                 None
221             }
222         },
223         F64(num) if num.fract() == 0.0 => {
224             if (-2_147_483_648.0..2_147_483_648.0).contains(num) {
225                 Some(num.round() as i32)
226             } else {
227                 None
228             }
229         },
230         _ => None,
231     }
232 }
233
234 fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
235     // Check receiver
236     if let Some((value, _)) = constant(cx, cx.tables(), &args[0]) {
237         let method = if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
238             "exp"
239         } else if F32(2.0) == value || F64(2.0) == value {
240             "exp2"
241         } else {
242             return;
243         };
244
245         span_lint_and_sugg(
246             cx,
247             SUBOPTIMAL_FLOPS,
248             expr.span,
249             "exponent for bases 2 and e can be computed more accurately",
250             "consider using",
251             format!("{}.{}()", prepare_receiver_sugg(cx, &args[1]), method),
252             Applicability::MachineApplicable,
253         );
254     }
255
256     // Check argument
257     if let Some((value, _)) = constant(cx, cx.tables(), &args[1]) {
258         let (lint, help, suggestion) = if F32(1.0 / 2.0) == value || F64(1.0 / 2.0) == value {
259             (
260                 SUBOPTIMAL_FLOPS,
261                 "square-root of a number can be computed more efficiently and accurately",
262                 format!("{}.sqrt()", Sugg::hir(cx, &args[0], "..")),
263             )
264         } else if F32(1.0 / 3.0) == value || F64(1.0 / 3.0) == value {
265             (
266                 IMPRECISE_FLOPS,
267                 "cube-root of a number can be computed more accurately",
268                 format!("{}.cbrt()", Sugg::hir(cx, &args[0], "..")),
269             )
270         } else if let Some(exponent) = get_integer_from_float_constant(&value) {
271             (
272                 SUBOPTIMAL_FLOPS,
273                 "exponentiation with integer powers can be computed more efficiently",
274                 format!(
275                     "{}.powi({})",
276                     Sugg::hir(cx, &args[0], ".."),
277                     numeric_literal::format(&exponent.to_string(), None, false)
278                 ),
279             )
280         } else {
281             return;
282         };
283
284         span_lint_and_sugg(
285             cx,
286             lint,
287             expr.span,
288             help,
289             "consider using",
290             suggestion,
291             Applicability::MachineApplicable,
292         );
293     }
294 }
295
296 fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
297     // Check argument
298     if let Some((value, _)) = constant(cx, cx.tables(), &args[1]) {
299         // TODO: need more specific check. this is too wide. remember also to include tests
300         if let Some(parent) = get_parent_expr(cx, expr) {
301             if let Some(grandparent) = get_parent_expr(cx, parent) {
302                 if let ExprKind::MethodCall(PathSegment { ident: method_name, .. }, _, args, _) = grandparent.kind {
303                     if method_name.as_str() == "sqrt" && detect_hypot(cx, args).is_some() {
304                         return;
305                     }
306                 }
307             }
308         }
309
310         let (lint, help, suggestion) = match value {
311             Int(2) => (
312                 IMPRECISE_FLOPS,
313                 "square can be computed more accurately",
314                 format!("{} * {}", Sugg::hir(cx, &args[0], ".."), Sugg::hir(cx, &args[0], "..")),
315             ),
316             _ => return,
317         };
318
319         span_lint_and_sugg(
320             cx,
321             lint,
322             expr.span,
323             help,
324             "consider using",
325             suggestion,
326             Applicability::MachineApplicable,
327         );
328     }
329 }
330
331 fn detect_hypot(cx: &LateContext<'_>, args: &[Expr<'_>]) -> Option<String> {
332     if let ExprKind::Binary(
333         Spanned {
334             node: BinOpKind::Add, ..
335         },
336         ref add_lhs,
337         ref add_rhs,
338     ) = args[0].kind
339     {
340         // check if expression of the form x * x + y * y
341         if_chain! {
342             if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, ref lmul_lhs, ref lmul_rhs) = add_lhs.kind;
343             if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, ref rmul_lhs, ref rmul_rhs) = add_rhs.kind;
344             if are_exprs_equal(cx, lmul_lhs, lmul_rhs);
345             if are_exprs_equal(cx, rmul_lhs, rmul_rhs);
346             then {
347                 return Some(format!("{}.hypot({})", Sugg::hir(cx, &lmul_lhs, ".."), Sugg::hir(cx, &rmul_lhs, "..")));
348             }
349         }
350
351         // check if expression of the form x.powi(2) + y.powi(2)
352         if_chain! {
353             if let ExprKind::MethodCall(
354                 PathSegment { ident: lmethod_name, .. },
355                 ref _lspan,
356                 ref largs,
357                 _
358             ) = add_lhs.kind;
359             if let ExprKind::MethodCall(
360                 PathSegment { ident: rmethod_name, .. },
361                 ref _rspan,
362                 ref rargs,
363                 _
364             ) = add_rhs.kind;
365             if lmethod_name.as_str() == "powi" && rmethod_name.as_str() == "powi";
366             if let Some((lvalue, _)) = constant(cx, cx.tables(), &largs[1]);
367             if let Some((rvalue, _)) = constant(cx, cx.tables(), &rargs[1]);
368             if Int(2) == lvalue && Int(2) == rvalue;
369             then {
370                 return Some(format!("{}.hypot({})", Sugg::hir(cx, &largs[0], ".."), Sugg::hir(cx, &rargs[0], "..")));
371             }
372         }
373     }
374
375     None
376 }
377
378 fn check_hypot(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
379     if let Some(message) = detect_hypot(cx, args) {
380         span_lint_and_sugg(
381             cx,
382             IMPRECISE_FLOPS,
383             expr.span,
384             "hypotenuse can be computed more accurately",
385             "consider using",
386             message,
387             Applicability::MachineApplicable,
388         );
389     }
390 }
391
392 // TODO: Lint expressions of the form `x.exp() - y` where y > 1
393 // and suggest usage of `x.exp_m1() - (y - 1)` instead
394 fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
395     if_chain! {
396         if let ExprKind::Binary(Spanned { node: BinOpKind::Sub, .. }, ref lhs, ref rhs) = expr.kind;
397         if cx.tables().expr_ty(lhs).is_floating_point();
398         if let Some((value, _)) = constant(cx, cx.tables(), rhs);
399         if F32(1.0) == value || F64(1.0) == value;
400         if let ExprKind::MethodCall(ref path, _, ref method_args, _) = lhs.kind;
401         if cx.tables().expr_ty(&method_args[0]).is_floating_point();
402         if path.ident.name.as_str() == "exp";
403         then {
404             span_lint_and_sugg(
405                 cx,
406                 IMPRECISE_FLOPS,
407                 expr.span,
408                 "(e.pow(x) - 1) can be computed more accurately",
409                 "consider using",
410                 format!(
411                     "{}.exp_m1()",
412                     Sugg::hir(cx, &method_args[0], "..")
413                 ),
414                 Applicability::MachineApplicable,
415             );
416         }
417     }
418 }
419
420 fn is_float_mul_expr<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(&'a Expr<'a>, &'a Expr<'a>)> {
421     if_chain! {
422         if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, ref lhs, ref rhs) = &expr.kind;
423         if cx.tables().expr_ty(lhs).is_floating_point();
424         if cx.tables().expr_ty(rhs).is_floating_point();
425         then {
426             return Some((lhs, rhs));
427         }
428     }
429
430     None
431 }
432
433 // TODO: Fix rust-lang/rust-clippy#4735
434 fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
435     if let ExprKind::Binary(
436         Spanned {
437             node: BinOpKind::Add, ..
438         },
439         lhs,
440         rhs,
441     ) = &expr.kind
442     {
443         if let Some(parent) = get_parent_expr(cx, expr) {
444             if let ExprKind::MethodCall(PathSegment { ident: method_name, .. }, _, args, _) = parent.kind {
445                 if method_name.as_str() == "sqrt" && detect_hypot(cx, args).is_some() {
446                     return;
447                 }
448             }
449         }
450
451         let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
452             (inner_lhs, inner_rhs, rhs)
453         } else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) {
454             (inner_lhs, inner_rhs, lhs)
455         } else {
456             return;
457         };
458
459         span_lint_and_sugg(
460             cx,
461             SUBOPTIMAL_FLOPS,
462             expr.span,
463             "multiply and add expressions can be calculated more efficiently and accurately",
464             "consider using",
465             format!(
466                 "{}.mul_add({}, {})",
467                 prepare_receiver_sugg(cx, recv),
468                 Sugg::hir(cx, arg1, ".."),
469                 Sugg::hir(cx, arg2, ".."),
470             ),
471             Applicability::MachineApplicable,
472         );
473     }
474 }
475
476 /// Returns true iff expr is an expression which tests whether or not
477 /// test is positive or an expression which tests whether or not test
478 /// is nonnegative.
479 /// Used for check-custom-abs function below
480 fn is_testing_positive(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
481     if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
482         match op {
483             BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, right) && are_exprs_equal(cx, left, test),
484             BinOpKind::Lt | BinOpKind::Le => is_zero(cx, left) && are_exprs_equal(cx, right, test),
485             _ => false,
486         }
487     } else {
488         false
489     }
490 }
491
492 /// See [`is_testing_positive`]
493 fn is_testing_negative(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
494     if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
495         match op {
496             BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, left) && are_exprs_equal(cx, right, test),
497             BinOpKind::Lt | BinOpKind::Le => is_zero(cx, right) && are_exprs_equal(cx, left, test),
498             _ => false,
499         }
500     } else {
501         false
502     }
503 }
504
505 fn are_exprs_equal(cx: &LateContext<'_>, expr1: &Expr<'_>, expr2: &Expr<'_>) -> bool {
506     SpanlessEq::new(cx).ignore_fn().eq_expr(expr1, expr2)
507 }
508
509 /// Returns true iff expr is some zero literal
510 fn is_zero(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
511     match constant_simple(cx, cx.tables(), expr) {
512         Some(Constant::Int(i)) => i == 0,
513         Some(Constant::F32(f)) => f == 0.0,
514         Some(Constant::F64(f)) => f == 0.0,
515         _ => false,
516     }
517 }
518
519 /// If the two expressions are negations of each other, then it returns
520 /// a tuple, in which the first element is true iff expr1 is the
521 /// positive expressions, and the second element is the positive
522 /// one of the two expressions
523 /// If the two expressions are not negations of each other, then it
524 /// returns None.
525 fn are_negated<'a>(cx: &LateContext<'_>, expr1: &'a Expr<'a>, expr2: &'a Expr<'a>) -> Option<(bool, &'a Expr<'a>)> {
526     if let ExprKind::Unary(UnOp::UnNeg, expr1_negated) = &expr1.kind {
527         if are_exprs_equal(cx, expr1_negated, expr2) {
528             return Some((false, expr2));
529         }
530     }
531     if let ExprKind::Unary(UnOp::UnNeg, expr2_negated) = &expr2.kind {
532         if are_exprs_equal(cx, expr1, expr2_negated) {
533             return Some((true, expr1));
534         }
535     }
536     None
537 }
538
539 fn check_custom_abs(cx: &LateContext<'_>, expr: &Expr<'_>) {
540     if_chain! {
541         if let Some((cond, body, Some(else_body))) = higher::if_block(&expr);
542         if let ExprKind::Block(block, _) = body.kind;
543         if block.stmts.is_empty();
544         if let Some(if_body_expr) = block.expr;
545         if let ExprKind::Block(else_block, _) = else_body.kind;
546         if else_block.stmts.is_empty();
547         if let Some(else_body_expr) = else_block.expr;
548         if let Some((if_expr_positive, body)) = are_negated(cx, if_body_expr, else_body_expr);
549         then {
550             let positive_abs_sugg = (
551                 "manual implementation of `abs` method",
552                 format!("{}.abs()", Sugg::hir(cx, body, "..")),
553             );
554             let negative_abs_sugg = (
555                 "manual implementation of negation of `abs` method",
556                 format!("-{}.abs()", Sugg::hir(cx, body, "..")),
557             );
558             let sugg = if is_testing_positive(cx, cond, body) {
559                 if if_expr_positive {
560                     positive_abs_sugg
561                 } else {
562                     negative_abs_sugg
563                 }
564             } else if is_testing_negative(cx, cond, body) {
565                 if if_expr_positive {
566                     negative_abs_sugg
567                 } else {
568                     positive_abs_sugg
569                 }
570             } else {
571                 return;
572             };
573             span_lint_and_sugg(
574                 cx,
575                 SUBOPTIMAL_FLOPS,
576                 expr.span,
577                 sugg.0,
578                 "try",
579                 sugg.1,
580                 Applicability::MachineApplicable,
581             );
582         }
583     }
584 }
585
586 fn are_same_base_logs(cx: &LateContext<'_>, expr_a: &Expr<'_>, expr_b: &Expr<'_>) -> bool {
587     if_chain! {
588         if let ExprKind::MethodCall(PathSegment { ident: method_name_a, .. }, _, ref args_a, _) = expr_a.kind;
589         if let ExprKind::MethodCall(PathSegment { ident: method_name_b, .. }, _, ref args_b, _) = expr_b.kind;
590         then {
591             return method_name_a.as_str() == method_name_b.as_str() &&
592                 args_a.len() == args_b.len() &&
593                 (
594                     ["ln", "log2", "log10"].contains(&&*method_name_a.as_str()) ||
595                     method_name_a.as_str() == "log" && args_a.len() == 2 && are_exprs_equal(cx, &args_a[1], &args_b[1])
596                 );
597         }
598     }
599
600     false
601 }
602
603 fn check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>) {
604     // check if expression of the form x.logN() / y.logN()
605     if_chain! {
606         if let ExprKind::Binary(
607             Spanned {
608                 node: BinOpKind::Div, ..
609             },
610             lhs,
611             rhs,
612         ) = &expr.kind;
613         if are_same_base_logs(cx, lhs, rhs);
614         if let ExprKind::MethodCall(_, _, ref largs, _) = lhs.kind;
615         if let ExprKind::MethodCall(_, _, ref rargs, _) = rhs.kind;
616         then {
617             span_lint_and_sugg(
618                 cx,
619                 SUBOPTIMAL_FLOPS,
620                 expr.span,
621                 "log base can be expressed more clearly",
622                 "consider using",
623                 format!("{}.log({})", Sugg::hir(cx, &largs[0], ".."), Sugg::hir(cx, &rargs[0], ".."),),
624                 Applicability::MachineApplicable,
625             );
626         }
627     }
628 }
629
630 fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
631     if_chain! {
632         if let ExprKind::Binary(
633             Spanned {
634                 node: BinOpKind::Div, ..
635             },
636             div_lhs,
637             div_rhs,
638         ) = &expr.kind;
639         if let ExprKind::Binary(
640             Spanned {
641                 node: BinOpKind::Mul, ..
642             },
643             mul_lhs,
644             mul_rhs,
645         ) = &div_lhs.kind;
646         if let Some((rvalue, _)) = constant(cx, cx.tables(), div_rhs);
647         if let Some((lvalue, _)) = constant(cx, cx.tables(), mul_rhs);
648         then {
649             if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&
650                (F32(180_f32) == lvalue || F64(180_f64) == lvalue)
651             {
652                 span_lint_and_sugg(
653                     cx,
654                     IMPRECISE_FLOPS,
655                     expr.span,
656                     "conversion to degrees can be done more accurately",
657                     "consider using",
658                     format!("{}.to_degrees()", Sugg::hir(cx, &mul_lhs, "..")),
659                     Applicability::MachineApplicable,
660                 );
661             } else if
662                 (F32(180_f32) == rvalue || F64(180_f64) == rvalue) &&
663                 (F32(f32_consts::PI) == lvalue || F64(f64_consts::PI) == lvalue)
664             {
665                 span_lint_and_sugg(
666                     cx,
667                     IMPRECISE_FLOPS,
668                     expr.span,
669                     "conversion to radians can be done more accurately",
670                     "consider using",
671                     format!("{}.to_radians()", Sugg::hir(cx, &mul_lhs, "..")),
672                     Applicability::MachineApplicable,
673                 );
674             }
675         }
676     }
677 }
678
679 impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
680     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
681         if let ExprKind::MethodCall(ref path, _, args, _) = &expr.kind {
682             let recv_ty = cx.tables().expr_ty(&args[0]);
683
684             if recv_ty.is_floating_point() {
685                 match &*path.ident.name.as_str() {
686                     "ln" => check_ln1p(cx, expr, args),
687                     "log" => check_log_base(cx, expr, args),
688                     "powf" => check_powf(cx, expr, args),
689                     "powi" => check_powi(cx, expr, args),
690                     "sqrt" => check_hypot(cx, expr, args),
691                     _ => {},
692                 }
693             }
694         } else {
695             check_expm1(cx, expr);
696             check_mul_add(cx, expr);
697             check_custom_abs(cx, expr);
698             check_log_division(cx, expr);
699             check_radians(cx, expr);
700         }
701     }
702 }