rust-lang / rust-lang/rust-analyzer

Feature request: convert tuple to a struct by creating a new struct type

Open
#13,179 5 comments 4 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

I wish rust-analyzer had a code action to refactor this:

fn numberwang() -> (f32, bool) {
   let a = 1.;
   let b = true;
   (a, b)
}
let (x,y) = numberwang();

into this:

struct Untupled {
  x: f32,
  b: bool,
}

fn numberwang() -> Untupled {
   let x = 1.;
   let y = true;
   Untupled {a, b}
}
let Untupled {a:x, b:y} = numberwang();

I tend to be lazy and overuse tuples. They're convenient when I have 2-3 values to work with, but eventually I end up with a tuple of 7 values and lose track of which field is which. It'd be great to have a code action that I can point to a tuple instance, and it will replace it with a struct type, and follow all usages of this tuple (places where it has become a compile error, not all places that use the same types together) and replace them with the struct type too.

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

Start with the requested Rust examples and define the supported tuple patterns, naming rules, and scope of usage replacement. Check how a code action could identify the tuple instance and distinguish its usages from unrelated tuples; done means the transformation is applied consistently and the resulting code compiles.

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
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.