rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

"Convert `match` to let-else" assist

Open jonas-schievink opened this issue 3 years ago • 2 comments

(let-else will be stable in about 6 weeks from now (Rust 1.65); we should probably not ship this assist before then)

It would be nice to have an assist to rewrite let statements with a match like the following, to their corresponding let-else form:

let val = match opt {
    Some(it) => it,
    None => return,
};

->

let Some(val) = opt else { return };

Criteria for the assist should be:

  • let statement whose initializer is a match expression
  • match has 2 arms, the first extracts data, the second has a diverging expression

jonas-schievink avatar Sep 18 '22 19:09 jonas-schievink

We should also adjust existing “convert to early return” assist to use the new syntax

matklad avatar Sep 18 '22 23:09 matklad

match has 2 arms, the first extracts data, the second has a diverging expression

Probably should handle:

let val = match opt {
    None => return,
    Some(it) => it,
};

As well.

jhgg avatar Sep 22 '22 05:09 jhgg

https://user-images.githubusercontent.com/308347/200244795-545f0465-ec0f-4d8c-8f70-5f08aea99bf0.mp4

lnicola avatar Nov 07 '22 06:11 lnicola

Ah, I just noticed this issue. It's a duplicate / subset? of #11908.

fmease avatar Nov 07 '22 13:11 fmease