rust-lang / rust-lang/rust-analyzer

Idea: Insert type names, or type-aware placeholders, in "Fill struct fields"

Open
#12,639 2 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

Right now, the “Fill struct fields” assist produces this result:

struct Foo {
    a: i32,
    b: Vec<i32>,
    c: Option<String>,
}

fn make() -> Foo {
    Foo {
        a: todo!(),            // generated code
        b: todo!(),            //
        c: todo!(),            //
    }
}

I think it would be useful if the placeholders indicated the type of the value that is needed. This would help guide filling in the blanks in situations where the value needed is obvious enough, but the type, the particular choice of representation, is not. (Maybe you're using an unfamiliar type, or maybe you just forgot which fields are Optional and which are not.)

One maybe-elegant way to do this would be to fill in todo!() arguments:

    Foo {
        a: todo!("need i32"),
        b: todo!("need Vec<i32>"),
        c: todo!("need Option<String>),
    }

It would also be frequently useful to fill in certain fields with their obvious default constructors (e.g. Option fields could be initialized with None). That is of course more hazardous since the user might forget to put in a non-placeholder value, but perhaps it could be offered as an alternative version of the assist to be used explicitly (much like creating modules offers options for foo.rs and foo/mod.rs). This would be harder, since it needs to know what idiomatic code to generate, if it doesn't resort to T::default().

    Foo {
        a: 0,
        b: vec![],
        c: None,
    }

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 by locating the rust-analyzer implementation and tests for the “Fill struct fields” assist, then inspect how it derives each field’s type. Compare the existing todo!() output with the proposed type-aware and default-constructor alternatives; done requires a decided behavior and tests covering the generated result.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.