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