Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Convert match to let-else" assist #13254

Closed
Tracked by #86
jonas-schievink opened this issue Sep 18, 2022 · 4 comments · Fixed by #13516
Closed
Tracked by #86

"Convert match to let-else" assist #13254

jonas-schievink opened this issue Sep 18, 2022 · 4 comments · Fixed by #13516
Labels
A-assists C-feature Category: feature request

Comments

@jonas-schievink
Copy link
Contributor

(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 jonas-schievink added A-assists C-feature Category: feature request labels Sep 18, 2022
@matklad
Copy link
Member

matklad commented Sep 18, 2022

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

@jhgg
Copy link
Contributor

jhgg commented Sep 22, 2022

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.

@bors bors closed this as completed in d90cb1e Nov 1, 2022
bors added a commit that referenced this issue Nov 2, 2022
…-return-assist, r=jonas-schievink

Use let-else statements in `Convert to guarded return` assist

Follow up for #13516, addresses remaining part of #13254 (comment)
@lnicola
Copy link
Member

lnicola commented Nov 7, 2022

convert-match-to-let-else.mp4

@fmease
Copy link
Member

fmease commented Nov 7, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists C-feature Category: feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants