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