1 //! Checks for needless address of operations (`&`)
3 //! This lint is **warn** by default
5 use clippy_utils::diagnostics::span_lint_and_then;
6 use clippy_utils::source::{snippet_opt, snippet_with_applicability, snippet_with_context};
7 use clippy_utils::{get_parent_expr, in_macro, path_to_local};
8 use if_chain::if_chain;
9 use rustc_ast::util::parser::PREC_POSTFIX;
10 use rustc_data_structures::fx::FxIndexMap;
11 use rustc_errors::Applicability;
12 use rustc_hir::{BindingAnnotation, Body, BodyId, BorrowKind, Expr, ExprKind, HirId, Mutability, Pat, PatKind, UnOp};
13 use rustc_lint::{LateContext, LateLintPass};
15 use rustc_middle::ty::adjustment::{Adjust, Adjustment};
16 use rustc_session::{declare_tool_lint, impl_lint_pass};
19 declare_clippy_lint! {
21 /// Checks for address of operations (`&`) that are going to
22 /// be dereferenced immediately by the compiler.
24 /// ### Why is this bad?
25 /// Suggests that the receiver of the expression borrows
30 /// fn fun(_a: &i32) {}
33 /// let x: &i32 = &&&&&&5;
42 "taking a reference that is going to be automatically dereferenced"
45 declare_clippy_lint! {
47 /// Checks for `ref` bindings which create a reference to a reference.
49 /// ### Why is this bad?
50 /// The address-of operator at the use site is clearer about the need for a reference.
56 /// if let Some(ref x) = x {
62 /// if let Some(x) = x {
66 pub REF_BINDING_TO_REFERENCE,
68 "`ref` binding to a reference"
71 impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW, REF_BINDING_TO_REFERENCE]);
73 pub struct NeedlessBorrow {
74 /// The body the first local was found in. Used to emit lints when the traversal of the body has
75 /// been finished. Note we can't lint at the end of every body as they can be nested within each
77 current_body: Option<BodyId>,
78 /// The list of locals currently being checked by the lint.
79 /// If the value is `None`, then the binding has been seen as a ref pattern, but is not linted.
80 /// This is needed for or patterns where one of the branches can be linted, but another can not
83 /// e.g. `m!(x) | Foo::Bar(ref x)`
84 ref_locals: FxIndexMap<HirId, Option<RefPat>>,
88 /// Whether every usage of the binding is dereferenced.
90 /// The spans of all the ref bindings for this local.
92 /// The applicability of this suggestion.
94 /// All the replacements which need to be made.
95 replacements: Vec<(Span, String)>,
98 impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
99 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
100 if let Some(local) = path_to_local(e) {
101 self.check_local_usage(cx, e, local);
104 if e.span.from_expansion() {
107 if let ExprKind::AddrOf(BorrowKind::Ref, mutability, inner) = e.kind {
108 if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(inner).kind() {
109 for adj3 in cx.typeck_results().expr_adjustments(e).windows(3) {
111 kind: Adjust::Deref(_), ..
113 kind: Adjust::Deref(_), ..
115 kind: Adjust::Borrow(_),
119 let help_msg_ty = if matches!(mutability, Mutability::Not) {
122 format!("&mut {}", ty)
130 "this expression borrows a reference (`{}`) that is immediately dereferenced \
135 if let Some(snippet) = snippet_opt(cx, inner.span) {
136 diag.span_suggestion(
140 Applicability::MachineApplicable,
151 fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
152 if let PatKind::Binding(BindingAnnotation::Ref, id, name, _) = pat.kind {
153 if let Some(opt_prev_pat) = self.ref_locals.get_mut(&id) {
154 // This binding id has been seen before. Add this pattern to the list of changes.
155 if let Some(prev_pat) = opt_prev_pat {
156 if in_macro(pat.span) {
157 // Doesn't match the context of the previous pattern. Can't lint here.
158 *opt_prev_pat = None;
160 prev_pat.spans.push(pat.span);
161 prev_pat.replacements.push((
163 snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
173 if !in_macro(pat.span);
174 if let ty::Ref(_, tam, _) = *cx.typeck_results().pat_ty(pat).kind();
175 // only lint immutable refs, because borrowed `&mut T` cannot be moved out
176 if let ty::Ref(_, _, Mutability::Not) = *tam.kind();
178 let mut app = Applicability::MachineApplicable;
179 let snip = snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut app).0;
180 self.current_body = self.current_body.or(cx.enclosing_body);
181 self.ref_locals.insert(
185 spans: vec![pat.span],
187 replacements: vec![(pat.span, snip.into())],
195 fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
196 if Some(body.id()) == self.current_body {
197 for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
198 let replacements = pat.replacements;
202 if pat.always_deref {
205 REF_BINDING_TO_REFERENCE
208 "this pattern creates a reference to a reference",
210 diag.multipart_suggestion("try this", replacements, app);
214 self.current_body = None;
218 impl NeedlessBorrow {
219 fn check_local_usage(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, local: HirId) {
220 if let Some(outer_pat) = self.ref_locals.get_mut(&local) {
221 if let Some(pat) = outer_pat {
222 // Check for auto-deref
224 cx.typeck_results().expr_adjustments(e),
227 kind: Adjust::Deref(_),
231 kind: Adjust::Deref(_),
237 match get_parent_expr(cx, e) {
238 // Field accesses are the same no matter the number of references.
240 kind: ExprKind::Field(..),
245 kind: ExprKind::Unary(UnOp::Deref, _),
247 }) if !in_macro(span) => {
248 // Remove explicit deref.
249 let snip = snippet_with_context(cx, e.span, span.ctxt(), "..", &mut pat.app).0;
250 pat.replacements.push((span, snip.into()));
252 Some(parent) if !in_macro(parent.span) => {
253 // Double reference might be needed at this point.
254 if parent.precedence().order() == PREC_POSTFIX {
255 // Parentheses would be needed here, don't lint.
258 pat.always_deref = false;
259 let snip = snippet_with_context(cx, e.span, parent.span.ctxt(), "..", &mut pat.app).0;
260 pat.replacements.push((e.span, format!("&{}", snip)));
263 _ if !in_macro(e.span) => {
264 // Double reference might be needed at this point.
265 pat.always_deref = false;
266 let snip = snippet_with_applicability(cx, e.span, "..", &mut pat.app);
267 pat.replacements.push((e.span, format!("&{}", snip)));
269 // Edge case for macros. The span of the identifier will usually match the context of the
270 // binding, but not if the identifier was created in a macro. e.g. `concat_idents` and proc
272 _ => *outer_pat = None,