|
| 1 | +use crate::utils::{match_qpath, snippet, span_lint_and_sugg}; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_middle::ty; |
| 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 8 | +use rustc_span::source_map::Spanned; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// **What it does:** Checks for statements of the form `(a - b) < f32::EPSILON` or |
| 12 | + /// `(a - b) < f64::EPSILON`. Notes the missing `.abs()`. |
| 13 | + /// |
| 14 | + /// **Why is this bad?** The code without `.abs()` is more likely to have a bug. |
| 15 | + /// |
| 16 | + /// **Known problems:** If the user can ensure that b is larger than a, the `.abs()` is |
| 17 | + /// technically unneccessary. However, it will make the code more robust and doesn't have any |
| 18 | + /// large performance implications. If the abs call was deliberately left out for performance |
| 19 | + /// reasons, it is probably better to state this explicitly in the code, which then can be done |
| 20 | + /// with an allow. |
| 21 | + /// |
| 22 | + /// **Example:** |
| 23 | + /// |
| 24 | + /// ```rust |
| 25 | + /// pub fn is_roughly_equal(a: f32, b: f32) -> bool { |
| 26 | + /// (a - b) < f32::EPSILON |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + /// Use instead: |
| 30 | + /// ```rust |
| 31 | + /// pub fn is_roughly_equal(a: f32, b: f32) -> bool { |
| 32 | + /// (a - b).abs() < f32::EPSILON |
| 33 | + /// } |
| 34 | + /// ``` |
| 35 | + pub FLOAT_EQUALITY_WITHOUT_ABS, |
| 36 | + correctness, |
| 37 | + "float equality check without `.abs()`" |
| 38 | +} |
| 39 | + |
| 40 | +declare_lint_pass!(FloatEqualityWithoutAbs => [FLOAT_EQUALITY_WITHOUT_ABS]); |
| 41 | + |
| 42 | +impl<'tcx> LateLintPass<'tcx> for FloatEqualityWithoutAbs { |
| 43 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 44 | + let lhs; |
| 45 | + let rhs; |
| 46 | + |
| 47 | + // check if expr is a binary expression with a lt or gt operator |
| 48 | + if let ExprKind::Binary(op, ref left, ref right) = expr.kind { |
| 49 | + match op.node { |
| 50 | + BinOpKind::Lt => { |
| 51 | + lhs = left; |
| 52 | + rhs = right; |
| 53 | + }, |
| 54 | + BinOpKind::Gt => { |
| 55 | + lhs = right; |
| 56 | + rhs = left; |
| 57 | + }, |
| 58 | + _ => return, |
| 59 | + }; |
| 60 | + } else { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if_chain! { |
| 65 | + |
| 66 | + // left hand side is a substraction |
| 67 | + if let ExprKind::Binary( |
| 68 | + Spanned { |
| 69 | + node: BinOpKind::Sub, |
| 70 | + .. |
| 71 | + }, |
| 72 | + val_l, |
| 73 | + val_r, |
| 74 | + ) = lhs.kind; |
| 75 | + |
| 76 | + // right hand side matches either f32::EPSILON or f64::EPSILON |
| 77 | + if let ExprKind::Path(ref epsilon_path) = rhs.kind; |
| 78 | + if match_qpath(epsilon_path, &["f32", "EPSILON"]) || match_qpath(epsilon_path, &["f64", "EPSILON"]); |
| 79 | + |
| 80 | + // values of the substractions on the left hand side are of the type float |
| 81 | + let t_val_l = cx.typeck_results().expr_ty(val_l); |
| 82 | + let t_val_r = cx.typeck_results().expr_ty(val_r); |
| 83 | + if let ty::Float(_) = t_val_l.kind; |
| 84 | + if let ty::Float(_) = t_val_r.kind; |
| 85 | + |
| 86 | + then { |
| 87 | + // get the snippet string |
| 88 | + let lhs_string = snippet( |
| 89 | + cx, |
| 90 | + lhs.span, |
| 91 | + "(...)", |
| 92 | + ); |
| 93 | + // format the suggestion |
| 94 | + let suggestion = if lhs_string.starts_with('(') { |
| 95 | + format!("{}.abs()", lhs_string) |
| 96 | + } else { |
| 97 | + format!("({}).abs()", lhs_string) |
| 98 | + }; |
| 99 | + // spans the lint |
| 100 | + span_lint_and_sugg( |
| 101 | + cx, |
| 102 | + FLOAT_EQUALITY_WITHOUT_ABS, |
| 103 | + expr.span, |
| 104 | + "float equality check without `.abs()`", |
| 105 | + "add `.abs()`", |
| 106 | + suggestion, |
| 107 | + Applicability::MaybeIncorrect, |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments