rust-lang / rust-lang/rust-analyzer

Extract Function assist should use the correct type for self parameters

Open
#19,753 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-assists C-bug
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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.