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
Nobody has claimed this yet.
- 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:
- A code action to turn a
self-taking method into a static method that takes athis: &Selfparameter. - 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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