1 use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2 use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
3 use clippy_utils::ty::peel_mid_ty_refs;
4 use clippy_utils::{get_parent_expr, get_parent_node, is_lint_allowed, path_to_local};
5 use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
6 use rustc_data_structures::fx::FxIndexMap;
7 use rustc_errors::Applicability;
9 BindingAnnotation, Body, BodyId, BorrowKind, Destination, Expr, ExprKind, HirId, MatchSource, Mutability, Node,
12 use rustc_lint::{LateContext, LateLintPass};
13 use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
14 use rustc_middle::ty::{self, Ty, TyCtxt, TyS, TypeckResults};
15 use rustc_session::{declare_tool_lint, impl_lint_pass};
16 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();
33 /// Could be written as:
35 /// let a: &mut String = &mut String::from("foo");
39 /// This lint excludes
41 /// let _ = d.unwrap().deref();
43 #[clippy::version = "1.44.0"]
44 pub EXPLICIT_DEREF_METHODS,
46 "Explicit use of deref or deref_mut method while not in a method chain."
49 declare_clippy_lint! {
51 /// Checks for address of operations (`&`) that are going to
52 /// be dereferenced immediately by the compiler.
54 /// ### Why is this bad?
55 /// Suggests that the receiver of the expression borrows
60 /// fn fun(_a: &i32) {}
63 /// let x: &i32 = &&&&&&5;
70 #[clippy::version = "pre 1.29.0"]
73 "taking a reference that is going to be automatically dereferenced"
76 declare_clippy_lint! {
78 /// Checks for `ref` bindings which create a reference to a reference.
80 /// ### Why is this bad?
81 /// The address-of operator at the use site is clearer about the need for a reference.
87 /// if let Some(ref x) = x {
93 /// if let Some(x) = x {
97 #[clippy::version = "1.54.0"]
98 pub REF_BINDING_TO_REFERENCE,
100 "`ref` binding to a reference"
103 impl_lint_pass!(Dereferencing => [
104 EXPLICIT_DEREF_METHODS,
106 REF_BINDING_TO_REFERENCE,
110 pub struct Dereferencing {
111 state: Option<(State, StateData)>,
113 // While parsing a `deref` method call in ufcs form, the path to the function is itself an
114 // expression. This is to store the id of that expression so it can be skipped when
115 // `check_expr` is called for it.
116 skip_expr: Option<HirId>,
118 /// The body the first local was found in. Used to emit lints when the traversal of the body has
119 /// been finished. Note we can't lint at the end of every body as they can be nested within each
121 current_body: Option<BodyId>,
122 /// The list of locals currently being checked by the lint.
123 /// If the value is `None`, then the binding has been seen as a ref pattern, but is not linted.
124 /// This is needed for or patterns where one of the branches can be linted, but another can not
127 /// e.g. `m!(x) | Foo::Bar(ref x)`
128 ref_locals: FxIndexMap<HirId, Option<RefPat>>,
132 /// Span of the top level expression
134 /// The required mutability
135 target_mut: Mutability,
139 // Any number of deref method calls.
141 // The number of calls in a sequence which changed the referenced type
142 ty_changed_count: usize,
150 // A reference operation considered by this lint pass
158 /// Whether every usage of the binding is dereferenced.
160 /// The spans of all the ref bindings for this local.
162 /// The applicability of this suggestion.
164 /// All the replacements which need to be made.
165 replacements: Vec<(Span, String)>,
168 impl<'tcx> LateLintPass<'tcx> for Dereferencing {
169 #[allow(clippy::too_many_lines)]
170 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
171 // Skip path expressions from deref calls. e.g. `Deref::deref(e)`
172 if Some(expr.hir_id) == self.skip_expr.take() {
176 if let Some(local) = path_to_local(expr) {
177 self.check_local_usage(cx, expr, local);
180 // Stop processing sub expressions when a macro call is seen
181 if expr.span.from_expansion() {
182 if let Some((state, data)) = self.state.take() {
183 report(cx, expr, state, data);
188 let typeck = cx.typeck_results();
189 let (kind, sub_expr) = if let Some(x) = try_parse_ref_op(cx.tcx, typeck, expr) {
192 // The whole chain of reference operations has been seen
193 if let Some((state, data)) = self.state.take() {
194 report(cx, expr, state, data);
199 match (self.state.take(), kind) {
201 let parent = get_parent_node(cx.tcx, expr.hir_id);
202 let expr_ty = typeck.expr_ty(expr);
205 RefOp::Method(target_mut)
206 if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
207 && is_linted_explicit_deref_position(parent, expr.hir_id, expr.span) =>
211 ty_changed_count: if deref_method_same_type(expr_ty, typeck.expr_ty(sub_expr)) {
216 is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
225 // Find the number of times the borrow is auto-derefed.
226 let mut iter = find_adjustments(cx.tcx, typeck, expr).iter();
227 if let Some((i, adjust)) = iter.by_ref().enumerate().find_map(|(i, adjust)| {
228 if !matches!(adjust.kind, Adjust::Deref(_)) {
230 } else if !adjust.target.is_ref() {
231 // Add one to the number of references found.
232 Some((i + 1, adjust))
237 // Found two consecutive derefs. At least one can be removed.
239 let target_mut = iter::once(adjust)
241 .find_map(|adjust| match adjust.kind {
242 Adjust::Borrow(AutoBorrow::Ref(_, m)) => Some(m.into()),
245 // This default should never happen. Auto-deref always reborrows.
246 .unwrap_or(Mutability::Not);
248 // Subtract one for the current borrow expression, and one to cover the last
249 // reference which can't be removed (it's either reborrowed, or needed for
250 // auto-deref to happen).
251 State::DerefedBorrow {
253 // Truncation here would require more than a `u32::MAX` level reference. The compiler
254 // does not support this.
255 #[allow(clippy::cast_possible_truncation)]
269 (Some((State::DerefMethod { ty_changed_count, .. }, data)), RefOp::Method(_)) => {
272 ty_changed_count: if deref_method_same_type(typeck.expr_ty(expr), typeck.expr_ty(sub_expr)) {
277 is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
282 (Some((State::DerefedBorrow { count }, data)), RefOp::AddrOf) if count != 0 => {
283 self.state = Some((State::DerefedBorrow { count: count - 1 }, data));
286 (Some((state, data)), _) => report(cx, expr, state, data),
290 fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
291 if let PatKind::Binding(BindingAnnotation::Ref, id, name, _) = pat.kind {
292 if let Some(opt_prev_pat) = self.ref_locals.get_mut(&id) {
293 // This binding id has been seen before. Add this pattern to the list of changes.
294 if let Some(prev_pat) = opt_prev_pat {
295 if pat.span.from_expansion() {
296 // Doesn't match the context of the previous pattern. Can't lint here.
297 *opt_prev_pat = None;
299 prev_pat.spans.push(pat.span);
300 prev_pat.replacements.push((
302 snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
312 if !pat.span.from_expansion();
313 if let ty::Ref(_, tam, _) = *cx.typeck_results().pat_ty(pat).kind();
314 // only lint immutable refs, because borrowed `&mut T` cannot be moved out
315 if let ty::Ref(_, _, Mutability::Not) = *tam.kind();
317 let mut app = Applicability::MachineApplicable;
318 let snip = snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut app).0;
319 self.current_body = self.current_body.or(cx.enclosing_body);
320 self.ref_locals.insert(
324 spans: vec![pat.span],
326 replacements: vec![(pat.span, snip.into())],
334 fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
335 if Some(body.id()) == self.current_body {
336 for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
337 let replacements = pat.replacements;
341 if pat.always_deref {
344 REF_BINDING_TO_REFERENCE
347 "this pattern creates a reference to a reference",
349 diag.multipart_suggestion("try this", replacements, app);
353 self.current_body = None;
358 fn try_parse_ref_op<'tcx>(
360 typeck: &'tcx TypeckResults<'_>,
361 expr: &'tcx Expr<'_>,
362 ) -> Option<(RefOp, &'tcx Expr<'tcx>)> {
363 let (def_id, arg) = match expr.kind {
364 ExprKind::MethodCall(_, _, [arg], _) => (typeck.type_dependent_def_id(expr.hir_id)?, arg),
367 kind: ExprKind::Path(path),
372 ) => (typeck.qpath_res(path, *hir_id).opt_def_id()?, arg),
373 ExprKind::Unary(UnOp::Deref, sub_expr) if !typeck.expr_ty(sub_expr).is_unsafe_ptr() => {
374 return Some((RefOp::Deref, sub_expr));
376 ExprKind::AddrOf(BorrowKind::Ref, _, sub_expr) => return Some((RefOp::AddrOf, sub_expr)),
379 if tcx.is_diagnostic_item(sym::deref_method, def_id) {
380 Some((RefOp::Method(Mutability::Not), arg))
381 } else if tcx.trait_of_item(def_id)? == tcx.lang_items().deref_mut_trait()? {
382 Some((RefOp::Method(Mutability::Mut), arg))
388 // Checks whether the type for a deref call actually changed the type, not just the mutability of
390 fn deref_method_same_type(result_ty: Ty<'_>, arg_ty: Ty<'_>) -> bool {
391 match (result_ty.kind(), arg_ty.kind()) {
392 (ty::Ref(_, result_ty, _), ty::Ref(_, arg_ty, _)) => TyS::same_type(result_ty, arg_ty),
394 // The result type for a deref method is always a reference
395 // Not matching the previous pattern means the argument type is not a reference
396 // This means that the type did change
401 // Checks whether the parent node is a suitable context for switching from a deref method to the
403 fn is_linted_explicit_deref_position(parent: Option<Node<'_>>, child_id: HirId, child_span: Span) -> bool {
404 let parent = match parent {
405 Some(Node::Expr(e)) if e.span.ctxt() == child_span.ctxt() => e,
409 // Leave deref calls in the middle of a method chain.
410 // e.g. x.deref().foo()
411 ExprKind::MethodCall(_, _, [self_arg, ..], _) if self_arg.hir_id == child_id => false,
413 // Leave deref calls resulting in a called function
414 // e.g. (x.deref())()
415 ExprKind::Call(func_expr, _) if func_expr.hir_id == child_id => false,
417 // Makes an ugly suggestion
418 // e.g. *x.deref() => *&*x
419 ExprKind::Unary(UnOp::Deref, _)
420 // Postfix expressions would require parens
421 | ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
422 | ExprKind::Field(..)
423 | ExprKind::Index(..)
424 | ExprKind::Err => false,
427 | ExprKind::ConstBlock(..)
430 | ExprKind::MethodCall(..)
432 | ExprKind::Binary(..)
433 | ExprKind::Unary(..)
437 | ExprKind::DropTemps(..)
440 | ExprKind::Match(..)
442 | ExprKind::Closure(..)
443 | ExprKind::Block(..)
444 | ExprKind::Assign(..)
445 | ExprKind::AssignOp(..)
447 | ExprKind::AddrOf(..)
448 | ExprKind::Break(..)
449 | ExprKind::Continue(..)
451 | ExprKind::InlineAsm(..)
452 | ExprKind::LlvmInlineAsm(..)
453 | ExprKind::Struct(..)
454 | ExprKind::Repeat(..)
455 | ExprKind::Yield(..) => true,
459 /// Adjustments are sometimes made in the parent block rather than the expression itself.
460 fn find_adjustments<'tcx>(
462 typeck: &'tcx TypeckResults<'_>,
463 expr: &'tcx Expr<'_>,
464 ) -> &'tcx [Adjustment<'tcx>] {
466 let mut iter = map.parent_iter(expr.hir_id);
470 match typeck.expr_adjustments(prev) {
475 match iter.next().map(|(_, x)| x) {
476 Some(Node::Block(_)) => {
477 if let Some((_, Node::Expr(e))) = iter.next() {
480 // This shouldn't happen. Blocks are always contained in an expression.
484 Some(Node::Expr(&Expr {
485 kind: ExprKind::Break(Destination { target_id: Ok(id), .. }, _),
488 if let Some(Node::Expr(e)) = map.find(id) {
490 iter = map.parent_iter(id);
492 // This shouldn't happen. The destination should exist.
501 #[allow(clippy::needless_pass_by_value)]
502 fn report(cx: &LateContext<'_>, expr: &Expr<'_>, state: State, data: StateData) {
508 let mut app = Applicability::MachineApplicable;
509 let (expr_str, expr_is_macro_call) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
510 let ty = cx.typeck_results().expr_ty(expr);
511 let (_, ref_count) = peel_mid_ty_refs(ty);
512 let deref_str = if ty_changed_count >= ref_count && ref_count != 0 {
513 // a deref call changing &T -> &U requires two deref operators the first time
514 // this occurs. One to remove the reference, a second to call the deref impl.
515 "*".repeat(ty_changed_count + 1)
517 "*".repeat(ty_changed_count)
519 let addr_of_str = if ty_changed_count < ref_count {
520 // Check if a reborrow from &mut T -> &T is required.
521 if data.target_mut == Mutability::Not && matches!(ty.kind(), ty::Ref(_, _, Mutability::Mut)) {
526 } else if data.target_mut == Mutability::Mut {
532 let expr_str = if !expr_is_macro_call && is_final_ufcs && expr.precedence().order() < PREC_PREFIX {
533 format!("({})", expr_str)
535 expr_str.into_owned()
540 EXPLICIT_DEREF_METHODS,
542 match data.target_mut {
543 Mutability::Not => "explicit `deref` method call",
544 Mutability::Mut => "explicit `deref_mut` method call",
547 format!("{}{}{}", addr_of_str, deref_str, expr_str),
551 State::DerefedBorrow { .. } => {
552 let mut app = Applicability::MachineApplicable;
553 let snip = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app).0;
559 "this expression borrows a reference (`{}`) that is immediately dereferenced by the compiler",
560 cx.typeck_results().expr_ty(expr),
571 fn check_local_usage(&mut self, cx: &LateContext<'_>, e: &Expr<'_>, local: HirId) {
572 if let Some(outer_pat) = self.ref_locals.get_mut(&local) {
573 if let Some(pat) = outer_pat {
574 // Check for auto-deref
576 cx.typeck_results().expr_adjustments(e),
579 kind: Adjust::Deref(_),
583 kind: Adjust::Deref(_),
589 match get_parent_expr(cx, e) {
590 // Field accesses are the same no matter the number of references.
592 kind: ExprKind::Field(..),
597 kind: ExprKind::Unary(UnOp::Deref, _),
599 }) if !span.from_expansion() => {
600 // Remove explicit deref.
601 let snip = snippet_with_context(cx, e.span, span.ctxt(), "..", &mut pat.app).0;
602 pat.replacements.push((span, snip.into()));
604 Some(parent) if !parent.span.from_expansion() => {
605 // Double reference might be needed at this point.
606 if parent.precedence().order() == PREC_POSTFIX {
607 // Parentheses would be needed here, don't lint.
610 pat.always_deref = false;
611 let snip = snippet_with_context(cx, e.span, parent.span.ctxt(), "..", &mut pat.app).0;
612 pat.replacements.push((e.span, format!("&{}", snip)));
615 _ if !e.span.from_expansion() => {
616 // Double reference might be needed at this point.
617 pat.always_deref = false;
618 let snip = snippet_with_applicability(cx, e.span, "..", &mut pat.app);
619 pat.replacements.push((e.span, format!("&{}", snip)));
621 // Edge case for macros. The span of the identifier will usually match the context of the
622 // binding, but not if the identifier was created in a macro. e.g. `concat_idents` and proc
624 _ => *outer_pat = None,