]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/ptr_eq.rs
Auto merge of #85344 - cbeuw:remap-across-cwd, r=michaelwoerister
[rust.git] / src / tools / clippy / clippy_lints / src / ptr_eq.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::in_macro;
3 use clippy_utils::source::snippet_opt;
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
6 use rustc_hir::{BinOpKind, Expr, ExprKind};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9
10 declare_clippy_lint! {
11     /// ### What it does
12     /// Use `std::ptr::eq` when applicable
13     ///
14     /// ### Why is this bad?
15     /// `ptr::eq` can be used to compare `&T` references
16     /// (which coerce to `*const T` implicitly) by their address rather than
17     /// comparing the values they point to.
18     ///
19     /// ### Example
20     /// ```rust
21     /// let a = &[1, 2, 3];
22     /// let b = &[1, 2, 3];
23     ///
24     /// assert!(a as *const _ as usize == b as *const _ as usize);
25     /// ```
26     /// Use instead:
27     /// ```rust
28     /// let a = &[1, 2, 3];
29     /// let b = &[1, 2, 3];
30     ///
31     /// assert!(std::ptr::eq(a, b));
32     /// ```
33     pub PTR_EQ,
34     style,
35     "use `std::ptr::eq` when comparing raw pointers"
36 }
37
38 declare_lint_pass!(PtrEq => [PTR_EQ]);
39
40 static LINT_MSG: &str = "use `std::ptr::eq` when comparing raw pointers";
41
42 impl LateLintPass<'_> for PtrEq {
43     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
44         if in_macro(expr.span) {
45             return;
46         }
47
48         if let ExprKind::Binary(ref op, left, right) = expr.kind {
49             if BinOpKind::Eq == op.node {
50                 let (left, right) = match (expr_as_cast_to_usize(cx, left), expr_as_cast_to_usize(cx, right)) {
51                     (Some(lhs), Some(rhs)) => (lhs, rhs),
52                     _ => (left, right),
53                 };
54
55                 if_chain! {
56                     if let Some(left_var) = expr_as_cast_to_raw_pointer(cx, left);
57                     if let Some(right_var) = expr_as_cast_to_raw_pointer(cx, right);
58                     if let Some(left_snip) = snippet_opt(cx, left_var.span);
59                     if let Some(right_snip) = snippet_opt(cx, right_var.span);
60                     then {
61                         span_lint_and_sugg(
62                             cx,
63                             PTR_EQ,
64                             expr.span,
65                             LINT_MSG,
66                             "try",
67                             format!("std::ptr::eq({}, {})", left_snip, right_snip),
68                             Applicability::MachineApplicable,
69                             );
70                     }
71                 }
72             }
73         }
74     }
75 }
76
77 // If the given expression is a cast to a usize, return the lhs of the cast
78 // E.g., `foo as *const _ as usize` returns `foo as *const _`.
79 fn expr_as_cast_to_usize<'tcx>(cx: &LateContext<'tcx>, cast_expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
80     if cx.typeck_results().expr_ty(cast_expr) == cx.tcx.types.usize {
81         if let ExprKind::Cast(expr, _) = cast_expr.kind {
82             return Some(expr);
83         }
84     }
85     None
86 }
87
88 // If the given expression is a cast to a `*const` pointer, return the lhs of the cast
89 // E.g., `foo as *const _` returns `foo`.
90 fn expr_as_cast_to_raw_pointer<'tcx>(cx: &LateContext<'tcx>, cast_expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
91     if cx.typeck_results().expr_ty(cast_expr).is_unsafe_ptr() {
92         if let ExprKind::Cast(expr, _) = cast_expr.kind {
93             return Some(expr);
94         }
95     }
96     None
97 }