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};
8 fn_def_id, get_parent_expr, get_parent_expr_for_hir, is_lint_allowed, path_to_local, walk_to_expr_usage,
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};
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,
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,
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;
38 declare_clippy_lint! {
40 /// Checks for explicit `deref()` or `deref_mut()` method calls.
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.
48 /// use std::ops::Deref;
49 /// let a: &mut String = &mut String::from("foo");
50 /// let b: &str = a.deref();
55 /// let a: &mut String = &mut String::from("foo");
59 /// This lint excludes:
61 /// let _ = d.unwrap().deref();
63 #[clippy::version = "1.44.0"]
64 pub EXPLICIT_DEREF_METHODS,
66 "Explicit use of deref or deref_mut method while not in a method chain."
69 declare_clippy_lint! {
71 /// Checks for address of operations (`&`) that are going to
72 /// be dereferenced immediately by the compiler.
74 /// ### Why is this bad?
75 /// Suggests that the receiver of the expression borrows
80 /// fn fun(_a: &i32) {}
82 /// let x: &i32 = &&&&&&5;
88 /// # fn fun(_a: &i32) {}
92 #[clippy::version = "pre 1.29.0"]
95 "taking a reference that is going to be automatically dereferenced"
98 declare_clippy_lint! {
100 /// Checks for `ref` bindings which create a reference to a reference.
102 /// ### Why is this bad?
103 /// The address-of operator at the use site is clearer about the need for a reference.
107 /// let x = Some("");
108 /// if let Some(ref x) = x {
115 /// let x = Some("");
116 /// if let Some(x) = x {
120 #[clippy::version = "1.54.0"]
121 pub REF_BINDING_TO_REFERENCE,
123 "`ref` binding to a reference"
126 declare_clippy_lint! {
128 /// Checks for dereferencing expressions which would be covered by auto-deref.
130 /// ### Why is this bad?
131 /// This unnecessarily complicates the code.
135 /// let x = String::new();
136 /// let y: &str = &*x;
140 /// let x = String::new();
141 /// let y: &str = &x;
143 #[clippy::version = "1.64.0"]
144 pub EXPLICIT_AUTO_DEREF,
146 "dereferencing when the compiler would automatically dereference"
149 impl_lint_pass!(Dereferencing<'_> => [
150 EXPLICIT_DEREF_METHODS,
152 REF_BINDING_TO_REFERENCE,
157 pub struct Dereferencing<'tcx> {
158 state: Option<(State, StateData)>,
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>,
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
168 current_body: Option<BodyId>,
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
175 /// e.g. `m!(x) | Foo::Bar(ref x)`
176 ref_locals: FxIndexMap<HirId, Option<RefPat>>,
178 /// Stack of (body owner, `PossibleBorrowerMap`) pairs. Used by
179 /// `needless_borrow_impl_arg_position` to determine when a borrowed expression can instead
181 possible_borrowers: Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>,
183 // `IntoIterator` for arrays requires Rust 1.53.
187 impl<'tcx> Dereferencing<'tcx> {
189 pub fn new(msrv: Msrv) -> Self {
192 ..Dereferencing::default()
199 /// Span of the top level expression
206 struct DerefedBorrow {
209 snip_expr: Option<HirId>,
214 // Any number of deref method calls.
216 // The number of calls in a sequence which changed the referenced type
217 ty_changed_count: usize,
219 /// The required mutability
220 target_mut: Mutability,
222 DerefedBorrow(DerefedBorrow),
224 mutability: Option<Mutability>,
230 mutability: Mutability,
233 mutability: Mutability,
237 // A reference operation considered by this lint pass
245 /// Whether every usage of the binding is dereferenced.
247 /// The spans of all the ref bindings for this local.
249 /// The applicability of this suggestion.
251 /// All the replacements which need to be made.
252 replacements: Vec<(Span, String)>,
253 /// The [`HirId`] that the lint should be emitted at.
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() {
265 if let Some(local) = path_to_local(expr) {
266 self.check_local_usage(cx, expr, local);
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);
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);
286 match (self.state.take(), kind) {
288 let expr_ty = typeck.expr_ty(expr);
289 let (position, adjustments) = walk_parents(cx, &mut self.possible_borrowers, expr, &self.msrv);
292 let sub_ty = typeck.expr_ty(sub_expr);
293 if let Position::FieldAccess {
297 && !ty_contains_field(sub_ty, name)
300 State::ExplicitDerefField { name },
301 StateData { span: expr.span, hir_id: expr.hir_id, position },
303 } else if position.is_deref_stable() && sub_ty.is_ref() {
305 State::ExplicitDeref { mutability: None },
306 StateData { span: expr.span, hir_id: expr.hir_id, position },
310 RefOp::Method(target_mut)
311 if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
312 && position.lint_explicit_deref() =>
314 let ty_changed_count = usize::from(!deref_method_same_type(expr_ty, typeck.expr_ty(sub_expr)));
318 is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
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 {
335 if !matches!(adjust.kind, Adjust::Deref(_)) {
337 } else if !adjust.target.is_ref() {
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
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
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.
360 // fn foo<T>(x: &mut Option<&mut T>, y: &mut T) {
362 // // Removing the borrow will cause `x` to be moved
363 // Some(x) => &mut *x,
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";
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)
379 if matches!(mutability, AutoBorrowMutability::Mut { .. }) && !position.is_reborrow_stable()
389 if deref_count >= required_refs {
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,
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(_)))
410 State::Borrow { mutability },
419 RefOp::Method(..) => (),
435 ty_changed_count: if deref_method_same_type(typeck.expr_ty(expr), typeck.expr_ty(sub_expr)) {
440 is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
446 (Some((State::DerefedBorrow(state), data)), RefOp::AddrOf(_)) if state.count != 0 => {
448 State::DerefedBorrow(DerefedBorrow {
449 count: state.count - 1,
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() {
460 State::Borrow { mutability },
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)
476 State::ExplicitDerefField { name },
477 StateData { span: expr.span, hir_id: expr.hir_id, position },
479 } else if position.is_deref_stable() {
481 State::ExplicitDeref { mutability: None },
482 StateData { span: expr.span, hir_id: expr.hir_id, position },
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));
492 State::ExplicitDeref {
493 mutability: Some(mutability),
499 (Some((State::Reborrow { mutability }, data)), RefOp::Deref) => {
501 State::ExplicitDeref {
502 mutability: Some(mutability),
507 (state @ Some((State::ExplicitDeref { .. }, _)), RefOp::Deref) => {
510 (Some((State::ExplicitDerefField { name }, data)), RefOp::Deref)
511 if !ty_contains_field(typeck.expr_ty(sub_expr), name) =>
513 self.state = Some((State::ExplicitDerefField { name }, data));
516 (Some((state, data)), _) => report(cx, expr, state, data),
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;
529 prev_pat.spans.push(pat.span);
530 prev_pat.replacements.push((
532 snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
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();
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(
554 spans: vec![pat.span],
556 replacements: vec![(pat.span, snip.into())],
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())
569 self.possible_borrowers.pop();
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;
576 let lint = if pat.always_deref {
579 REF_BINDING_TO_REFERENCE
581 span_lint_hir_and_then(
586 "this pattern creates a reference to a reference",
588 diag.multipart_suggestion("try this", replacements, app);
592 self.current_body = None;
596 extract_msrv_attr!(LateContext);
599 fn try_parse_ref_op<'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),
608 kind: ExprKind::Path(path),
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));
617 ExprKind::AddrOf(BorrowKind::Ref, mutability, sub_expr) => return Some((RefOp::AddrOf(mutability), sub_expr)),
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))
629 // Checks whether the type for a deref call actually changed the type, not just the mutability of
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,
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
642 /// The position of an expression relative to it's parent.
643 #[derive(Clone, Copy, Debug)]
646 /// The method is defined on a reference type. e.g. `impl Foo for &T`
647 MethodReceiverRefImpl,
653 }, // union fields cannot be auto borrowed
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.
662 /// Contains the precedence of the parent expression.
666 fn is_deref_stable(self) -> bool {
667 matches!(self, Self::DerefStable(..))
670 fn is_reborrow_stable(self) -> bool {
671 matches!(self, Self::DerefStable(..) | Self::ReborrowStable(_))
674 fn can_auto_borrow(self) -> bool {
677 Self::MethodReceiver | Self::FieldAccess { of_union: false, .. } | Self::Callee
681 fn lint_explicit_deref(self) -> bool {
682 matches!(self, Self::Other(_) | Self::DerefStable(..) | Self::ReborrowStable(_))
685 fn precedence(self) -> i8 {
688 | Self::MethodReceiverRefImpl
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,
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>)>,
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);
717 Node::Local(Local { ty: Some(ty), span, .. }) if span.ctxt() == ctxt => {
718 Some(binding_ty_auto_deref_stability(cx, ty, precedence, List::empty()))
721 kind: ItemKind::Static(..) | ItemKind::Const(..),
726 | Node::TraitItem(&TraitItem {
727 kind: TraitItemKind::Const(..),
732 | Node::ImplItem(&ImplItem {
733 kind: ImplItemKind::Const(..),
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))
743 kind: ItemKind::Fn(..),
748 | Node::TraitItem(&TraitItem {
749 kind: TraitItemKind::Fn(..),
754 | Node::ImplItem(&ImplItem {
755 kind: ImplItemKind::Fn(..),
759 }) if span.ctxt() == ctxt => {
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))
766 Node::ExprField(field) if field.span.ctxt() == ctxt => match get_parent_expr_for_hir(cx, field.hir_id) {
769 kind: ExprKind::Struct(path, ..),
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))
774 ty_auto_deref_stability(cx, cx.tcx.type_of(field_def.did), precedence).position_for_arg()
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());
784 closure_expr @ Expr {
785 kind: ExprKind::Closure(closure),
788 ) = cx.tcx.hir().get(owner_id)
790 closure_result_position(cx, closure, cx.typeck_results().expr_ty(closure_expr), precedence)
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)
799 ExprKind::Closure(closure) => Some(closure_result_position(
802 cx.typeck_results().expr_ty(parent),
805 ExprKind::Call(func, _) if func.hir_id == child_id => {
806 (child_id == e.hir_id).then_some(Position::Callee)
808 ExprKind::Call(func, args) => args
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)| {
815 // Type inference for closures can depend on how they're called. Only go by the explicit
818 binding_ty_auto_deref_stability(cx, hir_ty, precedence, ty.bound_vars())
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()
827 needless_borrow_impl_arg_position(
838 ty_auto_deref_stability(cx, cx.tcx.erase_late_bound_regions(ty), precedence)
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
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()
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`
865 // Trait methods taking `self`
867 } && impl_ty.is_ref()
868 && let infcx = cx.tcx.infer_ctxt().build()
870 .type_implements_trait(
872 [impl_ty.into()].into_iter().chain(subs.iter().copied()),
875 .must_apply_modulo_regions()
877 return Some(Position::MethodReceiverRefImpl)
879 return Some(Position::MethodReceiver);
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(
897 ty_auto_deref_stability(
899 cx.tcx.erase_late_bound_regions(cx.tcx.fn_sig(id).input(i + 1)),
906 ExprKind::Field(child, name) if child.hir_id == e.hir_id => Some(Position::FieldAccess {
908 of_union: is_union(cx.typeck_results(), child),
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 =>
915 Some(Position::Postfix)
917 _ if child_id == e.hir_id => {
918 precedence = parent.precedence().order();
926 .unwrap_or(Position::Other(precedence));
927 (position, adjustments)
930 fn is_union<'tcx>(typeck: &'tcx TypeckResults<'_>, path_expr: &'tcx Expr<'_>) -> bool {
932 .expr_ty_adjusted(path_expr)
934 .map_or(false, rustc_middle::ty::AdtDef::is_union)
937 fn closure_result_position<'tcx>(
938 cx: &LateContext<'tcx>,
939 closure: &'tcx Closure<'_>,
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()
948 binding_ty_auto_deref_stability(cx, hir_ty, precedence, output.bound_vars())
950 Position::Other(precedence)
953 FnRetTy::DefaultReturn(_) => Position::Other(precedence),
957 // Checks the stability of auto-deref when assigned to a binding with the given explicit type.
960 // let x = Box::new(Box::new(0u32));
961 // let y1: &Box<_> = x.deref();
962 // let y2: &Box<_> = &x;
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<'_>,
970 binder_args: &'tcx List<BoundVariableKind>,
972 let TyKind::Ref(_, ty) = &ty.kind else {
973 return Position::Other(precedence);
978 break match ty.ty.kind {
979 TyKind::Ref(_, ref ref_ty) => {
984 QPath::TypeRelative(_, path)
988 segments: [.., path], ..
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),
999 Position::ReborrowStable(precedence)
1001 Position::DerefStable(
1004 .erase_late_bound_regions(Binder::bind_with_vars(
1005 cx.typeck_results().node_type(ty.ty.hir_id),
1008 .is_sized(cx.tcx, cx.param_env.without_caller_bounds()),
1012 TyKind::Slice(_) => Position::DerefStable(precedence, false),
1013 TyKind::Array(..) | TyKind::Ptr(_) | TyKind::BareFn(_) => Position::DerefStable(precedence, true),
1016 | TyKind::Path(_) => Position::DerefStable(
1019 .erase_late_bound_regions(Binder::bind_with_vars(
1020 cx.typeck_results().node_type(ty.ty.hir_id),
1023 .is_sized(cx.tcx, cx.param_env.without_caller_bounds()),
1025 TyKind::OpaqueDef(..) | TyKind::Infer | TyKind::Typeof(..) | TyKind::TraitObject(..) | TyKind::Err => {
1026 Position::ReborrowStable(precedence)
1032 // Checks whether a type is inferred at some point.
1033 // e.g. `_`, `Box<_>`, `[_]`
1034 fn ty_contains_infer(ty: &hir::Ty<'_>) -> bool {
1036 impl Visitor<'_> for V {
1037 fn visit_ty(&mut self, ty: &hir::Ty<'_>) {
1041 TyKind::OpaqueDef(..) | TyKind::Infer | TyKind::Typeof(_) | TyKind::Err
1050 fn visit_generic_arg(&mut self, arg: &GenericArg<'_>) {
1051 if self.0 || matches!(arg, GenericArg::Infer(_)) {
1053 } else if let GenericArg::Type(ty) = arg {
1058 let mut v = V(false);
1063 fn call_is_qualified(expr: &Expr<'_>) -> bool {
1064 if let ExprKind::Path(path) = &expr.kind {
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,
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>,
1088 mut expr: &Expr<'tcx>,
1092 let destruct_trait_def_id = cx.tcx.lang_items().destruct_trait();
1093 let sized_trait_def_id = cx.tcx.lang_items().sized_trait();
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
1099 .node_substs(if let ExprKind::Call(callee, _) = parent.kind {
1105 let predicates = cx.tcx.param_env(callee_def_id).caller_bounds();
1106 let projection_predicates = predicates
1108 .filter_map(|predicate| {
1109 if let PredicateKind::Clause(Clause::Projection(projection_predicate)) = predicate.kind().skip_binder() {
1110 Some(projection_predicate)
1115 .collect::<Vec<_>>();
1117 let mut trait_with_ref_mut_self_method = false;
1119 // If no traits were found, or only the `Destruct`, `Sized`, or `Any` traits were found, return.
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)
1126 Some(trait_predicate.trait_ref.def_id)
1131 .inspect(|trait_def_id| {
1132 trait_with_ref_mut_self_method |= has_ref_mut_self_method(cx, *trait_def_id);
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)
1140 return Position::Other(precedence);
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
1148 .any(|projection_predicate| is_mixed_projection_predicate(cx, callee_def_id, projection_predicate))
1150 return Position::Other(precedence);
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();
1157 let mut check_reference_and_referent = |reference, referent| {
1158 let referent_ty = cx.typeck_results().expr_ty(referent);
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))
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)) {
1178 &projection_predicates,
1179 &mut substs_with_referent_ty,
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()
1190 && !msrv.meets(msrvs::ARRAY_INTO_ITERATOR)
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)
1202 let mut needless_borrow = false;
1203 while let ExprKind::AddrOf(_, _, referent) = expr.kind {
1204 if !check_reference_and_referent(expr, referent) {
1208 needless_borrow = true;
1211 if needless_borrow {
1212 Position::ImplArg(expr.hir_id)
1214 Position::Other(precedence)
1218 fn has_ref_mut_self_method(cx: &LateContext<'_>, trait_def_id: DefId) -> bool {
1220 .associated_items(trait_def_id)
1221 .in_definition_order()
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))
1232 fn is_mixed_projection_predicate<'tcx>(
1233 cx: &LateContext<'tcx>,
1234 callee_def_id: DefId,
1235 projection_predicate: &ProjectionPredicate<'tcx>,
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
1243 // The inner-most self type is a type parameter from the current function.
1244 let mut projection_ty = projection_predicate.projection_ty;
1246 match projection_ty.self_ty().kind() {
1247 ty::Alias(ty::Projection, inner_projection_ty) => {
1248 projection_ty = *inner_projection_ty;
1250 ty::Param(param_ty) => {
1251 return (param_ty.index as usize) >= generics.parent_count;
1263 fn referent_used_exactly_once<'tcx>(
1264 cx: &LateContext<'tcx>,
1265 possible_borrowers: &mut Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>,
1266 reference: &Expr<'tcx>,
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()
1277 let body_owner_local_def_id = cx.tcx.hir().enclosing_body_owner(reference.hir_id);
1278 if possible_borrowers
1280 .map_or(true, |&(local_def_id, _)| local_def_id != body_owner_local_def_id)
1282 possible_borrowers.push((body_owner_local_def_id, PossibleBorrowerMap::new(cx, mir)));
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)
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>,
1303 fn_sig: FnSig<'tcx>,
1305 projection_predicates: &[ProjectionPredicate<'tcx>],
1306 substs: &mut [ty::GenericArg<'tcx>],
1308 let mut replaced = BitSet::new_empty(substs.len());
1310 let mut deque = VecDeque::with_capacity(substs.len());
1311 deque.push_back((param_ty, new_ty));
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.
1319 .all(|(i, ty)| (replaced.is_empty() && i == arg_index) || !ty.contains(param_ty.to_ty(cx.tcx)))
1324 substs[param_ty.index as usize] = ty::GenericArg::from(new_ty);
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()
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, []));
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)
1341 deque.push_back((*term_param_ty, projected_ty));
1351 struct TyPosition<'tcx> {
1353 ty: Option<Ty<'tcx>>,
1355 impl From<Position> for TyPosition<'_> {
1356 fn from(position: Position) -> Self {
1357 Self { position, ty: None }
1360 impl<'tcx> TyPosition<'tcx> {
1361 fn new_deref_stable_for_result(precedence: i8, ty: Ty<'tcx>) -> Self {
1363 position: Position::ReborrowStable(precedence),
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))
1372 (position, _) => position,
1375 fn position_for_arg(self) -> Position {
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();
1387 break match *ty.kind() {
1388 ty::Ref(_, ref_ty, _) => {
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)
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()
1405 ty::Adt(_, substs) if substs.has_non_region_param() => {
1406 TyPosition::new_deref_stable_for_result(precedence, ty)
1415 | ty::FnPtr(_) => Position::DerefStable(precedence, true).into(),
1416 ty::Str | ty::Slice(..) => Position::DerefStable(precedence, false).into(),
1421 | ty::GeneratorWitness(..)
1425 | ty::Alias(ty::Projection, _) => {
1426 Position::DerefStable(precedence, ty.is_sized(cx.tcx, cx.param_env.without_caller_bounds())).into()
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)
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) {
1443 State::DerefMethod {
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)
1457 "*".repeat(ty_changed_count)
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)) {
1466 } else if target_mut == Mutability::Mut {
1472 let expr_str = if !expr_is_macro_call && is_final_ufcs && expr.precedence().order() < PREC_PREFIX {
1473 format!("({expr_str})")
1475 expr_str.into_owned()
1480 EXPLICIT_DEREF_METHODS,
1483 Mutability::Not => "explicit `deref` method call",
1484 Mutability::Mut => "explicit `deref_mut` method call",
1487 format!("{addr_of_str}{deref_str}{expr_str}"),
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)
1505 diag.span_suggestion(data.span, "change this to", sugg, app);
1508 State::ExplicitDeref { mutability } => {
1512 | ExprKind::ConstBlock(_)
1514 | ExprKind::Loop(..)
1515 | ExprKind::Match(..)
1516 ) && matches!(data.position, Position::DerefStable(_, true))
1518 // Rustc bug: auto deref doesn't work on block expression when targeting sized types.
1522 let (prefix, precedence) = if let Some(mutability) = mutability
1523 && !cx.typeck_results().expr_ty(expr).is_ref()
1525 let prefix = match mutability {
1526 Mutability::Not => "&",
1527 Mutability::Mut => "&mut ",
1531 ("", data.position.precedence())
1533 span_lint_hir_and_then(
1535 EXPLICIT_AUTO_DEREF,
1538 "deref which would be done by auto-deref",
1540 let mut app = Applicability::MachineApplicable;
1541 let (snip, snip_is_macro) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
1543 if !snip_is_macro && expr.precedence().order() < precedence && !has_enclosing_paren(&snip) {
1544 format!("{prefix}({snip})")
1546 format!("{prefix}{snip}")
1548 diag.span_suggestion(data.span, "try this", sugg, app);
1552 State::ExplicitDerefField { .. } => {
1556 | ExprKind::ConstBlock(_)
1558 | ExprKind::Loop(..)
1559 | ExprKind::Match(..)
1560 ) && matches!(data.position, Position::DerefStable(_, true))
1562 // Rustc bug: auto deref doesn't work on block expression when targeting sized types.
1566 span_lint_hir_and_then(
1568 EXPLICIT_AUTO_DEREF,
1571 "deref which would be done by auto-deref",
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);
1579 State::Borrow { .. } | State::Reborrow { .. } => (),
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
1589 cx.typeck_results().expr_adjustments(e),
1592 kind: Adjust::Deref(_),
1596 kind: Adjust::Deref(_),
1602 match get_parent_expr(cx, e) {
1603 // Field accesses are the same no matter the number of references.
1605 kind: ExprKind::Field(..),
1610 kind: ExprKind::Unary(UnOp::Deref, _),
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()));
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.
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}")));
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}")));
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
1637 _ => *outer_pat = None,