Don't lint when matching &&mut by &ref (Fix )

This commit is contained in:
sinkuu 2017-01-10 23:56:54 +09:00
parent 5e7727119e
commit c9091b71a1
2 changed files with 19 additions and 9 deletions
clippy_lints/src
tests/compile-fail

@ -58,14 +58,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
if in_macro(cx, pat.span) { if in_macro(cx, pat.span) {
return; return;
} }
if let PatKind::Binding(BindingMode::BindByRef(MutImmutable), _, _, _) = pat.node { if_let_chain! {[
if let ty::TyRef(_, ref tam) = cx.tcx.tables().pat_ty(pat).sty { let PatKind::Binding(BindingMode::BindByRef(MutImmutable), _, _, _) = pat.node,
if tam.mutbl == MutImmutable { let ty::TyRef(_, ref tam) = cx.tcx.tables().pat_ty(pat).sty,
if let ty::TyRef(..) = tam.ty.sty { tam.mutbl == MutImmutable,
span_lint(cx, NEEDLESS_BORROW, pat.span, "this pattern creates a reference to a reference") 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")
}}
} }
} }

@ -33,3 +33,12 @@ trait Trait {}
impl<'a> Trait for &'a str {} impl<'a> Trait for &'a str {}
fn h(_: &Trait) {} fn h(_: &Trait) {}
#[allow(dead_code)]
fn issue_1432() {
let mut v = Vec::<String>::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());
}