1 //! Checks for useless borrowed references.
3 //! This lint is **warn** by default
6 use rustc::hir::{MutImmutable, Pat, PatKind, BindingMode};
8 use utils::{span_lint, in_macro};
10 /// **What it does:** Checks for useless borrowed references.
12 /// **Why is this bad?** It is completely useless and make the code look more complex than it
15 /// **Known problems:** None.
19 /// let mut v = Vec::<String>::new();
20 /// let _ = v.iter_mut().filter(|&ref a| a.is_empty());
22 /// This clojure takes a reference on something that has been matched as a reference and
24 /// As such, it could just be |a| a.is_empty()
26 pub NEEDLESS_BORROWED_REFERENCE,
28 "taking a needless borrowed reference"
31 #[derive(Copy, Clone)]
32 pub struct NeedlessBorrowedRef;
34 impl LintPass for NeedlessBorrowedRef {
35 fn get_lints(&self) -> LintArray {
36 lint_array!(NEEDLESS_BORROWED_REFERENCE)
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrowedRef {
41 fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
42 if in_macro(pat.span) {
43 // OK, simple enough, lints doesn't check in macro.
48 // Pat is a pattern whose node
49 // is a binding which "involves" a immutable reference...
50 let PatKind::Binding(BindingMode::BindByRef(MutImmutable), ..) = pat.node,
51 // Pattern's type is a reference. Get the type and mutability of referenced value (tam: TypeAndMut).
52 let ty::TyRef(_, ref tam) = cx.tables.pat_ty(pat).sty,
53 // This is an immutable reference.
54 tam.mutbl == MutImmutable,
56 span_lint(cx, NEEDLESS_BORROWED_REFERENCE, pat.span, "this pattern takes a reference on something that is being de-referenced")