rust-lang / rust-lang/rust-analyzer
“Fill match arms” for nested enums
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
Consider the following code:
enum Example {
One,
Two,
// ...and lots more, perhaps
}
pub fn example(e: Option<Example>) {
match e {
// want to fill this
}
}
Invoking “Fill match arms” on e will currently produce
Some(_) => todo!(),
None => todo!(),
However, I might want to match the inner enum, to produce:
Some(Example::One) => todo!(),
Some(Example::Two) => todo!(),
None => todo!(),
This could be expressed by putting the cursor on the _ and invoking an assist that duplicates the arm with more specific patterns. (Or, “Fill match arms” could always descend into nested enums and rely on the user to delete unwanted ones, but that could potentially lead to quite an expansion in some cases, so I don’t think it is wise.) In general, I think it would make sense to have an assist that replaces an _ pattern or identifier pattern with an enum variant (or struct) pattern, and then duplicates the arm for all variants produced this way.
A different but closely related case is that I might be in the position of currently having some of these arms but not all (hence getting a non-exhaustive match error), and wanting to fill in the missing arms:
match e {
Some(Example::One) => todo!(),
None => todo!(),
}
In this case, the error's span is e (the same place that “Fill match arms” is available), but RA doesn't offer any assist/fix that will fill in the missing arm. In this case, I think “Fill match arms” should be available and fill in the missing Some(Example::Two) arm. This part won’t require any new UI design.
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
Start by locating the existing “Fill match arms” assist and the exhaustiveness diagnostic or fix for the nested enum examples. The work is done when nested patterns can be expanded as described and incomplete matches offer the missing inner variant without requiring new UI design.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100