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