lint #1674: lifetimed types exclusion

This commit is contained in:
Tim Nielens 2017-08-22 00:18:37 +02:00
parent 0acadb3c21
commit 3f575d874b
3 changed files with 40 additions and 16 deletions

View File

@ -54,6 +54,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
if_let_chain!([
let ItemImpl(.., ref item_type, ref refs) = item.node,
let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node,
let PathParameters::AngleBracketedParameters(ref angleBracketedParameterData)
= item_path.segments.last().unwrap().parameters,
angleBracketedParameterData.lifetimes.len() == 0,
], {
let visitor = &mut UseSelfVisitor {
item_path: item_path,
@ -76,12 +79,10 @@ 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 element")
.unwrap()
.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());
});
}

View File

@ -2,6 +2,7 @@
#![plugin(clippy)]
#![warn(use_self)]
#![allow(dead_code)]
#![allow(should_implement_trait)]
fn main() {}
@ -43,3 +44,25 @@ mod better {
}
}
}
//todo the lint does not handle lifetimed struct
//the following module should trigger the lint on the third method only
mod lifetimes {
struct Foo<'a>{foo_str: &'a str}
impl<'a> Foo<'a> {
// Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) -> Foo<'b>`
fn foo(s: &str) -> Foo {
Foo { foo_str: s }
}
// cannot replace with `Self`, because that's `Foo<'a>`
fn bar() -> Foo<'static> {
Foo { foo_str: "foo"}
}
// `Self` is applicable here
fn clone(&self) -> Foo<'a> {
Foo {foo_str: self.foo_str}
}
}
}

View File

@ -1,39 +1,39 @@
error: unnecessary structure name repetition
--> $DIR/use_self.rs:13:21
--> $DIR/use_self.rs:14:21
|
13 | fn new() -> Foo {
14 | fn new() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
|
= note: `-D use-self` implied by `-D warnings`
error: unnecessary structure name repetition
--> $DIR/use_self.rs:14:13
--> $DIR/use_self.rs:15:13
|
14 | Foo {}
15 | Foo {}
| ^^^ help: use the applicable keyword: `Self`
error: unnecessary structure name repetition
--> $DIR/use_self.rs:16:22
--> $DIR/use_self.rs:17:22
|
16 | fn test() -> Foo {
17 | fn test() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
error: unnecessary structure name repetition
--> $DIR/use_self.rs:17:13
--> $DIR/use_self.rs:18:13
|
17 | Foo::new()
18 | Foo::new()
| ^^^^^^^^ help: use the applicable keyword: `Self`
error: unnecessary structure name repetition
--> $DIR/use_self.rs:22:25
--> $DIR/use_self.rs:23:25
|
22 | fn default() -> Foo {
23 | fn default() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
error: unnecessary structure name repetition
--> $DIR/use_self.rs:23:13
--> $DIR/use_self.rs:24:13
|
23 | Foo::new()
24 | Foo::new()
| ^^^^^^^^ help: use the applicable keyword: `Self`
error: aborting due to 6 previous errors