]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/use_self.rs
Merge branch 'macro-use' into HEAD
[rust.git] / clippy_lints / src / use_self.rs
index 2a94d8fe9cc5104602ff83603c88fc971eff30c9..2ea4497a0d1b13aa9eb221ba7b1116c3f4fdd793 100644 (file)
@@ -1,14 +1,17 @@
-use rustc::lint::{LintArray, LateLintPass, LateContext, LintPass};
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::{declare_lint, lint_array};
+use if_chain::if_chain;
 use rustc::hir::*;
-use rustc::hir::intravisit::{Visitor, walk_path, NestedVisitorMap};
-use utils::span_lint_and_then;
+use rustc::hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
+use crate::utils::{in_macro, span_lint_and_then};
 use syntax::ast::NodeId;
 use syntax_pos::symbol::keywords::SelfType;
 
 /// **What it does:** Checks for unnecessary repetition of structure name when a
 /// replacement with `Self` is applicable.
 ///
-/// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct name
+/// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct
+/// name
 /// feels inconsistent.
 ///
 /// **Known problems:** None.
@@ -31,9 +34,9 @@
 ///     }
 /// }
 /// ```
-declare_lint! {
+declare_clippy_lint! {
     pub USE_SELF,
-    Allow,
+    pedantic,
     "Unnecessary structure name repetition whereas `Self` is applicable"
 }
 
@@ -46,20 +49,37 @@ fn get_lints(&self) -> LintArray {
     }
 }
 
+const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
+
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
-        if_let_chain!([
-            let ItemImpl(.., ref item_type, ref refs) = item.node,
-            let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node,
-        ], {
-            let visitor = &mut UseSelfVisitor {
-                item_path: item_path,
-                cx: cx,
-            };
-            for impl_item_ref in refs {
-                visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
+        if in_macro(item.span) {
+            return;
+        }
+        if_chain! {
+            if let ItemKind::Impl(.., ref item_type, ref refs) = item.node;
+            if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
+            then {
+                let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
+                let should_check = if let Some(ref params) = *parameters {
+                    !params.parenthesized && !params.args.iter().any(|arg| match arg {
+                        GenericArg::Lifetime(_) => true,
+                        GenericArg::Type(_) => false,
+                    })
+                } else {
+                    true
+                };
+                if should_check {
+                    let visitor = &mut UseSelfVisitor {
+                        item_path,
+                        cx,
+                    };
+                    for impl_item_ref in refs {
+                        visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
+                    }
+                }
             }
-        })
+        }
     }
 }
 
@@ -70,15 +90,9 @@ struct UseSelfVisitor<'a, 'tcx: 'a> {
 
 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
     fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
-        if self.item_path.def == path.def &&
-           path.segments
-            .last()
-            .expect("segments should be composed of at least 1 element")
-            .name != SelfType.name() {
+        if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfType.name() {
             span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
-                db.span_suggestion(path.span,
-                                   "use the applicable keyword",
-                                   "Self".to_owned());
+                db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
             });
         }