]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/dereference.rs
separate the receiver from arguments in HIR under /clippy
[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::{expr_sig, is_copy, peel_mid_ty_refs, ty_sig, variant_of_res};
5 use clippy_utils::{
6     fn_def_id, get_parent_expr, get_parent_expr_for_hir, is_lint_allowed, meets_msrv, msrvs, path_to_local,
7     walk_to_expr_usage,
8 };
9 use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
10 use rustc_data_structures::fx::FxIndexMap;
11 use rustc_errors::Applicability;
12 use rustc_hir::intravisit::{walk_ty, Visitor};
13 use rustc_hir::{
14     self as hir, def_id::DefId, BindingAnnotation, Body, BodyId, BorrowKind, Closure, Expr, ExprKind, FnRetTy,
15     GenericArg, HirId, ImplItem, ImplItemKind, Item, ItemKind, Local, MatchSource, Mutability, Node, Pat, PatKind,
16     Path, QPath, TraitItem, TraitItemKind, TyKind, UnOp,
17 };
18 use rustc_index::bit_set::BitSet;
19 use rustc_infer::infer::TyCtxtInferExt;
20 use rustc_lint::{LateContext, LateLintPass};
21 use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
22 use rustc_middle::ty::{
23     self, subst::Subst, Binder, BoundVariableKind, EarlyBinder, FnSig, GenericArgKind, List, ParamTy, PredicateKind,
24     ProjectionPredicate, Ty, TyCtxt, TypeVisitable, TypeckResults,
25 };
26 use rustc_semver::RustcVersion;
27 use rustc_session::{declare_tool_lint, impl_lint_pass};
28 use rustc_span::{symbol::sym, Span, Symbol, DUMMY_SP};
29 use rustc_trait_selection::infer::InferCtxtExt as _;
30 use rustc_trait_selection::traits::{query::evaluate_obligation::InferCtxtExt as _, Obligation, ObligationCause};
31 use std::collections::VecDeque;
32
33 declare_clippy_lint! {
34     /// ### What it does
35     /// Checks for explicit `deref()` or `deref_mut()` method calls.
36     ///
37     /// ### Why is this bad?
38     /// Dereferencing by `&*x` or `&mut *x` is clearer and more concise,
39     /// when not part of a method chain.
40     ///
41     /// ### Example
42     /// ```rust
43     /// use std::ops::Deref;
44     /// let a: &mut String = &mut String::from("foo");
45     /// let b: &str = a.deref();
46     /// ```
47     ///
48     /// Use instead:
49     /// ```rust
50     /// let a: &mut String = &mut String::from("foo");
51     /// let b = &*a;
52     /// ```
53     ///
54     /// This lint excludes:
55     /// ```rust,ignore
56     /// let _ = d.unwrap().deref();
57     /// ```
58     #[clippy::version = "1.44.0"]
59     pub EXPLICIT_DEREF_METHODS,
60     pedantic,
61     "Explicit use of deref or deref_mut method while not in a method chain."
62 }
63
64 declare_clippy_lint! {
65     /// ### What it does
66     /// Checks for address of operations (`&`) that are going to
67     /// be dereferenced immediately by the compiler.
68     ///
69     /// ### Why is this bad?
70     /// Suggests that the receiver of the expression borrows
71     /// the expression.
72     ///
73     /// ### Example
74     /// ```rust
75     /// fn fun(_a: &i32) {}
76     ///
77     /// let x: &i32 = &&&&&&5;
78     /// fun(&x);
79     /// ```
80     ///
81     /// Use instead:
82     /// ```rust
83     /// # fn fun(_a: &i32) {}
84     /// let x: &i32 = &5;
85     /// fun(x);
86     /// ```
87     #[clippy::version = "pre 1.29.0"]
88     pub NEEDLESS_BORROW,
89     style,
90     "taking a reference that is going to be automatically dereferenced"
91 }
92
93 declare_clippy_lint! {
94     /// ### What it does
95     /// Checks for `ref` bindings which create a reference to a reference.
96     ///
97     /// ### Why is this bad?
98     /// The address-of operator at the use site is clearer about the need for a reference.
99     ///
100     /// ### Example
101     /// ```rust
102     /// let x = Some("");
103     /// if let Some(ref x) = x {
104     ///     // use `x` here
105     /// }
106     /// ```
107     ///
108     /// Use instead:
109     /// ```rust
110     /// let x = Some("");
111     /// if let Some(x) = x {
112     ///     // use `&x` here
113     /// }
114     /// ```
115     #[clippy::version = "1.54.0"]
116     pub REF_BINDING_TO_REFERENCE,
117     pedantic,
118     "`ref` binding to a reference"
119 }
120
121 declare_clippy_lint! {
122     /// ### What it does
123     /// Checks for dereferencing expressions which would be covered by auto-deref.
124     ///
125     /// ### Why is this bad?
126     /// This unnecessarily complicates the code.
127     ///
128     /// ### Example
129     /// ```rust
130     /// let x = String::new();
131     /// let y: &str = &*x;
132     /// ```
133     /// Use instead:
134     /// ```rust
135     /// let x = String::new();
136     /// let y: &str = &x;
137     /// ```
138     #[clippy::version = "1.60.0"]
139     pub EXPLICIT_AUTO_DEREF,
140     complexity,
141     "dereferencing when the compiler would automatically dereference"
142 }
143
144 impl_lint_pass!(Dereferencing => [
145     EXPLICIT_DEREF_METHODS,
146     NEEDLESS_BORROW,
147     REF_BINDING_TO_REFERENCE,
148     EXPLICIT_AUTO_DEREF,
149 ]);
150
151 #[derive(Default)]
152 pub struct Dereferencing {
153     state: Option<(State, StateData)>,
154
155     // While parsing a `deref` method call in ufcs form, the path to the function is itself an
156     // expression. This is to store the id of that expression so it can be skipped when
157     // `check_expr` is called for it.
158     skip_expr: Option<HirId>,
159
160     /// The body the first local was found in. Used to emit lints when the traversal of the body has
161     /// been finished. Note we can't lint at the end of every body as they can be nested within each
162     /// other.
163     current_body: Option<BodyId>,
164
165     /// The list of locals currently being checked by the lint.
166     /// If the value is `None`, then the binding has been seen as a ref pattern, but is not linted.
167     /// This is needed for or patterns where one of the branches can be linted, but another can not
168     /// be.
169     ///
170     /// e.g. `m!(x) | Foo::Bar(ref x)`
171     ref_locals: FxIndexMap<HirId, Option<RefPat>>,
172
173     // `IntoIterator` for arrays requires Rust 1.53.
174     msrv: Option<RustcVersion>,
175 }
176
177 impl Dereferencing {
178     #[must_use]
179     pub fn new(msrv: Option<RustcVersion>) -> Self {
180         Self {
181             msrv,
182             ..Dereferencing::default()
183         }
184     }
185 }
186
187 struct StateData {
188     /// Span of the top level expression
189     span: Span,
190     hir_id: HirId,
191     position: Position,
192 }
193
194 struct DerefedBorrow {
195     count: usize,
196     msg: &'static str,
197     snip_expr: Option<HirId>,
198 }
199
200 enum State {
201     // Any number of deref method calls.
202     DerefMethod {
203         // The number of calls in a sequence which changed the referenced type
204         ty_changed_count: usize,
205         is_final_ufcs: bool,
206         /// The required mutability
207         target_mut: Mutability,
208     },
209     DerefedBorrow(DerefedBorrow),
210     ExplicitDeref {
211         mutability: Option<Mutability>,
212     },
213     ExplicitDerefField {
214         name: Symbol,
215     },
216     Reborrow {
217         mutability: Mutability,
218     },
219     Borrow {
220         mutability: Mutability,
221     },
222 }
223
224 // A reference operation considered by this lint pass
225 enum RefOp {
226     Method(Mutability),
227     Deref,
228     AddrOf(Mutability),
229 }
230
231 struct RefPat {
232     /// Whether every usage of the binding is dereferenced.
233     always_deref: bool,
234     /// The spans of all the ref bindings for this local.
235     spans: Vec<Span>,
236     /// The applicability of this suggestion.
237     app: Applicability,
238     /// All the replacements which need to be made.
239     replacements: Vec<(Span, String)>,
240     /// The [`HirId`] that the lint should be emitted at.
241     hir_id: HirId,
242 }
243
244 impl<'tcx> LateLintPass<'tcx> for Dereferencing {
245     #[expect(clippy::too_many_lines)]
246     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
247         // Skip path expressions from deref calls. e.g. `Deref::deref(e)`
248         if Some(expr.hir_id) == self.skip_expr.take() {
249             return;
250         }
251
252         if let Some(local) = path_to_local(expr) {
253             self.check_local_usage(cx, expr, local);
254         }
255
256         // Stop processing sub expressions when a macro call is seen
257         if expr.span.from_expansion() {
258             if let Some((state, data)) = self.state.take() {
259                 report(cx, expr, state, data);
260             }
261             return;
262         }
263
264         let typeck = cx.typeck_results();
265         let (kind, sub_expr) = if let Some(x) = try_parse_ref_op(cx.tcx, typeck, expr) {
266             x
267         } else {
268             // The whole chain of reference operations has been seen
269             if let Some((state, data)) = self.state.take() {
270                 report(cx, expr, state, data);
271             }
272             return;
273         };
274
275         match (self.state.take(), kind) {
276             (None, kind) => {
277                 let expr_ty = typeck.expr_ty(expr);
278                 let (position, adjustments) = walk_parents(cx, expr, self.msrv);
279
280                 match kind {
281                     RefOp::Deref => {
282                         if let Position::FieldAccess(name) = position
283                             && !ty_contains_field(typeck.expr_ty(sub_expr), name)
284                         {
285                             self.state = Some((
286                                 State::ExplicitDerefField { name },
287                                 StateData { span: expr.span, hir_id: expr.hir_id, position },
288                             ));
289                         } else if position.is_deref_stable() {
290                             self.state = Some((
291                                 State::ExplicitDeref { mutability: None },
292                                 StateData { span: expr.span, hir_id: expr.hir_id, position },
293                             ));
294                         }
295                     }
296                     RefOp::Method(target_mut)
297                         if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
298                             && position.lint_explicit_deref() =>
299                     {
300                         self.state = Some((
301                             State::DerefMethod {
302                                 ty_changed_count: if deref_method_same_type(expr_ty, typeck.expr_ty(sub_expr)) {
303                                     0
304                                 } else {
305                                     1
306                                 },
307                                 is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
308                                 target_mut,
309                             },
310                             StateData {
311                                 span: expr.span,
312                                 hir_id: expr.hir_id,
313                                 position
314                             },
315                         ));
316                     },
317                     RefOp::AddrOf(mutability) => {
318                         // Find the number of times the borrow is auto-derefed.
319                         let mut iter = adjustments.iter();
320                         let mut deref_count = 0usize;
321                         let next_adjust = loop {
322                             match iter.next() {
323                                 Some(adjust) => {
324                                     if !matches!(adjust.kind, Adjust::Deref(_)) {
325                                         break Some(adjust);
326                                     } else if !adjust.target.is_ref() {
327                                         deref_count += 1;
328                                         break iter.next();
329                                     }
330                                     deref_count += 1;
331                                 },
332                                 None => break None,
333                             };
334                         };
335
336                         // Determine the required number of references before any can be removed. In all cases the
337                         // reference made by the current expression will be removed. After that there are four cases to
338                         // handle.
339                         //
340                         // 1. Auto-borrow will trigger in the current position, so no further references are required.
341                         // 2. Auto-deref ends at a reference, or the underlying type, so one extra needs to be left to
342                         //    handle the automatically inserted re-borrow.
343                         // 3. Auto-deref hits a user-defined `Deref` impl, so at least one reference needs to exist to
344                         //    start auto-deref.
345                         // 4. If the chain of non-user-defined derefs ends with a mutable re-borrow, and re-borrow
346                         //    adjustments will not be inserted automatically, then leave one further reference to avoid
347                         //    moving a mutable borrow.
348                         //    e.g.
349                         //        fn foo<T>(x: &mut Option<&mut T>, y: &mut T) {
350                         //            let x = match x {
351                         //                // Removing the borrow will cause `x` to be moved
352                         //                Some(x) => &mut *x,
353                         //                None => y
354                         //            };
355                         //        }
356                         let deref_msg =
357                             "this expression creates a reference which is immediately dereferenced by the compiler";
358                         let borrow_msg = "this expression borrows a value the compiler would automatically borrow";
359                         let impl_msg = "the borrowed expression implements the required traits";
360
361                         let (required_refs, msg, snip_expr) = if position.can_auto_borrow() {
362                             (1, if deref_count == 1 { borrow_msg } else { deref_msg }, None)
363                         } else if let Position::ImplArg(hir_id) = position {
364                             (0, impl_msg, Some(hir_id))
365                         } else if let Some(&Adjust::Borrow(AutoBorrow::Ref(_, mutability))) =
366                             next_adjust.map(|a| &a.kind)
367                         {
368                             if matches!(mutability, AutoBorrowMutability::Mut { .. }) && !position.is_reborrow_stable()
369                             {
370                                 (3, deref_msg, None)
371                             } else {
372                                 (2, deref_msg, None)
373                             }
374                         } else {
375                             (2, deref_msg, None)
376                         };
377
378                         if deref_count >= required_refs {
379                             self.state = Some((
380                                 State::DerefedBorrow(DerefedBorrow {
381                                     // One of the required refs is for the current borrow expression, the remaining ones
382                                     // can't be removed without breaking the code. See earlier comment.
383                                     count: deref_count - required_refs,
384                                     msg,
385                                     snip_expr,
386                                 }),
387                                 StateData { span: expr.span, hir_id: expr.hir_id, position },
388                             ));
389                         } else if position.is_deref_stable()
390                             // Auto-deref doesn't combine with other adjustments
391                             && next_adjust.map_or(true, |a| matches!(a.kind, Adjust::Deref(_) | Adjust::Borrow(_)))
392                             && iter.all(|a| matches!(a.kind, Adjust::Deref(_) | Adjust::Borrow(_)))
393                         {
394                             self.state = Some((
395                                 State::Borrow { mutability },
396                                 StateData {
397                                     span: expr.span,
398                                     hir_id: expr.hir_id,
399                                     position
400                                 },
401                             ));
402                         }
403                     },
404                     RefOp::Method(..) => (),
405                 }
406             },
407             (
408                 Some((
409                     State::DerefMethod {
410                         target_mut,
411                         ty_changed_count,
412                         ..
413                     },
414                     data,
415                 )),
416                 RefOp::Method(_),
417             ) => {
418                 self.state = Some((
419                     State::DerefMethod {
420                         ty_changed_count: if deref_method_same_type(typeck.expr_ty(expr), typeck.expr_ty(sub_expr)) {
421                             ty_changed_count
422                         } else {
423                             ty_changed_count + 1
424                         },
425                         is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
426                         target_mut,
427                     },
428                     data,
429                 ));
430             },
431             (Some((State::DerefedBorrow(state), data)), RefOp::AddrOf(_)) if state.count != 0 => {
432                 self.state = Some((
433                     State::DerefedBorrow(DerefedBorrow {
434                         count: state.count - 1,
435                         ..state
436                     }),
437                     data,
438                 ));
439             },
440             (Some((State::DerefedBorrow(state), data)), RefOp::AddrOf(mutability)) => {
441                 let position = data.position;
442                 report(cx, expr, State::DerefedBorrow(state), data);
443                 if position.is_deref_stable() {
444                     self.state = Some((
445                         State::Borrow { mutability },
446                         StateData {
447                             span: expr.span,
448                             hir_id: expr.hir_id,
449                             position,
450                         },
451                     ));
452                 }
453             },
454             (Some((State::DerefedBorrow(state), data)), RefOp::Deref) => {
455                 let position = data.position;
456                 report(cx, expr, State::DerefedBorrow(state), data);
457                 if let Position::FieldAccess(name) = position
458                     && !ty_contains_field(typeck.expr_ty(sub_expr), name)
459                 {
460                     self.state = Some((
461                         State::ExplicitDerefField { name },
462                         StateData { span: expr.span, hir_id: expr.hir_id, position },
463                     ));
464                 } else if position.is_deref_stable() {
465                     self.state = Some((
466                         State::ExplicitDeref { mutability: None },
467                         StateData { span: expr.span, hir_id: expr.hir_id, position },
468                     ));
469                 }
470             },
471
472             (Some((State::Borrow { mutability }, data)), RefOp::Deref) => {
473                 if typeck.expr_ty(sub_expr).is_ref() {
474                     self.state = Some((State::Reborrow { mutability }, data));
475                 } else {
476                     self.state = Some((
477                         State::ExplicitDeref {
478                             mutability: Some(mutability),
479                         },
480                         data,
481                     ));
482                 }
483             },
484             (Some((State::Reborrow { mutability }, data)), RefOp::Deref) => {
485                 self.state = Some((
486                     State::ExplicitDeref {
487                         mutability: Some(mutability),
488                     },
489                     data,
490                 ));
491             },
492             (state @ Some((State::ExplicitDeref { .. }, _)), RefOp::Deref) => {
493                 self.state = state;
494             },
495             (Some((State::ExplicitDerefField { name }, data)), RefOp::Deref)
496                 if !ty_contains_field(typeck.expr_ty(sub_expr), name) =>
497             {
498                 self.state = Some((State::ExplicitDerefField { name }, data));
499             },
500
501             (Some((state, data)), _) => report(cx, expr, state, data),
502         }
503     }
504
505     fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
506         if let PatKind::Binding(BindingAnnotation::Ref, id, name, _) = pat.kind {
507             if let Some(opt_prev_pat) = self.ref_locals.get_mut(&id) {
508                 // This binding id has been seen before. Add this pattern to the list of changes.
509                 if let Some(prev_pat) = opt_prev_pat {
510                     if pat.span.from_expansion() {
511                         // Doesn't match the context of the previous pattern. Can't lint here.
512                         *opt_prev_pat = None;
513                     } else {
514                         prev_pat.spans.push(pat.span);
515                         prev_pat.replacements.push((
516                             pat.span,
517                             snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
518                                 .0
519                                 .into(),
520                         ));
521                     }
522                 }
523                 return;
524             }
525
526             if_chain! {
527                 if !pat.span.from_expansion();
528                 if let ty::Ref(_, tam, _) = *cx.typeck_results().pat_ty(pat).kind();
529                 // only lint immutable refs, because borrowed `&mut T` cannot be moved out
530                 if let ty::Ref(_, _, Mutability::Not) = *tam.kind();
531                 then {
532                     let mut app = Applicability::MachineApplicable;
533                     let snip = snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut app).0;
534                     self.current_body = self.current_body.or(cx.enclosing_body);
535                     self.ref_locals.insert(
536                         id,
537                         Some(RefPat {
538                             always_deref: true,
539                             spans: vec![pat.span],
540                             app,
541                             replacements: vec![(pat.span, snip.into())],
542                             hir_id: pat.hir_id,
543                         }),
544                     );
545                 }
546             }
547         }
548     }
549
550     fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
551         if Some(body.id()) == self.current_body {
552             for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
553                 let replacements = pat.replacements;
554                 let app = pat.app;
555                 let lint = if pat.always_deref {
556                     NEEDLESS_BORROW
557                 } else {
558                     REF_BINDING_TO_REFERENCE
559                 };
560                 span_lint_hir_and_then(
561                     cx,
562                     lint,
563                     pat.hir_id,
564                     pat.spans,
565                     "this pattern creates a reference to a reference",
566                     |diag| {
567                         diag.multipart_suggestion("try this", replacements, app);
568                     },
569                 );
570             }
571             self.current_body = None;
572         }
573     }
574
575     extract_msrv_attr!(LateContext);
576 }
577
578 fn try_parse_ref_op<'tcx>(
579     tcx: TyCtxt<'tcx>,
580     typeck: &'tcx TypeckResults<'_>,
581     expr: &'tcx Expr<'_>,
582 ) -> Option<(RefOp, &'tcx Expr<'tcx>)> {
583     let (def_id, arg) = match expr.kind {
584         ExprKind::MethodCall(_, arg, [], _) => (typeck.type_dependent_def_id(expr.hir_id)?, arg),
585         ExprKind::Call(
586             Expr {
587                 kind: ExprKind::Path(path),
588                 hir_id,
589                 ..
590             },
591             [arg],
592         ) => (typeck.qpath_res(path, *hir_id).opt_def_id()?, arg),
593         ExprKind::Unary(UnOp::Deref, sub_expr) if !typeck.expr_ty(sub_expr).is_unsafe_ptr() => {
594             return Some((RefOp::Deref, sub_expr));
595         },
596         ExprKind::AddrOf(BorrowKind::Ref, mutability, sub_expr) => return Some((RefOp::AddrOf(mutability), sub_expr)),
597         _ => return None,
598     };
599     if tcx.is_diagnostic_item(sym::deref_method, def_id) {
600         Some((RefOp::Method(Mutability::Not), arg))
601     } else if tcx.trait_of_item(def_id)? == tcx.lang_items().deref_mut_trait()? {
602         Some((RefOp::Method(Mutability::Mut), arg))
603     } else {
604         None
605     }
606 }
607
608 // Checks whether the type for a deref call actually changed the type, not just the mutability of
609 // the reference.
610 fn deref_method_same_type<'tcx>(result_ty: Ty<'tcx>, arg_ty: Ty<'tcx>) -> bool {
611     match (result_ty.kind(), arg_ty.kind()) {
612         (ty::Ref(_, result_ty, _), ty::Ref(_, arg_ty, _)) => result_ty == arg_ty,
613
614         // The result type for a deref method is always a reference
615         // Not matching the previous pattern means the argument type is not a reference
616         // This means that the type did change
617         _ => false,
618     }
619 }
620
621 /// The position of an expression relative to it's parent.
622 #[derive(Clone, Copy)]
623 enum Position {
624     MethodReceiver,
625     /// The method is defined on a reference type. e.g. `impl Foo for &T`
626     MethodReceiverRefImpl,
627     Callee,
628     ImplArg(HirId),
629     FieldAccess(Symbol),
630     Postfix,
631     Deref,
632     /// Any other location which will trigger auto-deref to a specific time.
633     /// Contains the precedence of the parent expression and whether the target type is sized.
634     DerefStable(i8, bool),
635     /// Any other location which will trigger auto-reborrowing.
636     /// Contains the precedence of the parent expression.
637     ReborrowStable(i8),
638     /// Contains the precedence of the parent expression.
639     Other(i8),
640 }
641 impl Position {
642     fn is_deref_stable(self) -> bool {
643         matches!(self, Self::DerefStable(..))
644     }
645
646     fn is_reborrow_stable(self) -> bool {
647         matches!(self, Self::DerefStable(..) | Self::ReborrowStable(_))
648     }
649
650     fn can_auto_borrow(self) -> bool {
651         matches!(self, Self::MethodReceiver | Self::FieldAccess(_) | Self::Callee)
652     }
653
654     fn lint_explicit_deref(self) -> bool {
655         matches!(self, Self::Other(_) | Self::DerefStable(..) | Self::ReborrowStable(_))
656     }
657
658     fn precedence(self) -> i8 {
659         match self {
660             Self::MethodReceiver
661             | Self::MethodReceiverRefImpl
662             | Self::Callee
663             | Self::FieldAccess(_)
664             | Self::Postfix => PREC_POSTFIX,
665             Self::ImplArg(_) | Self::Deref => PREC_PREFIX,
666             Self::DerefStable(p, _) | Self::ReborrowStable(p) | Self::Other(p) => p,
667         }
668     }
669 }
670
671 /// Walks up the parent expressions attempting to determine both how stable the auto-deref result
672 /// is, and which adjustments will be applied to it. Note this will not consider auto-borrow
673 /// locations as those follow different rules.
674 #[expect(clippy::too_many_lines)]
675 fn walk_parents<'tcx>(
676     cx: &LateContext<'tcx>,
677     e: &'tcx Expr<'_>,
678     msrv: Option<RustcVersion>,
679 ) -> (Position, &'tcx [Adjustment<'tcx>]) {
680     let mut adjustments = [].as_slice();
681     let mut precedence = 0i8;
682     let ctxt = e.span.ctxt();
683     let position = walk_to_expr_usage(cx, e, &mut |parent, child_id| {
684         // LocalTableInContext returns the wrong lifetime, so go use `expr_adjustments` instead.
685         if adjustments.is_empty() && let Node::Expr(e) = cx.tcx.hir().get(child_id) {
686             adjustments = cx.typeck_results().expr_adjustments(e);
687         }
688         match parent {
689             Node::Local(Local { ty: Some(ty), span, .. }) if span.ctxt() == ctxt => {
690                 Some(binding_ty_auto_deref_stability(cx, ty, precedence, List::empty()))
691             },
692             Node::Item(&Item {
693                 kind: ItemKind::Static(..) | ItemKind::Const(..),
694                 def_id,
695                 span,
696                 ..
697             })
698             | Node::TraitItem(&TraitItem {
699                 kind: TraitItemKind::Const(..),
700                 def_id,
701                 span,
702                 ..
703             })
704             | Node::ImplItem(&ImplItem {
705                 kind: ImplItemKind::Const(..),
706                 def_id,
707                 span,
708                 ..
709             }) if span.ctxt() == ctxt => {
710                 let ty = cx.tcx.type_of(def_id);
711                 Some(ty_auto_deref_stability(cx, ty, precedence).position_for_result(cx))
712             },
713
714             Node::Item(&Item {
715                 kind: ItemKind::Fn(..),
716                 def_id,
717                 span,
718                 ..
719             })
720             | Node::TraitItem(&TraitItem {
721                 kind: TraitItemKind::Fn(..),
722                 def_id,
723                 span,
724                 ..
725             })
726             | Node::ImplItem(&ImplItem {
727                 kind: ImplItemKind::Fn(..),
728                 def_id,
729                 span,
730                 ..
731             }) if span.ctxt() == ctxt => {
732                 let output = cx
733                     .tcx
734                     .erase_late_bound_regions(cx.tcx.fn_sig(def_id.to_def_id()).output());
735                 Some(ty_auto_deref_stability(cx, output, precedence).position_for_result(cx))
736             },
737
738             Node::ExprField(field) if field.span.ctxt() == ctxt => match get_parent_expr_for_hir(cx, field.hir_id) {
739                 Some(Expr {
740                     hir_id,
741                     kind: ExprKind::Struct(path, ..),
742                     ..
743                 }) => variant_of_res(cx, cx.qpath_res(path, *hir_id))
744                     .and_then(|variant| variant.fields.iter().find(|f| f.name == field.ident.name))
745                     .map(|field_def| {
746                         ty_auto_deref_stability(cx, cx.tcx.type_of(field_def.did), precedence).position_for_arg()
747                     }),
748                 _ => None,
749             },
750
751             Node::Expr(parent) if parent.span.ctxt() == ctxt => match parent.kind {
752                 ExprKind::Ret(_) => {
753                     let owner_id = cx.tcx.hir().body_owner(cx.enclosing_body.unwrap());
754                     Some(
755                         if let Node::Expr(
756                             closure_expr @ Expr {
757                                 kind: ExprKind::Closure(closure),
758                                 ..
759                             },
760                         ) = cx.tcx.hir().get(owner_id)
761                         {
762                             closure_result_position(cx, closure, cx.typeck_results().expr_ty(closure_expr), precedence)
763                         } else {
764                             let output = cx
765                                 .tcx
766                                 .erase_late_bound_regions(cx.tcx.fn_sig(cx.tcx.hir().local_def_id(owner_id)).output());
767                             ty_auto_deref_stability(cx, output, precedence).position_for_result(cx)
768                         },
769                     )
770                 },
771                 ExprKind::Closure(closure) => Some(closure_result_position(
772                     cx,
773                     closure,
774                     cx.typeck_results().expr_ty(parent),
775                     precedence,
776                 )),
777                 ExprKind::Call(func, _) if func.hir_id == child_id => {
778                     (child_id == e.hir_id).then_some(Position::Callee)
779                 },
780                 ExprKind::Call(func, args) => args
781                     .iter()
782                     .position(|arg| arg.hir_id == child_id)
783                     .zip(expr_sig(cx, func))
784                     .and_then(|(i, sig)| {
785                         sig.input_with_hir(i).map(|(hir_ty, ty)| match hir_ty {
786                             // Type inference for closures can depend on how they're called. Only go by the explicit
787                             // types here.
788                             Some(hir_ty) => binding_ty_auto_deref_stability(cx, hir_ty, precedence, ty.bound_vars()),
789                             None => {
790                                 if let ty::Param(param_ty) = ty.skip_binder().kind() {
791                                     needless_borrow_impl_arg_position(cx, parent, i, *param_ty, e, precedence, msrv)
792                                 } else {
793                                     ty_auto_deref_stability(cx, cx.tcx.erase_late_bound_regions(ty), precedence)
794                                         .position_for_arg()
795                                 }
796                             },
797                         })
798                     }),
799                 ExprKind::MethodCall(_, receiver, args, _) => {
800                     let id = cx.typeck_results().type_dependent_def_id(parent.hir_id).unwrap();
801                     std::iter::once(receiver)
802                         .chain(args.iter())
803                         .position(|arg| arg.hir_id == child_id)
804                         .map(|i| {
805                             if i == 0 {
806                                 // Check for calls to trait methods where the trait is implemented on a reference.
807                                 // Two cases need to be handled:
808                                 // * `self` methods on `&T` will never have auto-borrow
809                                 // * `&self` methods on `&T` can have auto-borrow, but `&self` methods on `T` will take
810                                 //   priority.
811                                 if e.hir_id != child_id {
812                                 Position::ReborrowStable(precedence)
813                             } else if let Some(trait_id) = cx.tcx.trait_of_item(id)
814                                 && let arg_ty = cx.tcx.erase_regions(cx.typeck_results().expr_ty_adjusted(e))
815                                 && let ty::Ref(_, sub_ty, _) = *arg_ty.kind()
816                                 && let subs = match cx
817                                     .typeck_results()
818                                     .node_substs_opt(parent.hir_id)
819                                     .and_then(|subs| subs.get(1..))
820                                 {
821                                     Some(subs) => cx.tcx.mk_substs(subs.iter().copied()),
822                                     None => cx.tcx.mk_substs(std::iter::empty::<ty::subst::GenericArg<'_>>()),
823                                 } && let impl_ty = if cx.tcx.fn_sig(id).skip_binder().inputs()[0].is_ref() {
824                                     // Trait methods taking `&self`
825                                     sub_ty
826                                 } else {
827                                     // Trait methods taking `self`
828                                     arg_ty
829                                 } && impl_ty.is_ref()
830                                 && cx.tcx.infer_ctxt().enter(|infcx|
831                                     infcx
832                                         .type_implements_trait(trait_id, impl_ty, subs, cx.param_env)
833                                         .must_apply_modulo_regions()
834                                 )
835                             {
836                                 Position::MethodReceiverRefImpl
837                             } else {
838                                 Position::MethodReceiver
839                             }
840                             } else {
841                                 let ty = cx.tcx.fn_sig(id).skip_binder().inputs()[i];
842                                 if let ty::Param(param_ty) = ty.kind() {
843                                     needless_borrow_impl_arg_position(cx, parent, i, *param_ty, e, precedence, msrv)
844                                 } else {
845                                     ty_auto_deref_stability(
846                                         cx,
847                                         cx.tcx.erase_late_bound_regions(cx.tcx.fn_sig(id).input(i)),
848                                         precedence,
849                                     )
850                                     .position_for_arg()
851                                 }
852                             }
853                         })
854                 },
855                 ExprKind::Field(child, name) if child.hir_id == e.hir_id => Some(Position::FieldAccess(name.name)),
856                 ExprKind::Unary(UnOp::Deref, child) if child.hir_id == e.hir_id => Some(Position::Deref),
857                 ExprKind::Match(child, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
858                 | ExprKind::Index(child, _)
859                     if child.hir_id == e.hir_id =>
860                 {
861                     Some(Position::Postfix)
862                 },
863                 _ if child_id == e.hir_id => {
864                     precedence = parent.precedence().order();
865                     None
866                 },
867                 _ => None,
868             },
869             _ => None,
870         }
871     })
872     .unwrap_or(Position::Other(precedence));
873     (position, adjustments)
874 }
875
876 fn closure_result_position<'tcx>(
877     cx: &LateContext<'tcx>,
878     closure: &'tcx Closure<'_>,
879     ty: Ty<'tcx>,
880     precedence: i8,
881 ) -> Position {
882     match closure.fn_decl.output {
883         FnRetTy::Return(hir_ty) => {
884             if let Some(sig) = ty_sig(cx, ty)
885                 && let Some(output) = sig.output()
886             {
887                 binding_ty_auto_deref_stability(cx, hir_ty, precedence, output.bound_vars())
888             } else {
889                 Position::Other(precedence)
890             }
891         },
892         FnRetTy::DefaultReturn(_) => Position::Other(precedence),
893     }
894 }
895
896 // Checks the stability of auto-deref when assigned to a binding with the given explicit type.
897 //
898 // e.g.
899 // let x = Box::new(Box::new(0u32));
900 // let y1: &Box<_> = x.deref();
901 // let y2: &Box<_> = &x;
902 //
903 // Here `y1` and `y2` would resolve to different types, so the type `&Box<_>` is not stable when
904 // switching to auto-dereferencing.
905 fn binding_ty_auto_deref_stability<'tcx>(
906     cx: &LateContext<'tcx>,
907     ty: &'tcx hir::Ty<'_>,
908     precedence: i8,
909     binder_args: &'tcx List<BoundVariableKind>,
910 ) -> Position {
911     let TyKind::Rptr(_, ty) = &ty.kind else {
912         return Position::Other(precedence);
913     };
914     let mut ty = ty;
915
916     loop {
917         break match ty.ty.kind {
918             TyKind::Rptr(_, ref ref_ty) => {
919                 ty = ref_ty;
920                 continue;
921             },
922             TyKind::Path(
923                 QPath::TypeRelative(_, path)
924                 | QPath::Resolved(
925                     _,
926                     Path {
927                         segments: [.., path], ..
928                     },
929                 ),
930             ) => {
931                 if let Some(args) = path.args
932                     && args.args.iter().any(|arg| match arg {
933                         GenericArg::Infer(_) => true,
934                         GenericArg::Type(ty) => ty_contains_infer(ty),
935                         _ => false,
936                     })
937                 {
938                     Position::ReborrowStable(precedence)
939                 } else {
940                     Position::DerefStable(
941                         precedence,
942                         cx.tcx
943                             .erase_late_bound_regions(Binder::bind_with_vars(
944                                 cx.typeck_results().node_type(ty.ty.hir_id),
945                                 binder_args,
946                             ))
947                             .is_sized(cx.tcx.at(DUMMY_SP), cx.param_env.without_caller_bounds()),
948                     )
949                 }
950             },
951             TyKind::Slice(_) => Position::DerefStable(precedence, false),
952             TyKind::Array(..) | TyKind::Ptr(_) | TyKind::BareFn(_) => Position::DerefStable(precedence, true),
953             TyKind::Never
954             | TyKind::Tup(_)
955             | TyKind::Path(_) => Position::DerefStable(
956                 precedence,
957                 cx.tcx
958                     .erase_late_bound_regions(Binder::bind_with_vars(
959                         cx.typeck_results().node_type(ty.ty.hir_id),
960                         binder_args,
961                     ))
962                     .is_sized(cx.tcx.at(DUMMY_SP), cx.param_env.without_caller_bounds()),
963             ),
964             TyKind::OpaqueDef(..) | TyKind::Infer | TyKind::Typeof(..) | TyKind::TraitObject(..) | TyKind::Err => {
965                 Position::ReborrowStable(precedence)
966             },
967         };
968     }
969 }
970
971 // Checks whether a type is inferred at some point.
972 // e.g. `_`, `Box<_>`, `[_]`
973 fn ty_contains_infer(ty: &hir::Ty<'_>) -> bool {
974     struct V(bool);
975     impl Visitor<'_> for V {
976         fn visit_ty(&mut self, ty: &hir::Ty<'_>) {
977             if self.0
978                 || matches!(
979                     ty.kind,
980                     TyKind::OpaqueDef(..) | TyKind::Infer | TyKind::Typeof(_) | TyKind::Err
981                 )
982             {
983                 self.0 = true;
984             } else {
985                 walk_ty(self, ty);
986             }
987         }
988
989         fn visit_generic_arg(&mut self, arg: &GenericArg<'_>) {
990             if self.0 || matches!(arg, GenericArg::Infer(_)) {
991                 self.0 = true;
992             } else if let GenericArg::Type(ty) = arg {
993                 self.visit_ty(ty);
994             }
995         }
996     }
997     let mut v = V(false);
998     v.visit_ty(ty);
999     v.0
1000 }
1001
1002 // Checks whether:
1003 // * child is an expression of the form `&e` in an argument position requiring an `impl Trait`
1004 // * `e`'s type implements `Trait` and is copyable
1005 // If the conditions are met, returns `Some(Position::ImplArg(..))`; otherwise, returns `None`.
1006 //   The "is copyable" condition is to avoid the case where removing the `&` means `e` would have to
1007 // be moved, but it cannot be.
1008 fn needless_borrow_impl_arg_position<'tcx>(
1009     cx: &LateContext<'tcx>,
1010     parent: &Expr<'tcx>,
1011     arg_index: usize,
1012     param_ty: ParamTy,
1013     mut expr: &Expr<'tcx>,
1014     precedence: i8,
1015     msrv: Option<RustcVersion>,
1016 ) -> Position {
1017     let destruct_trait_def_id = cx.tcx.lang_items().destruct_trait();
1018     let sized_trait_def_id = cx.tcx.lang_items().sized_trait();
1019
1020     let Some(callee_def_id) = fn_def_id(cx, parent) else { return Position::Other(precedence) };
1021     let fn_sig = cx.tcx.fn_sig(callee_def_id).skip_binder();
1022     let substs_with_expr_ty = cx
1023         .typeck_results()
1024         .node_substs(if let ExprKind::Call(callee, _) = parent.kind {
1025             callee.hir_id
1026         } else {
1027             parent.hir_id
1028         });
1029
1030     let predicates = cx.tcx.param_env(callee_def_id).caller_bounds();
1031     let projection_predicates = predicates
1032         .iter()
1033         .filter_map(|predicate| {
1034             if let PredicateKind::Projection(projection_predicate) = predicate.kind().skip_binder() {
1035                 Some(projection_predicate)
1036             } else {
1037                 None
1038             }
1039         })
1040         .collect::<Vec<_>>();
1041
1042     let mut trait_with_ref_mut_self_method = false;
1043
1044     // If no traits were found, or only the `Destruct`, `Sized`, or `Any` traits were found, return.
1045     if predicates
1046         .iter()
1047         .filter_map(|predicate| {
1048             if let PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder()
1049                 && trait_predicate.trait_ref.self_ty() == param_ty.to_ty(cx.tcx)
1050             {
1051                 Some(trait_predicate.trait_ref.def_id)
1052             } else {
1053                 None
1054             }
1055         })
1056         .inspect(|trait_def_id| {
1057             trait_with_ref_mut_self_method |= has_ref_mut_self_method(cx, *trait_def_id);
1058         })
1059         .all(|trait_def_id| {
1060             Some(trait_def_id) == destruct_trait_def_id
1061                 || Some(trait_def_id) == sized_trait_def_id
1062                 || cx.tcx.is_diagnostic_item(sym::Any, trait_def_id)
1063         })
1064     {
1065         return Position::Other(precedence);
1066     }
1067
1068     // `substs_with_referent_ty` can be constructed outside of `check_referent` because the same
1069     // elements are modified each time `check_referent` is called.
1070     let mut substs_with_referent_ty = substs_with_expr_ty.to_vec();
1071
1072     let mut check_referent = |referent| {
1073         let referent_ty = cx.typeck_results().expr_ty(referent);
1074
1075         if !is_copy(cx, referent_ty) {
1076             return false;
1077         }
1078
1079         // https://github.com/rust-lang/rust-clippy/pull/9136#pullrequestreview-1037379321
1080         if trait_with_ref_mut_self_method && !matches!(referent_ty.kind(), ty::Ref(_, _, Mutability::Mut)) {
1081             return false;
1082         }
1083
1084         if !replace_types(
1085             cx,
1086             param_ty,
1087             referent_ty,
1088             fn_sig,
1089             arg_index,
1090             &projection_predicates,
1091             &mut substs_with_referent_ty,
1092         ) {
1093             return false;
1094         }
1095
1096         predicates.iter().all(|predicate| {
1097             if let PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder()
1098                 && cx.tcx.is_diagnostic_item(sym::IntoIterator, trait_predicate.trait_ref.def_id)
1099                 && let ty::Param(param_ty) = trait_predicate.self_ty().kind()
1100                 && let GenericArgKind::Type(ty) = substs_with_referent_ty[param_ty.index as usize].unpack()
1101                 && ty.is_array()
1102                 && !meets_msrv(msrv, msrvs::ARRAY_INTO_ITERATOR)
1103             {
1104                 return false;
1105             }
1106
1107             let predicate = EarlyBinder(predicate).subst(cx.tcx, &substs_with_referent_ty);
1108             let obligation = Obligation::new(ObligationCause::dummy(), cx.param_env, predicate);
1109             cx.tcx
1110                 .infer_ctxt()
1111                 .enter(|infcx| infcx.predicate_must_hold_modulo_regions(&obligation))
1112         })
1113     };
1114
1115     let mut needless_borrow = false;
1116     while let ExprKind::AddrOf(_, _, referent) = expr.kind {
1117         if !check_referent(referent) {
1118             break;
1119         }
1120         expr = referent;
1121         needless_borrow = true;
1122     }
1123
1124     if needless_borrow {
1125         Position::ImplArg(expr.hir_id)
1126     } else {
1127         Position::Other(precedence)
1128     }
1129 }
1130
1131 fn has_ref_mut_self_method(cx: &LateContext<'_>, trait_def_id: DefId) -> bool {
1132     cx.tcx
1133         .associated_items(trait_def_id)
1134         .in_definition_order()
1135         .any(|assoc_item| {
1136             if assoc_item.fn_has_self_parameter {
1137                 let self_ty = cx.tcx.fn_sig(assoc_item.def_id).skip_binder().inputs()[0];
1138                 matches!(self_ty.kind(), ty::Ref(_, _, Mutability::Mut))
1139             } else {
1140                 false
1141             }
1142         })
1143 }
1144
1145 // Iteratively replaces `param_ty` with `new_ty` in `substs`, and similarly for each resulting
1146 // projected type that is a type parameter. Returns `false` if replacing the types would have an
1147 // effect on the function signature beyond substituting `new_ty` for `param_ty`.
1148 // See: https://github.com/rust-lang/rust-clippy/pull/9136#discussion_r927212757
1149 fn replace_types<'tcx>(
1150     cx: &LateContext<'tcx>,
1151     param_ty: ParamTy,
1152     new_ty: Ty<'tcx>,
1153     fn_sig: FnSig<'tcx>,
1154     arg_index: usize,
1155     projection_predicates: &[ProjectionPredicate<'tcx>],
1156     substs: &mut [ty::GenericArg<'tcx>],
1157 ) -> bool {
1158     let mut replaced = BitSet::new_empty(substs.len());
1159
1160     let mut deque = VecDeque::with_capacity(substs.len());
1161     deque.push_back((param_ty, new_ty));
1162
1163     while let Some((param_ty, new_ty)) = deque.pop_front() {
1164         // If `replaced.is_empty()`, then `param_ty` and `new_ty` are those initially passed in.
1165         if !fn_sig
1166             .inputs_and_output
1167             .iter()
1168             .enumerate()
1169             .all(|(i, ty)| (replaced.is_empty() && i == arg_index) || !ty.contains(param_ty.to_ty(cx.tcx)))
1170         {
1171             return false;
1172         }
1173
1174         substs[param_ty.index as usize] = ty::GenericArg::from(new_ty);
1175
1176         // The `replaced.insert(...)` check provides some protection against infinite loops.
1177         if replaced.insert(param_ty.index) {
1178             for projection_predicate in projection_predicates {
1179                 if projection_predicate.projection_ty.self_ty() == param_ty.to_ty(cx.tcx)
1180                     && let ty::Term::Ty(term_ty) = projection_predicate.term
1181                     && let ty::Param(term_param_ty) = term_ty.kind()
1182                 {
1183                     let item_def_id = projection_predicate.projection_ty.item_def_id;
1184                     let assoc_item = cx.tcx.associated_item(item_def_id);
1185                     let projection = cx.tcx
1186                         .mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(new_ty, &[]));
1187
1188                     if let Ok(projected_ty) = cx.tcx.try_normalize_erasing_regions(cx.param_env, projection)
1189                         && substs[term_param_ty.index as usize] != ty::GenericArg::from(projected_ty)
1190                     {
1191                         deque.push_back((*term_param_ty, projected_ty));
1192                     }
1193                 }
1194             }
1195         }
1196     }
1197
1198     true
1199 }
1200
1201 struct TyPosition<'tcx> {
1202     position: Position,
1203     ty: Option<Ty<'tcx>>,
1204 }
1205 impl From<Position> for TyPosition<'_> {
1206     fn from(position: Position) -> Self {
1207         Self { position, ty: None }
1208     }
1209 }
1210 impl<'tcx> TyPosition<'tcx> {
1211     fn new_deref_stable_for_result(precedence: i8, ty: Ty<'tcx>) -> Self {
1212         Self {
1213             position: Position::ReborrowStable(precedence),
1214             ty: Some(ty),
1215         }
1216     }
1217     fn position_for_result(self, cx: &LateContext<'tcx>) -> Position {
1218         match (self.position, self.ty) {
1219             (Position::ReborrowStable(precedence), Some(ty)) => {
1220                 Position::DerefStable(precedence, ty.is_sized(cx.tcx.at(DUMMY_SP), cx.param_env))
1221             },
1222             (position, _) => position,
1223         }
1224     }
1225     fn position_for_arg(self) -> Position {
1226         self.position
1227     }
1228 }
1229
1230 // Checks whether a type is stable when switching to auto dereferencing,
1231 fn ty_auto_deref_stability<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, precedence: i8) -> TyPosition<'tcx> {
1232     let ty::Ref(_, mut ty, _) = *ty.kind() else {
1233         return Position::Other(precedence).into();
1234     };
1235
1236     loop {
1237         break match *ty.kind() {
1238             ty::Ref(_, ref_ty, _) => {
1239                 ty = ref_ty;
1240                 continue;
1241             },
1242             ty::Param(_) => TyPosition::new_deref_stable_for_result(precedence, ty),
1243             ty::Infer(_) | ty::Error(_) | ty::Bound(..) | ty::Opaque(..) | ty::Placeholder(_) | ty::Dynamic(..) => {
1244                 Position::ReborrowStable(precedence).into()
1245             },
1246             ty::Adt(..) if ty.has_placeholders() || ty.has_opaque_types() => {
1247                 Position::ReborrowStable(precedence).into()
1248             },
1249             ty::Adt(_, substs) if substs.has_param_types_or_consts() => {
1250                 TyPosition::new_deref_stable_for_result(precedence, ty)
1251             },
1252             ty::Bool
1253             | ty::Char
1254             | ty::Int(_)
1255             | ty::Uint(_)
1256             | ty::Array(..)
1257             | ty::Float(_)
1258             | ty::RawPtr(..)
1259             | ty::FnPtr(_) => Position::DerefStable(precedence, true).into(),
1260             ty::Str | ty::Slice(..) => Position::DerefStable(precedence, false).into(),
1261             ty::Adt(..)
1262             | ty::Foreign(_)
1263             | ty::FnDef(..)
1264             | ty::Generator(..)
1265             | ty::GeneratorWitness(..)
1266             | ty::Closure(..)
1267             | ty::Never
1268             | ty::Tuple(_)
1269             | ty::Projection(_) => Position::DerefStable(
1270                 precedence,
1271                 ty.is_sized(cx.tcx.at(DUMMY_SP), cx.param_env.without_caller_bounds()),
1272             )
1273             .into(),
1274         };
1275     }
1276 }
1277
1278 fn ty_contains_field(ty: Ty<'_>, name: Symbol) -> bool {
1279     if let ty::Adt(adt, _) = *ty.kind() {
1280         adt.is_struct() && adt.all_fields().any(|f| f.name == name)
1281     } else {
1282         false
1283     }
1284 }
1285
1286 #[expect(clippy::needless_pass_by_value, clippy::too_many_lines)]
1287 fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data: StateData) {
1288     match state {
1289         State::DerefMethod {
1290             ty_changed_count,
1291             is_final_ufcs,
1292             target_mut,
1293         } => {
1294             let mut app = Applicability::MachineApplicable;
1295             let (expr_str, expr_is_macro_call) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
1296             let ty = cx.typeck_results().expr_ty(expr);
1297             let (_, ref_count) = peel_mid_ty_refs(ty);
1298             let deref_str = if ty_changed_count >= ref_count && ref_count != 0 {
1299                 // a deref call changing &T -> &U requires two deref operators the first time
1300                 // this occurs. One to remove the reference, a second to call the deref impl.
1301                 "*".repeat(ty_changed_count + 1)
1302             } else {
1303                 "*".repeat(ty_changed_count)
1304             };
1305             let addr_of_str = if ty_changed_count < ref_count {
1306                 // Check if a reborrow from &mut T -> &T is required.
1307                 if target_mut == Mutability::Not && matches!(ty.kind(), ty::Ref(_, _, Mutability::Mut)) {
1308                     "&*"
1309                 } else {
1310                     ""
1311                 }
1312             } else if target_mut == Mutability::Mut {
1313                 "&mut "
1314             } else {
1315                 "&"
1316             };
1317
1318             let expr_str = if !expr_is_macro_call && is_final_ufcs && expr.precedence().order() < PREC_PREFIX {
1319                 format!("({})", expr_str)
1320             } else {
1321                 expr_str.into_owned()
1322             };
1323
1324             span_lint_and_sugg(
1325                 cx,
1326                 EXPLICIT_DEREF_METHODS,
1327                 data.span,
1328                 match target_mut {
1329                     Mutability::Not => "explicit `deref` method call",
1330                     Mutability::Mut => "explicit `deref_mut` method call",
1331                 },
1332                 "try this",
1333                 format!("{}{}{}", addr_of_str, deref_str, expr_str),
1334                 app,
1335             );
1336         },
1337         State::DerefedBorrow(state) => {
1338             let mut app = Applicability::MachineApplicable;
1339             let snip_expr = state.snip_expr.map_or(expr, |hir_id| cx.tcx.hir().expect_expr(hir_id));
1340             let (snip, snip_is_macro) = snippet_with_context(cx, snip_expr.span, data.span.ctxt(), "..", &mut app);
1341             span_lint_hir_and_then(cx, NEEDLESS_BORROW, data.hir_id, data.span, state.msg, |diag| {
1342                 let calls_field = matches!(expr.kind, ExprKind::Field(..)) && matches!(data.position, Position::Callee);
1343                 let sugg = if !snip_is_macro
1344                     && !has_enclosing_paren(&snip)
1345                     && (expr.precedence().order() < data.position.precedence() || calls_field)
1346                 {
1347                     format!("({})", snip)
1348                 } else {
1349                     snip.into()
1350                 };
1351                 diag.span_suggestion(data.span, "change this to", sugg, app);
1352             });
1353         },
1354         State::ExplicitDeref { mutability } => {
1355             if matches!(
1356                 expr.kind,
1357                 ExprKind::Block(..)
1358                     | ExprKind::ConstBlock(_)
1359                     | ExprKind::If(..)
1360                     | ExprKind::Loop(..)
1361                     | ExprKind::Match(..)
1362             ) && matches!(data.position, Position::DerefStable(_, true))
1363             {
1364                 // Rustc bug: auto deref doesn't work on block expression when targeting sized types.
1365                 return;
1366             }
1367
1368             let (prefix, precedence) = if let Some(mutability) = mutability
1369                 && !cx.typeck_results().expr_ty(expr).is_ref()
1370             {
1371                 let prefix = match mutability {
1372                     Mutability::Not => "&",
1373                     Mutability::Mut => "&mut ",
1374                 };
1375                 (prefix, 0)
1376             } else {
1377                 ("", data.position.precedence())
1378             };
1379             span_lint_hir_and_then(
1380                 cx,
1381                 EXPLICIT_AUTO_DEREF,
1382                 data.hir_id,
1383                 data.span,
1384                 "deref which would be done by auto-deref",
1385                 |diag| {
1386                     let mut app = Applicability::MachineApplicable;
1387                     let (snip, snip_is_macro) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
1388                     let sugg =
1389                         if !snip_is_macro && expr.precedence().order() < precedence && !has_enclosing_paren(&snip) {
1390                             format!("{}({})", prefix, snip)
1391                         } else {
1392                             format!("{}{}", prefix, snip)
1393                         };
1394                     diag.span_suggestion(data.span, "try this", sugg, app);
1395                 },
1396             );
1397         },
1398         State::ExplicitDerefField { .. } => {
1399             if matches!(
1400                 expr.kind,
1401                 ExprKind::Block(..)
1402                     | ExprKind::ConstBlock(_)
1403                     | ExprKind::If(..)
1404                     | ExprKind::Loop(..)
1405                     | ExprKind::Match(..)
1406             ) && matches!(data.position, Position::DerefStable(_, true))
1407             {
1408                 // Rustc bug: auto deref doesn't work on block expression when targeting sized types.
1409                 return;
1410             }
1411
1412             span_lint_hir_and_then(
1413                 cx,
1414                 EXPLICIT_AUTO_DEREF,
1415                 data.hir_id,
1416                 data.span,
1417                 "deref which would be done by auto-deref",
1418                 |diag| {
1419                     let mut app = Applicability::MachineApplicable;
1420                     let snip = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app).0;
1421                     diag.span_suggestion(data.span, "try this", snip.into_owned(), app);
1422                 },
1423             );
1424         },
1425         State::Borrow { .. } | State::Reborrow { .. } => (),
1426     }
1427 }
1428
1429 impl Dereferencing {
1430     fn check_local_usage<'tcx>(&mut self, cx: &LateContext<'tcx>, e: &Expr<'tcx>, local: HirId) {
1431         if let Some(outer_pat) = self.ref_locals.get_mut(&local) {
1432             if let Some(pat) = outer_pat {
1433                 // Check for auto-deref
1434                 if !matches!(
1435                     cx.typeck_results().expr_adjustments(e),
1436                     [
1437                         Adjustment {
1438                             kind: Adjust::Deref(_),
1439                             ..
1440                         },
1441                         Adjustment {
1442                             kind: Adjust::Deref(_),
1443                             ..
1444                         },
1445                         ..
1446                     ]
1447                 ) {
1448                     match get_parent_expr(cx, e) {
1449                         // Field accesses are the same no matter the number of references.
1450                         Some(Expr {
1451                             kind: ExprKind::Field(..),
1452                             ..
1453                         }) => (),
1454                         Some(&Expr {
1455                             span,
1456                             kind: ExprKind::Unary(UnOp::Deref, _),
1457                             ..
1458                         }) if !span.from_expansion() => {
1459                             // Remove explicit deref.
1460                             let snip = snippet_with_context(cx, e.span, span.ctxt(), "..", &mut pat.app).0;
1461                             pat.replacements.push((span, snip.into()));
1462                         },
1463                         Some(parent) if !parent.span.from_expansion() => {
1464                             // Double reference might be needed at this point.
1465                             if parent.precedence().order() == PREC_POSTFIX {
1466                                 // Parentheses would be needed here, don't lint.
1467                                 *outer_pat = None;
1468                             } else {
1469                                 pat.always_deref = false;
1470                                 let snip = snippet_with_context(cx, e.span, parent.span.ctxt(), "..", &mut pat.app).0;
1471                                 pat.replacements.push((e.span, format!("&{}", snip)));
1472                             }
1473                         },
1474                         _ if !e.span.from_expansion() => {
1475                             // Double reference might be needed at this point.
1476                             pat.always_deref = false;
1477                             let snip = snippet_with_applicability(cx, e.span, "..", &mut pat.app);
1478                             pat.replacements.push((e.span, format!("&{}", snip)));
1479                         },
1480                         // Edge case for macros. The span of the identifier will usually match the context of the
1481                         // binding, but not if the identifier was created in a macro. e.g. `concat_idents` and proc
1482                         // macros
1483                         _ => *outer_pat = None,
1484                     }
1485                 }
1486             }
1487         }
1488     }
1489 }