rust-lang / rust-lang/rust-analyzer

Idea for a new code action: Turn `self`-taking methods into static methods with `self`'s accessed fields as parameters

Open
#20,350 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-assists C-feature
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Sometimes you need to do this to split borrows on self. Would be super neat if rust-analyzer could do it automatically.

For example, given this input:

impl Foo {
    fn whatever(&self) -> Thing {
        self.member.get_thing()
    }
}

The code action would create this output:

impl Foo {
    fn whatever(member: &TypeOfMember) -> Thing {
        member.get_thing()
    }
}

Tricky case is when the initial method calls another method on self. I guess the code action would just not be suggested in that case?

Alternatively, maybe this is really two code actions:

  1. A code action to turn a self-taking method into a static method that takes a this: &Self parameter.
  2. A code action to explode a struct parameter into the fields that the function body uses

Performing (1) followed by (2) is equivalent to my initial proposal, but both (1) and (2) could be useful in other situations as well.

What do y'all think?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the two proposed code actions and the example transformation in the issue first. Clarify whether this should be one combined action or two separate actions, and how methods that call another method on self should behave; done means an agreed scope and a tested implementation of that scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.