]> git.lizzy.rs Git - rust.git/commitdiff
Improve needless_borrowed_ref and add suggestion to it.
authorBenoît CORTIER <benoit.cortier@fried-world.eu>
Sat, 1 Jul 2017 10:01:39 +0000 (12:01 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Mon, 21 Aug 2017 12:00:56 +0000 (14:00 +0200)
clippy_lints/src/needless_borrowed_ref.rs

index 0167b171297150a7a3b835a85846e3477e52f029..db2c2cc95f86f9b79f3fee0cc3b2a81ddf4af4bc 100644 (file)
@@ -3,9 +3,14 @@
 //! This lint is **warn** by default
 
 use rustc::lint::*;
+<<<<<<< HEAD
 use rustc::hir::{MutImmutable, Pat, PatKind, BindingAnnotation};
+=======
+use rustc::hir::{MutImmutable, Pat, PatKind};
+>>>>>>> e30bf721... Improve needless_borrowed_ref and add suggestion to it.
 use rustc::ty;
-use utils::{span_lint, in_macro};
+use utils::{span_lint_and_then, in_macro, snippet};
+use syntax_pos::{Span, BytePos};
 
 /// **What it does:** Checks for useless borrowed references.
 ///
@@ -53,9 +58,18 @@ fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
             // Pattern's type is a reference. Get the type and mutability of referenced value (tam: TypeAndMut).
             let ty::TyRef(_, ref tam) = cx.tables.pat_ty(pat).sty,
             // Only lint immutable refs, because `&mut ref T` may be useful.
-            tam.mutbl == MutImmutable,
+            let PatKind::Ref(ref sub_pat, MutImmutable) = pat.node,
+
+            // Check sub_pat got a 'ref' keyword.
+            let ty::TyRef(_, _) = cx.tables.pat_ty(sub_pat).sty,
         ], {
-            span_lint(cx, NEEDLESS_BORROWED_REFERENCE, pat.span, "this pattern takes a reference on something that is being de-referenced")
+            let part_to_keep = Span{ lo: pat.span.lo + BytePos(5), hi: pat.span.hi, ctxt: pat.span.ctxt };
+            span_lint_and_then(cx, NEEDLESS_BORROWED_REFERENCE, pat.span,
+                               "this pattern takes a reference on something that is being de-referenced",
+                               |db| {
+                                   let hint = snippet(cx, part_to_keep, "..").into_owned();
+                                   db.span_suggestion(pat.span, "try removing the `&ref` part and just keep", hint);
+                               });
         }}
     }
 }