From c9091b71a15dd6b5efc65bb1299ebb7f7c02c339 Mon Sep 17 00:00:00 2001 From: sinkuu Date: Tue, 10 Jan 2017 23:56:54 +0900 Subject: [PATCH] Don't lint when matching `&&mut` by `&ref` (Fix #1432) --- clippy_lints/src/needless_borrow.rs | 19 ++++++++++--------- tests/compile-fail/needless_borrow.rs | 9 +++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/needless_borrow.rs b/clippy_lints/src/needless_borrow.rs index f05fdaab176..af95bdcecd7 100644 --- a/clippy_lints/src/needless_borrow.rs +++ b/clippy_lints/src/needless_borrow.rs @@ -58,14 +58,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow { if in_macro(cx, pat.span) { return; } - if let PatKind::Binding(BindingMode::BindByRef(MutImmutable), _, _, _) = pat.node { - if let ty::TyRef(_, ref tam) = cx.tcx.tables().pat_ty(pat).sty { - if tam.mutbl == MutImmutable { - if let ty::TyRef(..) = tam.ty.sty { - span_lint(cx, NEEDLESS_BORROW, pat.span, "this pattern creates a reference to a reference") - } - } - } - } + if_let_chain! {[ + let PatKind::Binding(BindingMode::BindByRef(MutImmutable), _, _, _) = pat.node, + let ty::TyRef(_, ref tam) = cx.tcx.tables().pat_ty(pat).sty, + tam.mutbl == MutImmutable, + let ty::TyRef(_, ref tam) = tam.ty.sty, + // only lint immutable refs, because borrowed `&mut T` cannot be moved out + tam.mutbl == MutImmutable, + ], { + span_lint(cx, NEEDLESS_BORROW, pat.span, "this pattern creates a reference to a reference") + }} } } diff --git a/tests/compile-fail/needless_borrow.rs b/tests/compile-fail/needless_borrow.rs index 0b615838a9e..77992252e10 100644 --- a/tests/compile-fail/needless_borrow.rs +++ b/tests/compile-fail/needless_borrow.rs @@ -33,3 +33,12 @@ trait Trait {} impl<'a> Trait for &'a str {} fn h(_: &Trait) {} + +#[allow(dead_code)] +fn issue_1432() { + let mut v = Vec::::new(); + let _ = v.iter_mut().filter(|&ref a| a.is_empty()); + let _ = v.iter().filter(|&ref a| a.is_empty()); + //~^WARNING this pattern creates a reference to a reference + let _ = v.iter().filter(|&a| a.is_empty()); +}