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