Skip to content

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

Merged
merged 5 commits into from
Jan 22, 2019
Merged

Fix automatic suggestion on use_self. #3679

merged 5 commits into from
Jan 22, 2019

Conversation

daxpedda
Copy link
Contributor

In an example like this:

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:

@@ -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);
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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.

@detrumi
Copy link
Member

detrumi commented Jan 20, 2019

I think you can do the same thing for those two other paths.
Also, in case this fixes all wrong suggestions in the test file, you can add // run-rustfix to the top of the test file and add the .fixed file.

@phansch phansch added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Jan 21, 2019
Copy link
Member

@flip1995 flip1995 left a 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?

@daxpedda
Copy link
Contributor Author

And does adding // run-rustfix to the top of the test file work?

Added it now.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 22, 2019

@bors r+

Thanks!

@bors
Copy link
Contributor

bors commented Jan 22, 2019

📌 Commit 42d5a07 has been approved by oli-obk

@bors
Copy link
Contributor

bors commented Jan 22, 2019

⌛ Testing commit 42d5a07 with merge b0b6134...

bors added a commit that referenced this pull request Jan 22, 2019
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
@bors
Copy link
Contributor

bors commented Jan 22, 2019

💔 Test failed - checks-travis

@flip1995
Copy link
Member

@bors rollup

flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Jan 22, 2019
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
bors added a commit that referenced this pull request Jan 22, 2019
Rollup of 4 pull requests

Successful merges:

 - #3582 (Add assert(true) and assert(false) lints)
 - #3679 (Fix automatic suggestion on `use_self`.)
 - #3684 ("make_return" and "blockify" convenience methods, fixes #3683)
 - #3685 (Rustup)

Failed merges:

r? @ghost
@bors
Copy link
Contributor

bors commented Jan 22, 2019

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Jan 22, 2019

📌 Commit 42d5a07 has been approved by phansch

@bors
Copy link
Contributor

bors commented Jan 22, 2019

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Jan 22, 2019

📌 Commit 42d5a07 has been approved by phansch

@bors
Copy link
Contributor

bors commented Jan 22, 2019

⌛ Testing commit 42d5a07 with merge a40d8e4...

bors added a commit that referenced this pull request Jan 22, 2019
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
@bors
Copy link
Contributor

bors commented Jan 22, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: phansch
Pushing a40d8e4 to master...

@bors bors merged commit 42d5a07 into rust-lang:master Jan 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants