1 //! Checks for needless address of operations (`&`)
3 //! This lint is **warn** by default
6 use rustc::hir::{ExprAddrOf, Expr, MutImmutable, Pat, PatKind, BindingMode};
8 use utils::{span_lint, in_macro};
9 use rustc::ty::adjustment::AutoAdjustment::AdjustDerefRef;
11 /// **What it does:** Checks for address of operations (`&`) that are going to
12 /// be dereferenced immediately by the compiler.
14 /// **Why is this bad?** Suggests that the receiver of the expression borrows
17 /// **Known problems:** None.
21 /// let x: &i32 = &&&&&&5;
26 "taking a reference that is going to be automatically dereferenced"
30 pub struct NeedlessBorrow;
32 impl LintPass for NeedlessBorrow {
33 fn get_lints(&self) -> LintArray {
34 lint_array!(NEEDLESS_BORROW)
38 impl LateLintPass for NeedlessBorrow {
39 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
40 if in_macro(cx, e.span) {
43 if let ExprAddrOf(MutImmutable, ref inner) = e.node {
44 if let TyRef(..) = cx.tcx.expr_ty(inner).sty {
45 if let Some(&AdjustDerefRef(ref deref)) = cx.tcx.tables.borrow().adjustments.get(&e.id) {
46 if deref.autoderefs > 1 && deref.autoref.is_some() {
50 "this expression borrows a reference that is immediately dereferenced by the \
57 fn check_pat(&mut self, cx: &LateContext, pat: &Pat) {
58 if in_macro(cx, pat.span) {
61 if let PatKind::Binding(BindingMode::BindByRef(MutImmutable), _, _) = pat.node {
62 if let TyRef(_, ref tam) = cx.tcx.pat_ty(pat).sty {
63 if tam.mutbl == MutImmutable {
64 if let TyRef(..) = tam.ty.sty {
68 "this pattern creates a reference to a reference")