|
| 1 | +use crate::utils; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc::hir::{BinOpKind, Expr, ExprKind}; |
| 4 | +use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
| 5 | +use rustc::{declare_lint_pass, declare_tool_lint}; |
| 6 | +use rustc_errors::Applicability; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// **What it does:** Use `std::ptr::eq` when applicable |
| 10 | + /// |
| 11 | + /// **Why is this bad?** This can be used to compare `&T` references |
| 12 | + /// (which coerce to `*const T` implicitly) by their address rather than |
| 13 | + /// comparing the values they point to. |
| 14 | + /// |
| 15 | + /// **Known problems:** None |
| 16 | + /// |
| 17 | + /// **Example:** |
| 18 | + /// ```rust |
| 19 | + /// let a = &[1, 2, 3]; |
| 20 | + /// let b = &[1, 2, 3]; |
| 21 | + /// |
| 22 | + /// let _ = a as *const _ as usize == b as *const _ as usize; |
| 23 | + /// // Could be written |
| 24 | + /// let _ = std::ptr::eq(a, b); |
| 25 | + /// ``` |
| 26 | + pub PTR_EQ, |
| 27 | + style, |
| 28 | + "use `std::ptr::eq` when applicable" |
| 29 | +} |
| 30 | + |
| 31 | +declare_lint_pass!(PtrEqLint => [PTR_EQ]); |
| 32 | + |
| 33 | +static LINT_MSG: &str = "use `std::ptr::eq` when applicable"; |
| 34 | + |
| 35 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PtrEqLint { |
| 36 | + fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { |
| 37 | + if let ExprKind::Binary(ref op, ref left, ref right) = expr.kind { |
| 38 | + if BinOpKind::Eq == op.node { |
| 39 | + let (left, right) = if_chain! { |
| 40 | + if let Some(lhs) = expr_as_cast_to_usize(cx, left); |
| 41 | + if let Some(rhs) = expr_as_cast_to_usize(cx, right); |
| 42 | + then { |
| 43 | + (lhs, rhs) |
| 44 | + } |
| 45 | + else { |
| 46 | + (&**left, &**right) |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + if let Some(left_var) = expr_as_cast_to_raw_pointer(cx, left) { |
| 51 | + if let Some(right_var) = expr_as_cast_to_raw_pointer(cx, right) { |
| 52 | + let left_snip = utils::snippet(cx, left_var.span, ".."); |
| 53 | + let right_snip = utils::snippet(cx, right_var.span, ".."); |
| 54 | + let sugg = format!("std::ptr::eq({}, {})", left_snip, right_snip); |
| 55 | + utils::span_lint_and_sugg( |
| 56 | + cx, |
| 57 | + PTR_EQ, |
| 58 | + expr.span, |
| 59 | + LINT_MSG, |
| 60 | + "try", |
| 61 | + sugg, |
| 62 | + Applicability::MachineApplicable, |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// If the given expression is a cast to an usize, return the lhs of the cast |
| 72 | +// E.g., `foo as *const _ as usize` returns `foo as *const _`. |
| 73 | +fn expr_as_cast_to_usize<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cast_expr: &'tcx Expr) -> Option<&'tcx Expr> { |
| 74 | + if cx.tables.expr_ty(cast_expr) == cx.tcx.types.usize { |
| 75 | + if let ExprKind::Cast(ref expr, _) = cast_expr.kind { |
| 76 | + return Some(expr); |
| 77 | + } |
| 78 | + } |
| 79 | + None |
| 80 | +} |
| 81 | + |
| 82 | +// If the given expression is a cast to a `*const` pointer, return the lhs of the cast |
| 83 | +// E.g., `foo as *const _` returns `foo`. |
| 84 | +fn expr_as_cast_to_raw_pointer<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cast_expr: &'tcx Expr) -> Option<&'tcx Expr> { |
| 85 | + if cx.tables.expr_ty(cast_expr).is_unsafe_ptr() { |
| 86 | + if let ExprKind::Cast(ref expr, _) = cast_expr.kind { |
| 87 | + return Some(expr); |
| 88 | + } |
| 89 | + } |
| 90 | + None |
| 91 | +} |
0 commit comments