rust-lang / rust-lang/rust-analyzer
Extract Function assist should use the correct type for self parameters
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
rust-analyzer version: (eg. output of "rust-analyzer: Show RA Version" command, accessible in VSCode via Ctrl/⌘+Shift+P)
rust-analyzer version: 0.4.2450-standalone (d30deb58fe 2025-05-05) [/Users/joshka/.vscode/extensions/rust-lang.rust-analyzer-0.4.2450-darwin-arm64/server/rust-analyzer]
rustc version: (eg. output of rustc -V)
rustc 1.86.0 (05f9846f8 2025-03-31)
editor or extension: (eg. VSCode, Vim, Emacs, etc. For VSCode users, specify your extension version; for users of other editors, provide the distribution if applicable)
VSCode
0.4.2450
relevant settings: (eg. client settings, or environment variables like CARGO, RUSTC, RUSTUP_HOME or CARGO_HOME)
repository link (if public, optional): (eg. rust-analyzer)
code snippet to reproduce:
struct Foo {
x: i32,
y: i32,
}
impl Foo {
fn bar(mut self) {
self.x = 42;
self.y = 42;
}
}
Extracting the self.x line to a function gives the following code:
fn fun_name(mut self) {
self.x = 42;
}
This function should have &mut self, not mut self
Similarly:
impl Foo {
fn bar(self) {
dbg!(self.x);
dbg!(self.y);
}
}
Extracts:
fn fun_name(self) {
dbg!(self.x);
}
Which should be &self not self.
I imagine this probably needs to:
- check if self is Copy
- check if there are further usages of self past the extract function point in the original function
- check if there are mutations of self in the extracted code or if it's just read only
Open question: I'm not sure whether if you extract a function which has no further use of self whether this should be a ref or not. (I'd guess it's probably reasonable to make it a ref, but I'm not sure of if there are some edge cases there)
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
Reproduce the Extract Function assist with the provided Foo examples and inspect the assist entry point that determines captured self parameters. Compare mutable and read-only uses, including later uses of self, and verify that the extracted function uses the appropriate self reference without changing unrelated extraction behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100