1 use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
2 use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
3 use clippy_utils::sugg::has_enclosing_paren;
4 use clippy_utils::ty::peel_mid_ty_refs;
5 use clippy_utils::{get_parent_expr, get_parent_node, is_lint_allowed, path_to_local};
6 use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
7 use rustc_data_structures::fx::FxIndexMap;
8 use rustc_errors::Applicability;
10 BindingAnnotation, Body, BodyId, BorrowKind, Destination, Expr, ExprKind, HirId, MatchSource, Mutability, Node,
13 use rustc_lint::{LateContext, LateLintPass};
14 use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
15 use rustc_middle::ty::{self, Ty, TyCtxt, TypeckResults};
16 use rustc_session::{declare_tool_lint, impl_lint_pass};
17 use rustc_span::{symbol::sym, Span};
19 declare_clippy_lint! {
21 /// Checks for explicit `deref()` or `deref_mut()` method calls.
23 /// ### Why is this bad?
24 /// Dereferencing by `&*x` or `&mut *x` is clearer and more concise,
25 /// when not part of a method chain.
29 /// use std::ops::Deref;
30 /// let a: &mut String = &mut String::from("foo");
31 /// let b: &str = a.deref();
36 /// let a: &mut String = &mut String::from("foo");
40 /// This lint excludes:
42 /// let _ = d.unwrap().deref();
44 #[clippy::version = "1.44.0"]
45 pub EXPLICIT_DEREF_METHODS,
47 "Explicit use of deref or deref_mut method while not in a method chain."
50 declare_clippy_lint! {
52 /// Checks for address of operations (`&`) that are going to
53 /// be dereferenced immediately by the compiler.
55 /// ### Why is this bad?
56 /// Suggests that the receiver of the expression borrows
61 /// fn fun(_a: &i32) {}
63 /// let x: &i32 = &&&&&&5;
69 /// # fn fun(_a: &i32) {}
73 #[clippy::version = "pre 1.29.0"]
76 "taking a reference that is going to be automatically dereferenced"
79 declare_clippy_lint! {
81 /// Checks for `ref` bindings which create a reference to a reference.
83 /// ### Why is this bad?
84 /// The address-of operator at the use site is clearer about the need for a reference.
89 /// if let Some(ref x) = x {
97 /// if let Some(x) = x {
101 #[clippy::version = "1.54.0"]
102 pub REF_BINDING_TO_REFERENCE,
104 "`ref` binding to a reference"
107 impl_lint_pass!(Dereferencing => [
108 EXPLICIT_DEREF_METHODS,
110 REF_BINDING_TO_REFERENCE,
114 pub struct Dereferencing {
115 state: Option<(State, StateData)>,
117 // While parsing a `deref` method call in ufcs form, the path to the function is itself an
118 // expression. This is to store the id of that expression so it can be skipped when
119 // `check_expr` is called for it.
120 skip_expr: Option<HirId>,
122 /// The body the first local was found in. Used to emit lints when the traversal of the body has
123 /// been finished. Note we can't lint at the end of every body as they can be nested within each
125 current_body: Option<BodyId>,
126 /// The list of locals currently being checked by the lint.
127 /// If the value is `None`, then the binding has been seen as a ref pattern, but is not linted.
128 /// This is needed for or patterns where one of the branches can be linted, but another can not
131 /// e.g. `m!(x) | Foo::Bar(ref x)`
132 ref_locals: FxIndexMap<HirId, Option<RefPat>>,
136 /// Span of the top level expression
142 // Any number of deref method calls.
144 // The number of calls in a sequence which changed the referenced type
145 ty_changed_count: usize,
147 /// The required mutability
148 target_mut: Mutability,
152 required_precedence: i8,
157 // A reference operation considered by this lint pass
165 /// Whether every usage of the binding is dereferenced.
167 /// The spans of all the ref bindings for this local.
169 /// The applicability of this suggestion.
171 /// All the replacements which need to be made.
172 replacements: Vec<(Span, String)>,
173 /// The [`HirId`] that the lint should be emitted at.
177 impl<'tcx> LateLintPass<'tcx> for Dereferencing {
178 #[expect(clippy::too_many_lines)]
179 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
180 // Skip path expressions from deref calls. e.g. `Deref::deref(e)`
181 if Some(expr.hir_id) == self.skip_expr.take() {
185 if let Some(local) = path_to_local(expr) {
186 self.check_local_usage(cx, expr, local);
189 // Stop processing sub expressions when a macro call is seen
190 if expr.span.from_expansion() {
191 if let Some((state, data)) = self.state.take() {
192 report(cx, expr, state, data);
197 let typeck = cx.typeck_results();
198 let (kind, sub_expr) = if let Some(x) = try_parse_ref_op(cx.tcx, typeck, expr) {
201 // The whole chain of reference operations has been seen
202 if let Some((state, data)) = self.state.take() {
203 report(cx, expr, state, data);
208 match (self.state.take(), kind) {
210 let parent = get_parent_node(cx.tcx, expr.hir_id);
211 let expr_ty = typeck.expr_ty(expr);
214 RefOp::Method(target_mut)
215 if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
216 && is_linted_explicit_deref_position(parent, expr.hir_id, expr.span) =>
220 ty_changed_count: if deref_method_same_type(expr_ty, typeck.expr_ty(sub_expr)) {
225 is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
235 // Find the number of times the borrow is auto-derefed.
236 let mut iter = find_adjustments(cx.tcx, typeck, expr).iter();
237 let mut deref_count = 0usize;
238 let next_adjust = loop {
241 if !matches!(adjust.kind, Adjust::Deref(_)) {
243 } else if !adjust.target.is_ref() {
253 // Determine the required number of references before any can be removed. In all cases the
254 // reference made by the current expression will be removed. After that there are four cases to
257 // 1. Auto-borrow will trigger in the current position, so no further references are required.
258 // 2. Auto-deref ends at a reference, or the underlying type, so one extra needs to be left to
259 // handle the automatically inserted re-borrow.
260 // 3. Auto-deref hits a user-defined `Deref` impl, so at least one reference needs to exist to
262 // 4. If the chain of non-user-defined derefs ends with a mutable re-borrow, and re-borrow
263 // adjustments will not be inserted automatically, then leave one further reference to avoid
264 // moving a mutable borrow.
266 // fn foo<T>(x: &mut Option<&mut T>, y: &mut T) {
268 // // Removing the borrow will cause `x` to be moved
269 // Some(x) => &mut *x,
274 "this expression creates a reference which is immediately dereferenced by the compiler";
275 let borrow_msg = "this expression borrows a value the compiler would automatically borrow";
277 let (required_refs, required_precedence, msg) = if is_auto_borrow_position(parent, expr.hir_id)
279 (1, PREC_POSTFIX, if deref_count == 1 { borrow_msg } else { deref_msg })
280 } else if let Some(&Adjust::Borrow(AutoBorrow::Ref(_, mutability))) =
281 next_adjust.map(|a| &a.kind)
283 if matches!(mutability, AutoBorrowMutability::Mut { .. })
284 && !is_auto_reborrow_position(parent)
294 if deref_count >= required_refs {
296 State::DerefedBorrow {
297 // One of the required refs is for the current borrow expression, the remaining ones
298 // can't be removed without breaking the code. See earlier comment.
299 count: deref_count - required_refs,
326 ty_changed_count: if deref_method_same_type(typeck.expr_ty(expr), typeck.expr_ty(sub_expr)) {
331 is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
339 State::DerefedBorrow {
349 State::DerefedBorrow {
358 (Some((state, data)), _) => report(cx, expr, state, data),
362 fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
363 if let PatKind::Binding(BindingAnnotation::Ref, id, name, _) = pat.kind {
364 if let Some(opt_prev_pat) = self.ref_locals.get_mut(&id) {
365 // This binding id has been seen before. Add this pattern to the list of changes.
366 if let Some(prev_pat) = opt_prev_pat {
367 if pat.span.from_expansion() {
368 // Doesn't match the context of the previous pattern. Can't lint here.
369 *opt_prev_pat = None;
371 prev_pat.spans.push(pat.span);
372 prev_pat.replacements.push((
374 snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
384 if !pat.span.from_expansion();
385 if let ty::Ref(_, tam, _) = *cx.typeck_results().pat_ty(pat).kind();
386 // only lint immutable refs, because borrowed `&mut T` cannot be moved out
387 if let ty::Ref(_, _, Mutability::Not) = *tam.kind();
389 let mut app = Applicability::MachineApplicable;
390 let snip = snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut app).0;
391 self.current_body = self.current_body.or(cx.enclosing_body);
392 self.ref_locals.insert(
396 spans: vec![pat.span],
398 replacements: vec![(pat.span, snip.into())],
407 fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
408 if Some(body.id()) == self.current_body {
409 for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
410 let replacements = pat.replacements;
412 let lint = if pat.always_deref {
415 REF_BINDING_TO_REFERENCE
417 span_lint_hir_and_then(
422 "this pattern creates a reference to a reference",
424 diag.multipart_suggestion("try this", replacements, app);
428 self.current_body = None;
433 fn try_parse_ref_op<'tcx>(
435 typeck: &'tcx TypeckResults<'_>,
436 expr: &'tcx Expr<'_>,
437 ) -> Option<(RefOp, &'tcx Expr<'tcx>)> {
438 let (def_id, arg) = match expr.kind {
439 ExprKind::MethodCall(_, [arg], _) => (typeck.type_dependent_def_id(expr.hir_id)?, arg),
442 kind: ExprKind::Path(path),
447 ) => (typeck.qpath_res(path, *hir_id).opt_def_id()?, arg),
448 ExprKind::Unary(UnOp::Deref, sub_expr) if !typeck.expr_ty(sub_expr).is_unsafe_ptr() => {
449 return Some((RefOp::Deref, sub_expr));
451 ExprKind::AddrOf(BorrowKind::Ref, _, sub_expr) => return Some((RefOp::AddrOf, sub_expr)),
454 if tcx.is_diagnostic_item(sym::deref_method, def_id) {
455 Some((RefOp::Method(Mutability::Not), arg))
456 } else if tcx.trait_of_item(def_id)? == tcx.lang_items().deref_mut_trait()? {
457 Some((RefOp::Method(Mutability::Mut), arg))
463 // Checks whether the type for a deref call actually changed the type, not just the mutability of
465 fn deref_method_same_type<'tcx>(result_ty: Ty<'tcx>, arg_ty: Ty<'tcx>) -> bool {
466 match (result_ty.kind(), arg_ty.kind()) {
467 (ty::Ref(_, result_ty, _), ty::Ref(_, arg_ty, _)) => result_ty == arg_ty,
469 // The result type for a deref method is always a reference
470 // Not matching the previous pattern means the argument type is not a reference
471 // This means that the type did change
476 // Checks whether the parent node is a suitable context for switching from a deref method to the
478 fn is_linted_explicit_deref_position(parent: Option<Node<'_>>, child_id: HirId, child_span: Span) -> bool {
479 let parent = match parent {
480 Some(Node::Expr(e)) if e.span.ctxt() == child_span.ctxt() => e,
484 // Leave deref calls in the middle of a method chain.
485 // e.g. x.deref().foo()
486 ExprKind::MethodCall(_, [self_arg, ..], _) if self_arg.hir_id == child_id => false,
488 // Leave deref calls resulting in a called function
489 // e.g. (x.deref())()
490 ExprKind::Call(func_expr, _) if func_expr.hir_id == child_id => false,
492 // Makes an ugly suggestion
493 // e.g. *x.deref() => *&*x
494 ExprKind::Unary(UnOp::Deref, _)
495 // Postfix expressions would require parens
496 | ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
497 | ExprKind::Field(..)
498 | ExprKind::Index(..)
499 | ExprKind::Err => false,
502 | ExprKind::ConstBlock(..)
505 | ExprKind::MethodCall(..)
507 | ExprKind::Binary(..)
508 | ExprKind::Unary(..)
512 | ExprKind::DropTemps(..)
515 | ExprKind::Match(..)
517 | ExprKind::Closure(..)
518 | ExprKind::Block(..)
519 | ExprKind::Assign(..)
520 | ExprKind::AssignOp(..)
522 | ExprKind::AddrOf(..)
523 | ExprKind::Break(..)
524 | ExprKind::Continue(..)
526 | ExprKind::InlineAsm(..)
527 | ExprKind::Struct(..)
528 | ExprKind::Repeat(..)
529 | ExprKind::Yield(..) => true,
533 /// Checks if the given expression is in a position which can be auto-reborrowed.
534 /// Note: This is only correct assuming auto-deref is already occurring.
535 fn is_auto_reborrow_position(parent: Option<Node<'_>>) -> bool {
537 Some(Node::Expr(parent)) => matches!(parent.kind, ExprKind::MethodCall(..) | ExprKind::Call(..)),
538 Some(Node::Local(_)) => true,
543 /// Checks if the given expression is a position which can auto-borrow.
544 fn is_auto_borrow_position(parent: Option<Node<'_>>, child_id: HirId) -> bool {
545 if let Some(Node::Expr(parent)) = parent {
547 // ExprKind::MethodCall(_, [self_arg, ..], _) => self_arg.hir_id == child_id,
548 ExprKind::Field(..) => true,
549 ExprKind::Call(f, _) => f.hir_id == child_id,
557 /// Adjustments are sometimes made in the parent block rather than the expression itself.
558 fn find_adjustments<'tcx>(
560 typeck: &'tcx TypeckResults<'tcx>,
561 expr: &'tcx Expr<'tcx>,
562 ) -> &'tcx [Adjustment<'tcx>] {
564 let mut iter = map.parent_iter(expr.hir_id);
568 match typeck.expr_adjustments(prev) {
573 match iter.next().map(|(_, x)| x) {
574 Some(Node::Block(_)) => {
575 if let Some((_, Node::Expr(e))) = iter.next() {
578 // This shouldn't happen. Blocks are always contained in an expression.
582 Some(Node::Expr(&Expr {
583 kind: ExprKind::Break(Destination { target_id: Ok(id), .. }, _),
586 if let Some(Node::Expr(e)) = map.find(id) {
588 iter = map.parent_iter(id);
590 // This shouldn't happen. The destination should exist.
599 #[expect(clippy::needless_pass_by_value)]
600 fn report<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, state: State, data: StateData) {
607 let mut app = Applicability::MachineApplicable;
608 let (expr_str, expr_is_macro_call) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
609 let ty = cx.typeck_results().expr_ty(expr);
610 let (_, ref_count) = peel_mid_ty_refs(ty);
611 let deref_str = if ty_changed_count >= ref_count && ref_count != 0 {
612 // a deref call changing &T -> &U requires two deref operators the first time
613 // this occurs. One to remove the reference, a second to call the deref impl.
614 "*".repeat(ty_changed_count + 1)
616 "*".repeat(ty_changed_count)
618 let addr_of_str = if ty_changed_count < ref_count {
619 // Check if a reborrow from &mut T -> &T is required.
620 if target_mut == Mutability::Not && matches!(ty.kind(), ty::Ref(_, _, Mutability::Mut)) {
625 } else if target_mut == Mutability::Mut {
631 let expr_str = if !expr_is_macro_call && is_final_ufcs && expr.precedence().order() < PREC_PREFIX {
632 format!("({})", expr_str)
634 expr_str.into_owned()
639 EXPLICIT_DEREF_METHODS,
642 Mutability::Not => "explicit `deref` method call",
643 Mutability::Mut => "explicit `deref_mut` method call",
646 format!("{}{}{}", addr_of_str, deref_str, expr_str),
650 State::DerefedBorrow {
655 let mut app = Applicability::MachineApplicable;
656 let snip = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app).0;
657 span_lint_hir_and_then(cx, NEEDLESS_BORROW, data.hir_id, data.span, msg, |diag| {
658 let sugg = if required_precedence > expr.precedence().order() && !has_enclosing_paren(&snip) {
659 format!("({})", snip)
663 diag.span_suggestion(data.span, "change this to", sugg, app);
670 fn check_local_usage<'tcx>(&mut self, cx: &LateContext<'tcx>, e: &Expr<'tcx>, local: HirId) {
671 if let Some(outer_pat) = self.ref_locals.get_mut(&local) {
672 if let Some(pat) = outer_pat {
673 // Check for auto-deref
675 cx.typeck_results().expr_adjustments(e),
678 kind: Adjust::Deref(_),
682 kind: Adjust::Deref(_),
688 match get_parent_expr(cx, e) {
689 // Field accesses are the same no matter the number of references.
691 kind: ExprKind::Field(..),
696 kind: ExprKind::Unary(UnOp::Deref, _),
698 }) if !span.from_expansion() => {
699 // Remove explicit deref.
700 let snip = snippet_with_context(cx, e.span, span.ctxt(), "..", &mut pat.app).0;
701 pat.replacements.push((span, snip.into()));
703 Some(parent) if !parent.span.from_expansion() => {
704 // Double reference might be needed at this point.
705 if parent.precedence().order() == PREC_POSTFIX {
706 // Parentheses would be needed here, don't lint.
709 pat.always_deref = false;
710 let snip = snippet_with_context(cx, e.span, parent.span.ctxt(), "..", &mut pat.app).0;
711 pat.replacements.push((e.span, format!("&{}", snip)));
714 _ if !e.span.from_expansion() => {
715 // Double reference might be needed at this point.
716 pat.always_deref = false;
717 let snip = snippet_with_applicability(cx, e.span, "..", &mut pat.app);
718 pat.replacements.push((e.span, format!("&{}", snip)));
720 // Edge case for macros. The span of the identifier will usually match the context of the
721 // binding, but not if the identifier was created in a macro. e.g. `concat_idents` and proc
723 _ => *outer_pat = None,