-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix automatic suggestion on use_self
.
#3679
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
clippy_lints/src/use_self.rs
Outdated
@@ -221,10 +221,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> { | |||
fn visit_path(&mut self, path: &'tcx Path, _id: HirId) { | |||
if path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfUpper.name() { | |||
if self.item_path.def == path.def { | |||
span_use_self_lint(self.cx, path); | |||
span_use_self_lint(self.cx, path.segments.first().expect(SEGMENTS_MSG).ident.span); |
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 first segment might not be the type, but a module instead. So this would replace the module name, instead of the type. Playground example
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.
rust-clippy/clippy_lints/src/use_self.rs
Lines 59 to 63 in 2e0977f
// path segments only include actual path, no methods or fields | |
let last_path_span = path.segments.last().expect(SEGMENTS_MSG).ident.span; | |
// `to()` doesn't shorten span, so we shorten it with `until(..)` | |
// and then include it with `to(..)` | |
let span = path.span.until(last_path_span).to(last_path_span); |
Not sure if putting it in span_use_self_lint
is the best idea.
If there is any cleaner way to clip path
, any guidance would be appreciated.
I also added tests to check for this, any better suggestions on naming or more tests would be nice.
I think you can do the same thing for those two other paths. |
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.
This LGTM, just the span trimming could be improved a bit
And does adding // run-rustfix
to the top of the test file work?
Co-Authored-By: daxpedda <[email protected]>
Added it now. |
@bors r+ Thanks! |
📌 Commit 42d5a07 has been approved by |
Fix automatic suggestion on `use_self`. In an example like this: ```rust impl Example { fn fun_1() { } fn fun_2() { Example::fun_1(); } } ``` Clippy tries to replace `Example::fun_1` with `Self`, loosing `::fun_1` in the process, it should rather try to replace `Example` with `Self`. **Question** - There may be other paths that need the same treatment, but I'm not sure I understand them fully: - https://github.com/rust-lang/rust-clippy/blob/e648adf0866a1cea7db6ce2d33ea86e442f25377/clippy_lints/src/use_self.rs#L94-L96 - https://github.com/rust-lang/rust-clippy/blob/e648adf0866a1cea7db6ce2d33ea86e442f25377/clippy_lints/src/use_self.rs#L225-L229
💔 Test failed - checks-travis |
@bors rollup |
Fix automatic suggestion on `use_self`. In an example like this: ```rust impl Example { fn fun_1() { } fn fun_2() { Example::fun_1(); } } ``` Clippy tries to replace `Example::fun_1` with `Self`, loosing `::fun_1` in the process, it should rather try to replace `Example` with `Self`. **Question** - There may be other paths that need the same treatment, but I'm not sure I understand them fully: - https://github.com/rust-lang/rust-clippy/blob/e648adf0866a1cea7db6ce2d33ea86e442f25377/clippy_lints/src/use_self.rs#L94-L96 - https://github.com/rust-lang/rust-clippy/blob/e648adf0866a1cea7db6ce2d33ea86e442f25377/clippy_lints/src/use_self.rs#L225-L229
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 42d5a07 has been approved by |
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 42d5a07 has been approved by |
Fix automatic suggestion on `use_self`. In an example like this: ```rust impl Example { fn fun_1() { } fn fun_2() { Example::fun_1(); } } ``` Clippy tries to replace `Example::fun_1` with `Self`, loosing `::fun_1` in the process, it should rather try to replace `Example` with `Self`. **Question** - There may be other paths that need the same treatment, but I'm not sure I understand them fully: - https://github.com/rust-lang/rust-clippy/blob/e648adf0866a1cea7db6ce2d33ea86e442f25377/clippy_lints/src/use_self.rs#L94-L96 - https://github.com/rust-lang/rust-clippy/blob/e648adf0866a1cea7db6ce2d33ea86e442f25377/clippy_lints/src/use_self.rs#L225-L229
☀️ Test successful - checks-travis, status-appveyor |
In an example like this:
Clippy tries to replace
Example::fun_1
withSelf
, loosing::fun_1
in the process, it should rather try to replaceExample
withSelf
.Question
rust-clippy/clippy_lints/src/use_self.rs
Lines 94 to 96 in e648adf
rust-clippy/clippy_lints/src/use_self.rs
Lines 225 to 229 in e648adf