]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mul_add.rs
Auto merge of #4938 - flip1995:rustup, r=flip1995
[rust.git] / clippy_lints / src / mul_add.rs
1 use rustc::declare_lint_pass;
2 use rustc::hir::*;
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc_errors::Applicability;
5 use rustc_session::declare_tool_lint;
6
7 use crate::utils::*;
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for expressions of the form `a * b + c`
11     /// or `c + a * b` where `a`, `b`, `c` are floats and suggests using
12     /// `a.mul_add(b, c)` instead.
13     ///
14     /// **Why is this bad?** Calculating `a * b + c` may lead to slight
15     /// numerical inaccuracies as `a * b` is rounded before being added to
16     /// `c`. Depending on the target architecture, `mul_add()` may be more
17     /// performant.
18     ///
19     /// **Known problems:** This lint can emit semantic incorrect suggestions.
20     /// For example, for `a * b * c + d` the suggestion `a * b.mul_add(c, d)`
21     /// is emitted, which is equivalent to `a * (b * c + d)`. (#4735)
22     ///
23     /// **Example:**
24     ///
25     /// ```rust
26     /// # let a = 0_f32;
27     /// # let b = 0_f32;
28     /// # let c = 0_f32;
29     /// let foo = (a * b) + c;
30     /// ```
31     ///
32     /// can be written as
33     ///
34     /// ```rust
35     /// # let a = 0_f32;
36     /// # let b = 0_f32;
37     /// # let c = 0_f32;
38     /// let foo = a.mul_add(b, c);
39     /// ```
40     pub MANUAL_MUL_ADD,
41     nursery,
42     "Using `a.mul_add(b, c)` for floating points has higher numerical precision than `a * b + c`"
43 }
44
45 declare_lint_pass!(MulAddCheck => [MANUAL_MUL_ADD]);
46
47 fn is_float<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr) -> bool {
48     cx.tables.expr_ty(expr).is_floating_point()
49 }
50
51 // Checks whether expression is multiplication of two floats
52 fn is_float_mult_expr<'a, 'tcx, 'b>(cx: &LateContext<'a, 'tcx>, expr: &'b Expr) -> Option<(&'b Expr, &'b Expr)> {
53     if let ExprKind::Binary(op, lhs, rhs) = &expr.kind {
54         if let BinOpKind::Mul = op.node {
55             if is_float(cx, &lhs) && is_float(cx, &rhs) {
56                 return Some((&lhs, &rhs));
57             }
58         }
59     }
60
61     None
62 }
63
64 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MulAddCheck {
65     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
66         if let ExprKind::Binary(op, lhs, rhs) = &expr.kind {
67             if let BinOpKind::Add = op.node {
68                 //Converts mult_lhs * mult_rhs + rhs to mult_lhs.mult_add(mult_rhs, rhs)
69                 if let Some((mult_lhs, mult_rhs)) = is_float_mult_expr(cx, lhs) {
70                     if is_float(cx, rhs) {
71                         span_lint_and_sugg(
72                             cx,
73                             MANUAL_MUL_ADD,
74                             expr.span,
75                             "consider using mul_add() for better numerical precision",
76                             "try",
77                             format!(
78                                 "{}.mul_add({}, {})",
79                                 snippet(cx, mult_lhs.span, "_"),
80                                 snippet(cx, mult_rhs.span, "_"),
81                                 snippet(cx, rhs.span, "_"),
82                             ),
83                             Applicability::MaybeIncorrect,
84                         );
85                     }
86                 }
87                 //Converts lhs + mult_lhs * mult_rhs to mult_lhs.mult_add(mult_rhs, lhs)
88                 if let Some((mult_lhs, mult_rhs)) = is_float_mult_expr(cx, rhs) {
89                     if is_float(cx, lhs) {
90                         span_lint_and_sugg(
91                             cx,
92                             MANUAL_MUL_ADD,
93                             expr.span,
94                             "consider using mul_add() for better numerical precision",
95                             "try",
96                             format!(
97                                 "{}.mul_add({}, {})",
98                                 snippet(cx, mult_lhs.span, "_"),
99                                 snippet(cx, mult_rhs.span, "_"),
100                                 snippet(cx, lhs.span, "_"),
101                             ),
102                             Applicability::MaybeIncorrect,
103                         );
104                     }
105                 }
106             }
107         }
108     }
109 }