1 //! Checks for needless address of operations (`&`)
3 //! This lint is **warn** by default
6 use rustc::hir::{ExprAddrOf, Expr, MutImmutable};
8 use utils::{span_lint, in_macro};
9 use rustc::ty::adjustment::AutoAdjustment::AdjustDerefRef;
11 /// **What it does:** This lint checks for address of operations (`&`) that are going to be dereferenced immediately by the compiler
13 /// **Why is this bad?** Suggests that the receiver of the expression borrows the expression
15 /// **Known problems:**
17 /// **Example:** `let x: &i32 = &&&&&&5;`
21 "taking a reference that is going to be automatically dereferenced"
25 pub struct NeedlessBorrow;
27 impl LintPass for NeedlessBorrow {
28 fn get_lints(&self) -> LintArray {
29 lint_array!(NEEDLESS_BORROW)
33 impl LateLintPass for NeedlessBorrow {
34 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
35 if in_macro(cx, e.span) {
38 if let ExprAddrOf(MutImmutable, ref inner) = e.node {
39 if let TyRef(..) = cx.tcx.expr_ty(inner).sty {
40 if let Some(&AdjustDerefRef(ref deref)) = cx.tcx.tables.borrow().adjustments.get(&e.id) {
41 if deref.autoderefs > 1 && deref.autoref.is_some() {
45 "this expression borrows a reference that is immediately dereferenced by the compiler");