]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/dereference.rs
Merge commit 'd7b5cbf065b88830ca519adcb73fad4c0d24b1c7' into clippyup
[rust.git] / src / tools / clippy / clippy_lints / src / dereference.rs
1 use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
2 use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
3 use clippy_utils::sugg::has_enclosing_paren;
4 use clippy_utils::ty::peel_mid_ty_refs;
5 use clippy_utils::{get_parent_expr, get_parent_node, is_lint_allowed, path_to_local};
6 use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
7 use rustc_data_structures::fx::FxIndexMap;
8 use rustc_errors::Applicability;
9 use rustc_hir::{
10     BindingAnnotation, Body, BodyId, BorrowKind, Destination, Expr, ExprKind, HirId, MatchSource, Mutability, Node,
11     Pat, PatKind, UnOp,
12 };
13 use rustc_lint::{LateContext, LateLintPass};
14 use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
15 use rustc_middle::ty::{self, Ty, TyCtxt, TypeckResults};
16 use rustc_session::{declare_tool_lint, impl_lint_pass};
17 use rustc_span::{symbol::sym, Span};
18
19 declare_clippy_lint! {
20     /// ### What it does
21     /// Checks for explicit `deref()` or `deref_mut()` method calls.
22     ///
23     /// ### Why is this bad?
24     /// Dereferencing by `&*x` or `&mut *x` is clearer and more concise,
25     /// when not part of a method chain.
26     ///
27     /// ### Example
28     /// ```rust
29     /// use std::ops::Deref;
30     /// let a: &mut String = &mut String::from("foo");
31     /// let b: &str = a.deref();
32     /// ```
33     ///
34     /// Use instead:
35     /// ```rust
36     /// let a: &mut String = &mut String::from("foo");
37     /// let b = &*a;
38     /// ```
39     ///
40     /// This lint excludes:
41     /// ```rust,ignore
42     /// let _ = d.unwrap().deref();
43     /// ```
44     #[clippy::version = "1.44.0"]
45     pub EXPLICIT_DEREF_METHODS,
46     pedantic,
47     "Explicit use of deref or deref_mut method while not in a method chain."
48 }
49
50 declare_clippy_lint! {
51     /// ### What it does
52     /// Checks for address of operations (`&`) that are going to
53     /// be dereferenced immediately by the compiler.
54     ///
55     /// ### Why is this bad?
56     /// Suggests that the receiver of the expression borrows
57     /// the expression.
58     ///
59     /// ### Example
60     /// ```rust
61     /// fn fun(_a: &i32) {}
62     ///
63     /// let x: &i32 = &&&&&&5;
64     /// fun(&x);
65     /// ```
66     ///
67     /// Use instead:
68     /// ```rust
69     /// # fn fun(_a: &i32) {}
70     /// let x: &i32 = &5;
71     /// fun(x);
72     /// ```
73     #[clippy::version = "pre 1.29.0"]
74     pub NEEDLESS_BORROW,
75     style,
76     "taking a reference that is going to be automatically dereferenced"
77 }
78
79 declare_clippy_lint! {
80     /// ### What it does
81     /// Checks for `ref` bindings which create a reference to a reference.
82     ///
83     /// ### Why is this bad?
84     /// The address-of operator at the use site is clearer about the need for a reference.
85     ///
86     /// ### Example
87     /// ```rust
88     /// let x = Some("");
89     /// if let Some(ref x) = x {
90     ///     // use `x` here
91     /// }
92     /// ```
93     ///
94     /// Use instead:
95     /// ```rust
96     /// let x = Some("");
97     /// if let Some(x) = x {
98     ///     // use `&x` here
99     /// }
100     /// ```
101     #[clippy::version = "1.54.0"]
102     pub REF_BINDING_TO_REFERENCE,
103     pedantic,
104     "`ref` binding to a reference"
105 }
106
107 impl_lint_pass!(Dereferencing => [
108     EXPLICIT_DEREF_METHODS,
109     NEEDLESS_BORROW,
110     REF_BINDING_TO_REFERENCE,
111 ]);
112
113 #[derive(Default)]
114 pub struct Dereferencing {
115     state: Option<(State, StateData)>,
116
117     // While parsing a `deref` method call in ufcs form, the path to the function is itself an
118     // expression. This is to store the id of that expression so it can be skipped when
119     // `check_expr` is called for it.
120     skip_expr: Option<HirId>,
121
122     /// The body the first local was found in. Used to emit lints when the traversal of the body has
123     /// been finished. Note we can't lint at the end of every body as they can be nested within each
124     /// other.
125     current_body: Option<BodyId>,
126     /// The list of locals currently being checked by the lint.
127     /// If the value is `None`, then the binding has been seen as a ref pattern, but is not linted.
128     /// This is needed for or patterns where one of the branches can be linted, but another can not
129     /// be.
130     ///
131     /// e.g. `m!(x) | Foo::Bar(ref x)`
132     ref_locals: FxIndexMap<HirId, Option<RefPat>>,
133 }
134
135 struct StateData {
136     /// Span of the top level expression
137     span: Span,
138     hir_id: HirId,
139 }
140
141 enum State {
142     // Any number of deref method calls.
143     DerefMethod {
144         // The number of calls in a sequence which changed the referenced type
145         ty_changed_count: usize,
146         is_final_ufcs: bool,
147         /// The required mutability
148         target_mut: Mutability,
149     },
150     DerefedBorrow {
151         count: usize,
152         required_precedence: i8,
153         msg: &'static str,
154     },
155 }
156
157 // A reference operation considered by this lint pass
158 enum RefOp {
159     Method(Mutability),
160     Deref,
161     AddrOf,
162 }
163
164 struct RefPat {
165     /// Whether every usage of the binding is dereferenced.
166     always_deref: bool,
167     /// The spans of all the ref bindings for this local.
168     spans: Vec<Span>,
169     /// The applicability of this suggestion.
170     app: Applicability,
171     /// All the replacements which need to be made.
172     replacements: Vec<(Span, String)>,
173     /// The [`HirId`] that the lint should be emitted at.
174     hir_id: HirId,
175 }
176
177 impl<'tcx> LateLintPass<'tcx> for Dereferencing {
178     #[expect(clippy::too_many_lines)]
179     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
180         // Skip path expressions from deref calls. e.g. `Deref::deref(e)`
181         if Some(expr.hir_id) == self.skip_expr.take() {
182             return;
183         }
184
185         if let Some(local) = path_to_local(expr) {
186             self.check_local_usage(cx, expr, local);
187         }
188
189         // Stop processing sub expressions when a macro call is seen
190         if expr.span.from_expansion() {
191             if let Some((state, data)) = self.state.take() {
192                 report(cx, expr, state, data);
193             }
194             return;
195         }
196
197         let typeck = cx.typeck_results();
198         let (kind, sub_expr) = if let Some(x) = try_parse_ref_op(cx.tcx, typeck, expr) {
199             x
200         } else {
201             // The whole chain of reference operations has been seen
202             if let Some((state, data)) = self.state.take() {
203                 report(cx, expr, state, data);
204             }
205             return;
206         };
207
208         match (self.state.take(), kind) {
209             (None, kind) => {
210                 let parent = get_parent_node(cx.tcx, expr.hir_id);
211                 let expr_ty = typeck.expr_ty(expr);
212
213                 match kind {
214                     RefOp::Method(target_mut)
215                         if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
216                             && is_linted_explicit_deref_position(parent, expr.hir_id, expr.span) =>
217                     {
218                         self.state = Some((
219                             State::DerefMethod {
220                                 ty_changed_count: if deref_method_same_type(expr_ty, typeck.expr_ty(sub_expr)) {
221                                     0
222                                 } else {
223                                     1
224                                 },
225                                 is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
226                                 target_mut,
227                             },
228                             StateData {
229                                 span: expr.span,
230                                 hir_id: expr.hir_id,
231                             },
232                         ));
233                     },
234                     RefOp::AddrOf => {
235                         // Find the number of times the borrow is auto-derefed.
236                         let mut iter = find_adjustments(cx.tcx, typeck, expr).iter();
237                         let mut deref_count = 0usize;
238                         let next_adjust = loop {
239                             match iter.next() {
240                                 Some(adjust) => {
241                                     if !matches!(adjust.kind, Adjust::Deref(_)) {
242                                         break Some(adjust);
243                                     } else if !adjust.target.is_ref() {
244                                         deref_count += 1;
245                                         break iter.next();
246                                     }
247                                     deref_count += 1;
248                                 },
249                                 None => break None,
250                             };
251                         };
252
253                         // Determine the required number of references before any can be removed. In all cases the
254                         // reference made by the current expression will be removed. After that there are four cases to
255                         // handle.
256                         //
257                         // 1. Auto-borrow will trigger in the current position, so no further references are required.
258                         // 2. Auto-deref ends at a reference, or the underlying type, so one extra needs to be left to
259                         //    handle the automatically inserted re-borrow.
260                         // 3. Auto-deref hits a user-defined `Deref` impl, so at least one reference needs to exist to
261                         //    start auto-deref.
262                         // 4. If the chain of non-user-defined derefs ends with a mutable re-borrow, and re-borrow
263                         //    adjustments will not be inserted automatically, then leave one further reference to avoid
264                         //    moving a mutable borrow.
265                         //    e.g.
266                         //        fn foo<T>(x: &mut Option<&mut T>, y: &mut T) {
267                         //            let x = match x {
268                         //                // Removing the borrow will cause `x` to be moved
269                         //                Some(x) => &mut *x,
270                         //                None => y
271                         //            };
272                         //        }
273                         let deref_msg =
274                             "this expression creates a reference which is immediately dereferenced by the compiler";
275                         let borrow_msg = "this expression borrows a value the compiler would automatically borrow";
276
277                         let (required_refs, required_precedence, msg) = if is_auto_borrow_position(parent, expr.hir_id)
278                         {
279                             (1, PREC_POSTFIX, if deref_count == 1 { borrow_msg } else { deref_msg })
280                         } else if let Some(&Adjust::Borrow(AutoBorrow::Ref(_, mutability))) =
281                             next_adjust.map(|a| &a.kind)
282                         {
283                             if matches!(mutability, AutoBorrowMutability::Mut { .. })
284                                 && !is_auto_reborrow_position(parent)
285                             {
286                                 (3, 0, deref_msg)
287                             } else {
288                                 (2, 0, deref_msg)
289                             }
290                         } else {
291                             (2, 0, deref_msg)
292                         };
293
294                         if deref_count >= required_refs {
295                             self.state = Some((
296                                 State::DerefedBorrow {
297                                     // One of the required refs is for the current borrow expression, the remaining ones
298                                     // can't be removed without breaking the code. See earlier comment.
299                                     count: deref_count - required_refs,
300                                     required_precedence,
301                                     msg,
302                                 },
303                                 StateData {
304                                     span: expr.span,
305                                     hir_id: expr.hir_id,
306                                 },
307                             ));
308                         }
309                     },
310                     _ => (),
311                 }
312             },
313             (
314                 Some((
315                     State::DerefMethod {
316                         target_mut,
317                         ty_changed_count,
318                         ..
319                     },
320                     data,
321                 )),
322                 RefOp::Method(_),
323             ) => {
324                 self.state = Some((
325                     State::DerefMethod {
326                         ty_changed_count: if deref_method_same_type(typeck.expr_ty(expr), typeck.expr_ty(sub_expr)) {
327                             ty_changed_count
328                         } else {
329                             ty_changed_count + 1
330                         },
331                         is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
332                         target_mut,
333                     },
334                     data,
335                 ));
336             },
337             (
338                 Some((
339                     State::DerefedBorrow {
340                         count,
341                         required_precedence,
342                         msg,
343                     },
344                     data,
345                 )),
346                 RefOp::AddrOf,
347             ) if count != 0 => {
348                 self.state = Some((
349                     State::DerefedBorrow {
350                         count: count - 1,
351                         required_precedence,
352                         msg,
353                     },
354                     data,
355                 ));
356             },
357
358             (Some((state, data)), _) => report(cx, expr, state, data),
359         }
360     }
361
362     fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
363         if let PatKind::Binding(BindingAnnotation::Ref, id, name, _) = pat.kind {
364             if let Some(opt_prev_pat) = self.ref_locals.get_mut(&id) {
365                 // This binding id has been seen before. Add this pattern to the list of changes.
366                 if let Some(prev_pat) = opt_prev_pat {
367                     if pat.span.from_expansion() {
368                         // Doesn't match the context of the previous pattern. Can't lint here.
369                         *opt_prev_pat = None;
370                     } else {
371                         prev_pat.spans.push(pat.span);
372                         prev_pat.replacements.push((
373                             pat.span,
374                             snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
375                                 .0
376                                 .into(),
377                         ));
378                     }
379                 }
380                 return;
381             }
382
383             if_chain! {
384                 if !pat.span.from_expansion();
385                 if let ty::Ref(_, tam, _) = *cx.typeck_results().pat_ty(pat).kind();
386                 // only lint immutable refs, because borrowed `&mut T` cannot be moved out
387                 if let ty::Ref(_, _, Mutability::Not) = *tam.kind();
388                 then {
389                     let mut app = Applicability::MachineApplicable;
390                     let snip = snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut app).0;
391                     self.current_body = self.current_body.or(cx.enclosing_body);
392                     self.ref_locals.insert(
393                         id,
394                         Some(RefPat {
395                             always_deref: true,
396                             spans: vec![pat.span],
397                             app,
398                             replacements: vec![(pat.span, snip.into())],
399                             hir_id: pat.hir_id
400                         }),
401                     );
402                 }
403             }
404         }
405     }
406
407     fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
408         if Some(body.id()) == self.current_body {
409             for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
410                 let replacements = pat.replacements;
411                 let app = pat.app;
412                 let lint = if pat.always_deref {
413                     NEEDLESS_BORROW
414                 } else {
415                     REF_BINDING_TO_REFERENCE
416                 };
417                 span_lint_hir_and_then(
418                     cx,
419                     lint,
420                     pat.hir_id,
421                     pat.spans,
422                     "this pattern creates a reference to a reference",
423                     |diag| {
424                         diag.multipart_suggestion("try this", replacements, app);
425                     },
426                 );
427             }
428             self.current_body = None;
429         }
430     }
431 }
432
433 fn try_parse_ref_op<'tcx>(
434     tcx: TyCtxt<'tcx>,
435     typeck: &'tcx TypeckResults<'_>,
436     expr: &'tcx Expr<'_>,
437 ) -> Option<(RefOp, &'tcx Expr<'tcx>)> {
438     let (def_id, arg) = match expr.kind {
439         ExprKind::MethodCall(_, [arg], _) => (typeck.type_dependent_def_id(expr.hir_id)?, arg),
440         ExprKind::Call(
441             Expr {
442                 kind: ExprKind::Path(path),
443                 hir_id,
444                 ..
445             },
446             [arg],
447         ) => (typeck.qpath_res(path, *hir_id).opt_def_id()?, arg),
448         ExprKind::Unary(UnOp::Deref, sub_expr) if !typeck.expr_ty(sub_expr).is_unsafe_ptr() => {
449             return Some((RefOp::Deref, sub_expr));
450         },
451         ExprKind::AddrOf(BorrowKind::Ref, _, sub_expr) => return Some((RefOp::AddrOf, sub_expr)),
452         _ => return None,
453     };
454     if tcx.is_diagnostic_item(sym::deref_method, def_id) {
455         Some((RefOp::Method(Mutability::Not), arg))
456     } else if tcx.trait_of_item(def_id)? == tcx.lang_items().deref_mut_trait()? {
457         Some((RefOp::Method(Mutability::Mut), arg))
458     } else {
459         None
460     }
461 }
462
463 // Checks whether the type for a deref call actually changed the type, not just the mutability of
464 // the reference.
465 fn deref_method_same_type<'tcx>(result_ty: Ty<'tcx>, arg_ty: Ty<'tcx>) -> bool {
466     match (result_ty.kind(), arg_ty.kind()) {
467         (ty::Ref(_, result_ty, _), ty::Ref(_, arg_ty, _)) => result_ty == arg_ty,
468
469         // The result type for a deref method is always a reference
470         // Not matching the previous pattern means the argument type is not a reference
471         // This means that the type did change
472         _ => false,
473     }
474 }
475
476 // Checks whether the parent node is a suitable context for switching from a deref method to the
477 // deref operator.
478 fn is_linted_explicit_deref_position(parent: Option<Node<'_>>, child_id: HirId, child_span: Span) -> bool {
479     let parent = match parent {
480         Some(Node::Expr(e)) if e.span.ctxt() == child_span.ctxt() => e,
481         _ => return true,
482     };
483     match parent.kind {
484         // Leave deref calls in the middle of a method chain.
485         // e.g. x.deref().foo()
486         ExprKind::MethodCall(_, [self_arg, ..], _) if self_arg.hir_id == child_id => false,
487
488         // Leave deref calls resulting in a called function
489         // e.g. (x.deref())()
490         ExprKind::Call(func_expr, _) if func_expr.hir_id == child_id => false,
491
492         // Makes an ugly suggestion
493         // e.g. *x.deref() => *&*x
494         ExprKind::Unary(UnOp::Deref, _)
495         // Postfix expressions would require parens
496         | ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
497         | ExprKind::Field(..)
498         | ExprKind::Index(..)
499         | ExprKind::Err => false,
500
501         ExprKind::Box(..)
502         | ExprKind::ConstBlock(..)
503         | ExprKind::Array(_)
504         | ExprKind::Call(..)
505         | ExprKind::MethodCall(..)
506         | ExprKind::Tup(..)
507         | ExprKind::Binary(..)
508         | ExprKind::Unary(..)
509         | ExprKind::Lit(..)
510         | ExprKind::Cast(..)
511         | ExprKind::Type(..)
512         | ExprKind::DropTemps(..)
513         | ExprKind::If(..)
514         | ExprKind::Loop(..)
515         | ExprKind::Match(..)
516         | ExprKind::Let(..)
517         | ExprKind::Closure{..}
518         | ExprKind::Block(..)
519         | ExprKind::Assign(..)
520         | ExprKind::AssignOp(..)
521         | ExprKind::Path(..)
522         | ExprKind::AddrOf(..)
523         | ExprKind::Break(..)
524         | ExprKind::Continue(..)
525         | ExprKind::Ret(..)
526         | ExprKind::InlineAsm(..)
527         | ExprKind::Struct(..)
528         | ExprKind::Repeat(..)
529         | ExprKind::Yield(..) => true,
530     }
531 }
532
533 /// Checks if the given expression is in a position which can be auto-reborrowed.
534 /// Note: This is only correct assuming auto-deref is already occurring.
535 fn is_auto_reborrow_position(parent: Option<Node<'_>>) -> bool {
536     match parent {
537         Some(Node::Expr(parent)) => matches!(parent.kind, ExprKind::MethodCall(..) | ExprKind::Call(..)),
538         Some(Node::Local(_)) => true,
539         _ => false,
540     }
541 }
542
543 /// Checks if the given expression is a position which can auto-borrow.
544 fn is_auto_borrow_position(parent: Option<Node<'_>>, child_id: HirId) -> bool {
545     if let Some(Node::Expr(parent)) = parent {
546         match parent.kind {
547             // ExprKind::MethodCall(_, [self_arg, ..], _) => self_arg.hir_id == child_id,
548             ExprKind::Field(..) => true,
549             ExprKind::Call(f, _) => f.hir_id == child_id,
550             _ => false,
551         }
552     } else {
553         false
554     }
555 }
556
557 /// Adjustments are sometimes made in the parent block rather than the expression itself.
558 fn find_adjustments<'tcx>(
559     tcx: TyCtxt<'tcx>,
560     typeck: &'tcx TypeckResults<'tcx>,
561     expr: &'tcx Expr<'tcx>,
562 ) -> &'tcx [Adjustment<'tcx>] {
563     let map = tcx.hir();
564     let mut iter = map.parent_iter(expr.hir_id);
565     let mut prev = expr;
566
567     loop {
568         match typeck.expr_adjustments(prev) {
569             [] => (),
570             a => break a,
571         };
572
573         match iter.next().map(|(_, x)| x) {
574             Some(Node::Block(_)) => {
575                 if let Some((_, Node::Expr(e))) = iter.next() {
576                     prev = e;
577                 } else {
578                     // This shouldn't happen. Blocks are always contained in an expression.
579                     break &[];
580                 }
581             },
582             Some(Node::Expr(&Expr {
583                 kind: ExprKind::Break(Destination { target_id: Ok(id), .. }, _),
584                 ..
585             })) => {
586                 if let Some(Node::Expr(e)) = map.find(id) {
587                     prev = e;
588                     iter = map.parent_iter(id);
589                 } else {
590                     // This shouldn't happen. The destination should exist.
591                     break &[];
592                 }
593             },
594             _ => break &[],
595         }
596     }
597 }
598
599 #[expect(clippy::needless_pass_by_value)]
600 fn report<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, state: State, data: StateData) {
601     match state {
602         State::DerefMethod {
603             ty_changed_count,
604             is_final_ufcs,
605             target_mut,
606         } => {
607             let mut app = Applicability::MachineApplicable;
608             let (expr_str, expr_is_macro_call) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
609             let ty = cx.typeck_results().expr_ty(expr);
610             let (_, ref_count) = peel_mid_ty_refs(ty);
611             let deref_str = if ty_changed_count >= ref_count && ref_count != 0 {
612                 // a deref call changing &T -> &U requires two deref operators the first time
613                 // this occurs. One to remove the reference, a second to call the deref impl.
614                 "*".repeat(ty_changed_count + 1)
615             } else {
616                 "*".repeat(ty_changed_count)
617             };
618             let addr_of_str = if ty_changed_count < ref_count {
619                 // Check if a reborrow from &mut T -> &T is required.
620                 if target_mut == Mutability::Not && matches!(ty.kind(), ty::Ref(_, _, Mutability::Mut)) {
621                     "&*"
622                 } else {
623                     ""
624                 }
625             } else if target_mut == Mutability::Mut {
626                 "&mut "
627             } else {
628                 "&"
629             };
630
631             let expr_str = if !expr_is_macro_call && is_final_ufcs && expr.precedence().order() < PREC_PREFIX {
632                 format!("({})", expr_str)
633             } else {
634                 expr_str.into_owned()
635             };
636
637             span_lint_and_sugg(
638                 cx,
639                 EXPLICIT_DEREF_METHODS,
640                 data.span,
641                 match target_mut {
642                     Mutability::Not => "explicit `deref` method call",
643                     Mutability::Mut => "explicit `deref_mut` method call",
644                 },
645                 "try this",
646                 format!("{}{}{}", addr_of_str, deref_str, expr_str),
647                 app,
648             );
649         },
650         State::DerefedBorrow {
651             required_precedence,
652             msg,
653             ..
654         } => {
655             let mut app = Applicability::MachineApplicable;
656             let snip = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app).0;
657             span_lint_hir_and_then(cx, NEEDLESS_BORROW, data.hir_id, data.span, msg, |diag| {
658                 let sugg = if required_precedence > expr.precedence().order() && !has_enclosing_paren(&snip) {
659                     format!("({})", snip)
660                 } else {
661                     snip.into()
662                 };
663                 diag.span_suggestion(data.span, "change this to", sugg, app);
664             });
665         },
666     }
667 }
668
669 impl Dereferencing {
670     fn check_local_usage<'tcx>(&mut self, cx: &LateContext<'tcx>, e: &Expr<'tcx>, local: HirId) {
671         if let Some(outer_pat) = self.ref_locals.get_mut(&local) {
672             if let Some(pat) = outer_pat {
673                 // Check for auto-deref
674                 if !matches!(
675                     cx.typeck_results().expr_adjustments(e),
676                     [
677                         Adjustment {
678                             kind: Adjust::Deref(_),
679                             ..
680                         },
681                         Adjustment {
682                             kind: Adjust::Deref(_),
683                             ..
684                         },
685                         ..
686                     ]
687                 ) {
688                     match get_parent_expr(cx, e) {
689                         // Field accesses are the same no matter the number of references.
690                         Some(Expr {
691                             kind: ExprKind::Field(..),
692                             ..
693                         }) => (),
694                         Some(&Expr {
695                             span,
696                             kind: ExprKind::Unary(UnOp::Deref, _),
697                             ..
698                         }) if !span.from_expansion() => {
699                             // Remove explicit deref.
700                             let snip = snippet_with_context(cx, e.span, span.ctxt(), "..", &mut pat.app).0;
701                             pat.replacements.push((span, snip.into()));
702                         },
703                         Some(parent) if !parent.span.from_expansion() => {
704                             // Double reference might be needed at this point.
705                             if parent.precedence().order() == PREC_POSTFIX {
706                                 // Parentheses would be needed here, don't lint.
707                                 *outer_pat = None;
708                             } else {
709                                 pat.always_deref = false;
710                                 let snip = snippet_with_context(cx, e.span, parent.span.ctxt(), "..", &mut pat.app).0;
711                                 pat.replacements.push((e.span, format!("&{}", snip)));
712                             }
713                         },
714                         _ if !e.span.from_expansion() => {
715                             // Double reference might be needed at this point.
716                             pat.always_deref = false;
717                             let snip = snippet_with_applicability(cx, e.span, "..", &mut pat.app);
718                             pat.replacements.push((e.span, format!("&{}", snip)));
719                         },
720                         // Edge case for macros. The span of the identifier will usually match the context of the
721                         // binding, but not if the identifier was created in a macro. e.g. `concat_idents` and proc
722                         // macros
723                         _ => *outer_pat = None,
724                     }
725                 }
726             }
727         }
728     }
729 }