rust-lang / rust-lang/rust-analyzer

Discussion: Extract into function behavior w.r.t. first assignment of a variable

Open
#10,137 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Currently when extracting some code that includes a variable assignment into a function, rust-analyzer tries taking a mutable reference to that variable regardless of whether that variable was previously initialized. Use cases for having declaration separate from initialization are On-Stack Dynamic Dispatch and conditional initialization of values of "guard" types (e.g. sentry::init's return type).

In both cases if the assignment is the last statement of the code to be extracted, RA could instead make the new function return the value that is to be assigned to that variable, but there is of course no general solution here with the current language which is why I started the issue title with Discussion:. I'm curious how others think a situation like this should be handled.

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

No source file, test, or entry point is identified in the issue. Start by examining rust-analyzer's extract-into-function behavior and the examples linked in the discussion, then determine the intended behavior for assignments whose variables were or were not previously initialized. Done requires an agreed design and corresponding implementation scope.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.