Skip to content

Commit 7aebe3a

Browse files
committed
lint #1674: replace struct name with Self when applicable
SelfType const and suggestion
1 parent 7759bd6 commit 7aebe3a

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

clippy_lints/src/use_self.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use rustc::lint::{LintArray, LateLintPass, LateContext, LintPass};
22
use rustc::hir::*;
33
use rustc::hir::intravisit::{Visitor, walk_path, NestedVisitorMap};
4-
use utils::span_lint;
4+
use utils::span_lint_and_then;
55
use syntax::ast::NodeId;
6+
use syntax_pos::symbol::keywords::SelfType;
67

78
/// **What it does:** Checks for unnecessary repetition of structure name when a
89
/// replacement with `Self` is applicable.
@@ -33,7 +34,7 @@ use syntax::ast::NodeId;
3334
declare_lint! {
3435
pub USE_SELF,
3536
Allow,
36-
"Repetitive struct name usage whereas `Self` is applicable"
37+
"Unnecessary structure name repetition whereas `Self` is applicable"
3738
}
3839

3940
#[derive(Copy, Clone, Default)]
@@ -72,13 +73,13 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
7273
if self.item_path.def == path.def &&
7374
path.segments
7475
.last()
75-
.expect("segments should be composed of at least 1 elemnt")
76-
.name
77-
.as_str() != "Self" {
78-
span_lint(self.cx,
79-
USE_SELF,
80-
path.span,
81-
"repetitive struct name usage. Use `Self` instead.");
76+
.expect("segments should be composed of at least 1 element")
77+
.name != SelfType.name() {
78+
span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
79+
db.span_suggestion(path.span,
80+
"use the applicable keyword",
81+
"Self".to_owned());
82+
});
8283
}
8384

8485
walk_path(self, path);

tests/ui/use_self.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
error: repetitive struct name usage. Use `Self` instead.
1+
error: unnecessary structure name repetition
22
--> $DIR/use_self.rs:13:21
33
|
44
13 | fn new() -> Foo {
5-
| ^^^
5+
| ^^^ help: use the applicable keyword: `Self`
66
|
77
= note: `-D use-self` implied by `-D warnings`
88

9-
error: repetitive struct name usage. Use `Self` instead.
9+
error: unnecessary structure name repetition
1010
--> $DIR/use_self.rs:14:13
1111
|
1212
14 | Foo {}
13-
| ^^^
13+
| ^^^ help: use the applicable keyword: `Self`
1414

15-
error: repetitive struct name usage. Use `Self` instead.
15+
error: unnecessary structure name repetition
1616
--> $DIR/use_self.rs:16:22
1717
|
1818
16 | fn test() -> Foo {
19-
| ^^^
19+
| ^^^ help: use the applicable keyword: `Self`
2020

21-
error: repetitive struct name usage. Use `Self` instead.
21+
error: unnecessary structure name repetition
2222
--> $DIR/use_self.rs:17:13
2323
|
2424
17 | Foo::new()
25-
| ^^^^^^^^
25+
| ^^^^^^^^ help: use the applicable keyword: `Self`
2626

27-
error: repetitive struct name usage. Use `Self` instead.
27+
error: unnecessary structure name repetition
2828
--> $DIR/use_self.rs:22:25
2929
|
3030
22 | fn default() -> Foo {
31-
| ^^^
31+
| ^^^ help: use the applicable keyword: `Self`
3232

33-
error: repetitive struct name usage. Use `Self` instead.
33+
error: unnecessary structure name repetition
3434
--> $DIR/use_self.rs:23:13
3535
|
3636
23 | Foo::new()
37-
| ^^^^^^^^
37+
| ^^^^^^^^ help: use the applicable keyword: `Self`
3838

3939
error: aborting due to 6 previous errors
4040

0 commit comments

Comments
 (0)