]> git.lizzy.rs Git - rust.git/commitdiff
lint #1674: replace struct name with `Self` when applicable
authorTim Nielens <tim.nielens@gmail.com>
Fri, 18 Aug 2017 17:42:26 +0000 (19:42 +0200)
committerTim Nielens <tim.nielens@gmail.com>
Fri, 18 Aug 2017 17:46:50 +0000 (19:46 +0200)
SelfType const and suggestion

clippy_lints/src/use_self.rs
tests/ui/use_self.stderr

index 6344c25b0841011c065d4464936c46a43615ac4d..2a94d8fe9cc5104602ff83603c88fc971eff30c9 100644 (file)
@@ -1,8 +1,9 @@
 use rustc::lint::{LintArray, LateLintPass, LateContext, LintPass};
 use rustc::hir::*;
 use rustc::hir::intravisit::{Visitor, walk_path, NestedVisitorMap};
-use utils::span_lint;
+use utils::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.
@@ -33,7 +34,7 @@
 declare_lint! {
     pub USE_SELF,
     Allow,
-    "Repetitive struct name usage whereas `Self` is applicable"
+    "Unnecessary structure name repetition whereas `Self` is applicable"
 }
 
 #[derive(Copy, Clone, Default)]
@@ -72,13 +73,13 @@ 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 elemnt")
-            .name
-            .as_str() != "Self" {
-            span_lint(self.cx,
-                      USE_SELF,
-                      path.span,
-                      "repetitive struct name usage. Use `Self` instead.");
+            .expect("segments should be composed of at least 1 element")
+            .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());
+            });
         }
 
         walk_path(self, path);
index 1529978f2b378d18a4abe4e9573d14de0a3271fd..0cbd574b506401e875482f78ab919dd716ed201a 100644 (file)
@@ -1,40 +1,40 @@
-error: repetitive struct name usage. Use `Self` instead.
+error: unnecessary structure name repetition
   --> $DIR/use_self.rs:13:21
    |
 13 |         fn new() -> Foo {
-   |                     ^^^
+   |                     ^^^ help: use the applicable keyword: `Self`
    |
    = note: `-D use-self` implied by `-D warnings`
 
-error: repetitive struct name usage. Use `Self` instead.
+error: unnecessary structure name repetition
   --> $DIR/use_self.rs:14:13
    |
 14 |             Foo {}
-   |             ^^^
+   |             ^^^ help: use the applicable keyword: `Self`
 
-error: repetitive struct name usage. Use `Self` instead.
+error: unnecessary structure name repetition
   --> $DIR/use_self.rs:16:22
    |
 16 |         fn test() -> Foo {
-   |                      ^^^
+   |                      ^^^ help: use the applicable keyword: `Self`
 
-error: repetitive struct name usage. Use `Self` instead.
+error: unnecessary structure name repetition
   --> $DIR/use_self.rs:17:13
    |
 17 |             Foo::new()
-   |             ^^^^^^^^
+   |             ^^^^^^^^ help: use the applicable keyword: `Self`
 
-error: repetitive struct name usage. Use `Self` instead.
+error: unnecessary structure name repetition
   --> $DIR/use_self.rs:22:25
    |
 22 |         fn default() -> Foo {
-   |                         ^^^
+   |                         ^^^ help: use the applicable keyword: `Self`
 
-error: repetitive struct name usage. Use `Self` instead.
+error: unnecessary structure name repetition
   --> $DIR/use_self.rs:23:13
    |
 23 |             Foo::new()
-   |             ^^^^^^^^
+   |             ^^^^^^^^ help: use the applicable keyword: `Self`
 
 error: aborting due to 6 previous errors