1 //! Checks for needless address of operations (`&`)
3 //! This lint is **warn** by default
5 use crate::utils::{is_automatically_derived, snippet_opt, span_lint_and_then};
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
8 use rustc_hir::{BindingAnnotation, BorrowKind, Expr, ExprKind, Item, Mutability, Pat, PatKind};
9 use rustc_lint::{LateContext, LateLintPass};
11 use rustc_middle::ty::adjustment::{Adjust, Adjustment};
12 use rustc_session::{declare_tool_lint, impl_lint_pass};
13 use rustc_span::def_id::LocalDefId;
15 declare_clippy_lint! {
16 /// **What it does:** Checks for address of operations (`&`) that are going to
17 /// be dereferenced immediately by the compiler.
19 /// **Why is this bad?** Suggests that the receiver of the expression borrows
22 /// **Known problems:** None.
27 /// let x: &i32 = &&&&&&5;
34 "taking a reference that is going to be automatically dereferenced"
38 pub struct NeedlessBorrow {
39 derived_item: Option<LocalDefId>,
42 impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW]);
44 impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
45 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
46 if e.span.from_expansion() || self.derived_item.is_some() {
49 if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, ref inner) = e.kind {
50 if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(inner).kind() {
51 for adj3 in cx.typeck_results().expr_adjustments(e).windows(3) {
53 kind: Adjust::Deref(_), ..
55 kind: Adjust::Deref(_), ..
57 kind: Adjust::Borrow(_),
66 "this expression borrows a reference (`&{}`) that is immediately dereferenced \
71 if let Some(snippet) = snippet_opt(cx, inner.span) {
76 Applicability::MachineApplicable,
86 fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
87 if pat.span.from_expansion() || self.derived_item.is_some() {
91 if let PatKind::Binding(BindingAnnotation::Ref, .., name, _) = pat.kind;
92 if let ty::Ref(_, tam, mutbl) = *cx.typeck_results().pat_ty(pat).kind();
93 if mutbl == Mutability::Not;
94 if let ty::Ref(_, _, mutbl) = *tam.kind();
95 // only lint immutable refs, because borrowed `&mut T` cannot be moved out
96 if mutbl == Mutability::Not;
102 "this pattern creates a reference to a reference",
104 if let Some(snippet) = snippet_opt(cx, name.span) {
105 diag.span_suggestion(
109 Applicability::MachineApplicable,
118 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
119 let attrs = cx.tcx.hir().attrs(item.hir_id());
120 if is_automatically_derived(attrs) {
121 debug_assert!(self.derived_item.is_none());
122 self.derived_item = Some(item.def_id);
126 fn check_item_post(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) {
127 if let Some(id) = self.derived_item {
128 if item.def_id == id {
129 self.derived_item = None;