lint #1674: replace struct name with Self when applicable

SelfType const and suggestion
This commit is contained in:
Tim Nielens 2017-08-18 19:42:26 +02:00
parent 7759bd6111
commit 7aebe3a690
2 changed files with 22 additions and 21 deletions

View 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 @@ use syntax::ast::NodeId;
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 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
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);

View 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