]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/needless_borrow.rs
Auto merge of #91284 - t6:freebsd-riscv64, r=Amanieu
[rust.git] / src / tools / clippy / clippy_lints / src / needless_borrow.rs
1 //! Checks for needless address of operations (`&`)
2 //!
3 //! This lint is **warn** by default
4
5 use clippy_utils::diagnostics::span_lint_and_then;
6 use clippy_utils::source::{snippet_opt, snippet_with_applicability, snippet_with_context};
7 use clippy_utils::{get_parent_expr, in_macro, path_to_local};
8 use if_chain::if_chain;
9 use rustc_ast::util::parser::PREC_POSTFIX;
10 use rustc_data_structures::fx::FxIndexMap;
11 use rustc_errors::Applicability;
12 use rustc_hir::{BindingAnnotation, Body, BodyId, BorrowKind, Expr, ExprKind, HirId, Mutability, Pat, PatKind, UnOp};
13 use rustc_lint::{LateContext, LateLintPass};
14 use rustc_middle::ty;
15 use rustc_middle::ty::adjustment::{Adjust, Adjustment};
16 use rustc_session::{declare_tool_lint, impl_lint_pass};
17 use rustc_span::Span;
18
19 declare_clippy_lint! {
20     /// ### What it does
21     /// Checks for address of operations (`&`) that are going to
22     /// be dereferenced immediately by the compiler.
23     ///
24     /// ### Why is this bad?
25     /// Suggests that the receiver of the expression borrows
26     /// the expression.
27     ///
28     /// ### Example
29     /// ```rust
30     /// fn fun(_a: &i32) {}
31     ///
32     /// // Bad
33     /// let x: &i32 = &&&&&&5;
34     /// fun(&x);
35     ///
36     /// // Good
37     /// let x: &i32 = &5;
38     /// fun(x);
39     /// ```
40     pub NEEDLESS_BORROW,
41     style,
42     "taking a reference that is going to be automatically dereferenced"
43 }
44
45 declare_clippy_lint! {
46     /// ### What it does
47     /// Checks for `ref` bindings which create a reference to a reference.
48     ///
49     /// ### Why is this bad?
50     /// The address-of operator at the use site is clearer about the need for a reference.
51     ///
52     /// ### Example
53     /// ```rust
54     /// // Bad
55     /// let x = Some("");
56     /// if let Some(ref x) = x {
57     ///     // use `x` here
58     /// }
59     ///
60     /// // Good
61     /// let x = Some("");
62     /// if let Some(x) = x {
63     ///     // use `&x` here
64     /// }
65     /// ```
66     pub REF_BINDING_TO_REFERENCE,
67     pedantic,
68     "`ref` binding to a reference"
69 }
70
71 impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW, REF_BINDING_TO_REFERENCE]);
72 #[derive(Default)]
73 pub struct NeedlessBorrow {
74     /// The body the first local was found in. Used to emit lints when the traversal of the body has
75     /// been finished. Note we can't lint at the end of every body as they can be nested within each
76     /// other.
77     current_body: Option<BodyId>,
78     /// The list of locals currently being checked by the lint.
79     /// If the value is `None`, then the binding has been seen as a ref pattern, but is not linted.
80     /// This is needed for or patterns where one of the branches can be linted, but another can not
81     /// be.
82     ///
83     /// e.g. `m!(x) | Foo::Bar(ref x)`
84     ref_locals: FxIndexMap<HirId, Option<RefPat>>,
85 }
86
87 struct RefPat {
88     /// Whether every usage of the binding is dereferenced.
89     always_deref: bool,
90     /// The spans of all the ref bindings for this local.
91     spans: Vec<Span>,
92     /// The applicability of this suggestion.
93     app: Applicability,
94     /// All the replacements which need to be made.
95     replacements: Vec<(Span, String)>,
96 }
97
98 impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
99     fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
100         if let Some(local) = path_to_local(e) {
101             self.check_local_usage(cx, e, local);
102         }
103
104         if e.span.from_expansion() {
105             return;
106         }
107         if let ExprKind::AddrOf(BorrowKind::Ref, mutability, inner) = e.kind {
108             if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(inner).kind() {
109                 for adj3 in cx.typeck_results().expr_adjustments(e).windows(3) {
110                     if let [
111                         Adjustment {
112                             kind: Adjust::Deref(_), ..
113                         },
114                         Adjustment {
115                             kind: Adjust::Deref(_), ..
116                         },
117                         Adjustment {
118                             kind: Adjust::Borrow(_),
119                             ..
120                         },
121                     ] = *adj3
122                     {
123                         let help_msg_ty = if matches!(mutability, Mutability::Not) {
124                             format!("&{}", ty)
125                         } else {
126                             format!("&mut {}", ty)
127                         };
128
129                         span_lint_and_then(
130                             cx,
131                             NEEDLESS_BORROW,
132                             e.span,
133                             &format!(
134                                 "this expression borrows a reference (`{}`) that is immediately dereferenced \
135                              by the compiler",
136                                 help_msg_ty
137                             ),
138                             |diag| {
139                                 if let Some(snippet) = snippet_opt(cx, inner.span) {
140                                     diag.span_suggestion(
141                                         e.span,
142                                         "change this to",
143                                         snippet,
144                                         Applicability::MachineApplicable,
145                                     );
146                                 }
147                             },
148                         );
149                     }
150                 }
151             }
152         }
153     }
154
155     fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
156         if let PatKind::Binding(BindingAnnotation::Ref, id, name, _) = pat.kind {
157             if let Some(opt_prev_pat) = self.ref_locals.get_mut(&id) {
158                 // This binding id has been seen before. Add this pattern to the list of changes.
159                 if let Some(prev_pat) = opt_prev_pat {
160                     if in_macro(pat.span) {
161                         // Doesn't match the context of the previous pattern. Can't lint here.
162                         *opt_prev_pat = None;
163                     } else {
164                         prev_pat.spans.push(pat.span);
165                         prev_pat.replacements.push((
166                             pat.span,
167                             snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
168                                 .0
169                                 .into(),
170                         ));
171                     }
172                 }
173                 return;
174             }
175
176             if_chain! {
177                 if !in_macro(pat.span);
178                 if let ty::Ref(_, tam, _) = *cx.typeck_results().pat_ty(pat).kind();
179                 // only lint immutable refs, because borrowed `&mut T` cannot be moved out
180                 if let ty::Ref(_, _, Mutability::Not) = *tam.kind();
181                 then {
182                     let mut app = Applicability::MachineApplicable;
183                     let snip = snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut app).0;
184                     self.current_body = self.current_body.or(cx.enclosing_body);
185                     self.ref_locals.insert(
186                         id,
187                         Some(RefPat {
188                             always_deref: true,
189                             spans: vec![pat.span],
190                             app,
191                             replacements: vec![(pat.span, snip.into())],
192                         }),
193                     );
194                 }
195             }
196         }
197     }
198
199     fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
200         if Some(body.id()) == self.current_body {
201             for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
202                 let replacements = pat.replacements;
203                 let app = pat.app;
204                 span_lint_and_then(
205                     cx,
206                     if pat.always_deref {
207                         NEEDLESS_BORROW
208                     } else {
209                         REF_BINDING_TO_REFERENCE
210                     },
211                     pat.spans,
212                     "this pattern creates a reference to a reference",
213                     |diag| {
214                         diag.multipart_suggestion("try this", replacements, app);
215                     },
216                 );
217             }
218             self.current_body = None;
219         }
220     }
221 }
222 impl NeedlessBorrow {
223     fn check_local_usage(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, local: HirId) {
224         if let Some(outer_pat) = self.ref_locals.get_mut(&local) {
225             if let Some(pat) = outer_pat {
226                 // Check for auto-deref
227                 if !matches!(
228                     cx.typeck_results().expr_adjustments(e),
229                     [
230                         Adjustment {
231                             kind: Adjust::Deref(_),
232                             ..
233                         },
234                         Adjustment {
235                             kind: Adjust::Deref(_),
236                             ..
237                         },
238                         ..
239                     ]
240                 ) {
241                     match get_parent_expr(cx, e) {
242                         // Field accesses are the same no matter the number of references.
243                         Some(Expr {
244                             kind: ExprKind::Field(..),
245                             ..
246                         }) => (),
247                         Some(&Expr {
248                             span,
249                             kind: ExprKind::Unary(UnOp::Deref, _),
250                             ..
251                         }) if !in_macro(span) => {
252                             // Remove explicit deref.
253                             let snip = snippet_with_context(cx, e.span, span.ctxt(), "..", &mut pat.app).0;
254                             pat.replacements.push((span, snip.into()));
255                         },
256                         Some(parent) if !in_macro(parent.span) => {
257                             // Double reference might be needed at this point.
258                             if parent.precedence().order() == PREC_POSTFIX {
259                                 // Parentheses would be needed here, don't lint.
260                                 *outer_pat = None;
261                             } else {
262                                 pat.always_deref = false;
263                                 let snip = snippet_with_context(cx, e.span, parent.span.ctxt(), "..", &mut pat.app).0;
264                                 pat.replacements.push((e.span, format!("&{}", snip)));
265                             }
266                         },
267                         _ if !in_macro(e.span) => {
268                             // Double reference might be needed at this point.
269                             pat.always_deref = false;
270                             let snip = snippet_with_applicability(cx, e.span, "..", &mut pat.app);
271                             pat.replacements.push((e.span, format!("&{}", snip)));
272                         },
273                         // Edge case for macros. The span of the identifier will usually match the context of the
274                         // binding, but not if the identifier was created in a macro. e.g. `concat_idents` and proc
275                         // macros
276                         _ => *outer_pat = None,
277                     }
278                 }
279             }
280         }
281     }
282 }