1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 //! Checks for needless address of operations (`&`)
13 //! This lint is **warn** by default
15 use crate::rustc::hir::{BindingAnnotation, Expr, ExprKind, Item, MutImmutable, Pat, PatKind};
16 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
18 use crate::rustc::ty::adjustment::{Adjust, Adjustment};
19 use crate::rustc::{declare_tool_lint, lint_array};
20 use crate::rustc_errors::Applicability;
21 use crate::utils::{in_macro, snippet_opt, span_lint_and_then};
22 use crate::syntax::ast::NodeId;
23 use if_chain::if_chain;
25 /// **What it does:** Checks for address of operations (`&`) that are going to
26 /// be dereferenced immediately by the compiler.
28 /// **Why is this bad?** Suggests that the receiver of the expression borrows
33 /// let x: &i32 = &&&&&&5;
36 /// **Known problems:** None.
37 declare_clippy_lint! {
40 "taking a reference that is going to be automatically dereferenced"
44 pub struct NeedlessBorrow {
45 derived_item: Option<NodeId>,
48 impl LintPass for NeedlessBorrow {
49 fn get_lints(&self) -> LintArray {
50 lint_array!(NEEDLESS_BORROW)
54 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
55 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
56 if in_macro(e.span) || self.derived_item.is_some() {
59 if let ExprKind::AddrOf(MutImmutable, ref inner) = e.node {
60 if let ty::Ref(..) = cx.tables.expr_ty(inner).sty {
61 for adj3 in cx.tables.expr_adjustments(e).windows(3) {
63 kind: Adjust::Deref(_),
66 kind: Adjust::Deref(_),
69 kind: Adjust::Borrow(_),
77 "this expression borrows a reference that is immediately dereferenced \
80 if let Some(snippet) = snippet_opt(cx, inner.span) {
81 db.span_suggestion_with_applicability(
85 Applicability::MachineApplicable,
95 fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
96 if in_macro(pat.span) || self.derived_item.is_some() {
100 if let PatKind::Binding(BindingAnnotation::Ref, _, name, _) = pat.node;
101 if let ty::Ref(_, tam, mutbl) = cx.tables.pat_ty(pat).sty;
102 if mutbl == MutImmutable;
103 if let ty::Ref(_, _, mutbl) = tam.sty;
104 // only lint immutable refs, because borrowed `&mut T` cannot be moved out
105 if mutbl == MutImmutable;
111 "this pattern creates a reference to a reference",
113 if let Some(snippet) = snippet_opt(cx, name.span) {
114 db.span_suggestion_with_applicability(
118 Applicability::MachineApplicable,
127 fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
128 if item.attrs.iter().any(|a| a.check_name("automatically_derived")) {
129 debug_assert!(self.derived_item.is_none());
130 self.derived_item = Some(item.id);
134 fn check_item_post(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
135 if let Some(id) = self.derived_item {
137 self.derived_item = None;