]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/borrow_deref_ref.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / borrow_deref_ref.rs
1 use crate::reference::DEREF_ADDROF;
2 use clippy_utils::diagnostics::span_lint_and_then;
3 use clippy_utils::source::snippet_opt;
4 use clippy_utils::ty::implements_trait;
5 use clippy_utils::{get_parent_expr, is_lint_allowed};
6 use rustc_errors::Applicability;
7 use rustc_hir::{ExprKind, UnOp};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_middle::mir::Mutability;
10 use rustc_middle::ty;
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
12
13 declare_clippy_lint! {
14 /// ### What it does
15 /// Checks for `&*(&T)`.
16 ///
17 /// ### Why is this bad?
18 /// Dereferencing and then borrowing a reference value has no effect in most cases.
19 ///
20 /// ### Known problems
21 /// False negative on such code:
22 /// ```
23 /// let x = &12;
24 /// let addr_x = &x as *const _ as usize;
25 /// let addr_y = &&*x as *const _ as usize; // assert ok now, and lint triggered.
26 /// // But if we fix it, assert will fail.
27 /// assert_ne!(addr_x, addr_y);
28 /// ```
29 ///
30 /// ### Example
31 /// ```rust
32 /// fn foo(_x: &str) {}
33 ///
34 /// let s = &String::new();
35 ///
36 /// let a: &String = &* s;
37 /// foo(&*s);
38 /// ```
39 ///
40 /// Use instead:
41 /// ```rust
42 /// # fn foo(_x: &str) {}
43 /// # let s = &String::new();
44 /// let a: &String = s;
45 /// foo(&**s);
46 /// ```
47 #[clippy::version = "1.59.0"]
48 pub BORROW_DEREF_REF,
49 complexity,
50 "deref on an immutable reference returns the same type as itself"
51 }
52
53 declare_lint_pass!(BorrowDerefRef => [BORROW_DEREF_REF]);
54
55 impl LateLintPass<'_> for BorrowDerefRef {
56 fn check_expr(&mut self, cx: &LateContext<'_>, e: &rustc_hir::Expr<'_>) {
57 if_chain! {
58 if !e.span.from_expansion();
59 if let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind;
60 if !addrof_target.span.from_expansion();
61 if let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind;
62 if !deref_target.span.from_expansion();
63 if !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..) );
64 let ref_ty = cx.typeck_results().expr_ty(deref_target);
65 if let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind();
66 then{
67
68 if let Some(parent_expr) = get_parent_expr(cx, e){
69 if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) &&
70 !is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) {
71 return;
72 }
73
74 // modification to `&mut &*x` is different from `&mut x`
75 if matches!(deref_target.kind, ExprKind::Path(..)
76 | ExprKind::Field(..)
77 | ExprKind::Index(..)
78 | ExprKind::Unary(UnOp::Deref, ..))
79 && matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) {
80 return;
81 }
82 }
83
84 span_lint_and_then(
85 cx,
86 BORROW_DEREF_REF,
87 e.span,
88 "deref on an immutable reference",
89 |diag| {
90 diag.span_suggestion(
91 e.span,
92 "if you would like to reborrow, try removing `&*`",
93 snippet_opt(cx, deref_target.span).unwrap(),
94 Applicability::MachineApplicable
95 );
96
97 // has deref trait -> give 2 help
98 // doesn't have deref trait -> give 1 help
99 if let Some(deref_trait_id) = cx.tcx.lang_items().deref_trait(){
100 if !implements_trait(cx, *inner_ty, deref_trait_id, &[]) {
101 return;
102 }
103 }
104
105 diag.span_suggestion(
106 e.span,
107 "if you would like to deref, try using `&**`",
108 format!(
109 "&**{}",
110 &snippet_opt(cx, deref_target.span).unwrap(),
111 ),
112 Applicability::MaybeIncorrect
113 );
114
115 }
116 );
117
118 }
119 }
120 }
121 }