-
Notifications
You must be signed in to change notification settings - Fork 1.8k
lint #1674: replace struct name with Self when applicable
#1965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
oli-obk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some nits and a suggestion would be great.
clippy_lints/src/use_self.rs
Outdated
| if self.item_path.def == path.def && | ||
| path.segments | ||
| .last() | ||
| .expect("segments should be composed of at least 1 elemnt") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elemnt -> element
clippy_lints/src/use_self.rs
Outdated
| path.segments | ||
| .last() | ||
| .expect("segments should be composed of at least 1 elemnt") | ||
| .name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can compare name with http://manishearth.github.io/rust-internals-docs/syntax_pos/symbol/keywords/constant.SelfType.html in order to not have to do as_str and compare with a string
clippy_lints/src/use_self.rs
Outdated
| .expect("segments should be composed of at least 1 elemnt") | ||
| .name | ||
| .as_str() != "Self" { | ||
| span_lint(self.cx, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a suggestion that suggests replacing the explicit name with Self?
Grep for span_suggestion if you need any examples on how to do it. If you run into any problems don't hesitate to ask
| error: repetitive struct name usage. Use `Self` instead. | ||
| --> $DIR/use_self.rs:17:13 | ||
| | | ||
| 17 | Foo::new() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The span of this one isn't quite right. It should only point to the Foo part
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, this is a rustc bug. Everything's good
SelfType const and suggestion
|
Thanks. This one will clean up a lot of code. |
|
Thank you for the review! |
|
@montrivo this introduced some false positives. Do you want to take a crack at them? Most notably, this triggers on But it also triggers on things like |
|
Yes.
Should I add the lint to one of the groups?
What tests exactly did you run to find out about the false positives?
|
|
I already fixed the
I didn't run any, but I found them inside clippy (the An example is: struct Foo<'a>(&'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 {
Self(s)
}
// cannot replace with `Self`, because that's `Foo<'a>`
fn foo() -> Foo<'static> {
Self("foo")
}
}There are probably more issues once you have generic structs. I think the only time the suggestion should come in I think simply adding a lot of tests with various features like associated types, trait impls setting said associtated types to |
|
@oli-obk The lint also fires for |
|
Is with mod named_tuples {
struct Foo(&'static str);
impl Foo {
fn new() -> Self {
Self("foo")
}
}
} |
Hello,
This tries to implement issue #1674. Let me know if it needs modifications in order to be merged.
Cheers