Skip to content

'Fill match arms' should work with existing match arms #3623

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

Merged
merged 3 commits into from
Mar 20, 2020

Conversation

slyngbaek
Copy link
Contributor

Addresses #3039

This essentially adds missing match arms. The algorithm for this
can get complicated rather quickly so bail in certain conditions
and rely on a PlaceholderPat.

The algorighm works as such:

  • Iterate through the Enum Def Variants

  • Attempt to see if the variant already exists as a match arm

  • If yes, skip the enum variant. If no, include it.

  • If it becomes complicated, rather than exhaustively deal with every
    branch, mark it as a "partial match" and simply include the
    placeholder.

    Conditions for "complication":

    • The match arm contains a match guard
    • Any kind of nested destrucuring

Order the resulting merged match branches as such:

  1. Provided match arms
  2. Missing enum variant branch arms
  3. End with Placeholder if required
  • Add extra tests

Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this is a nice step forward, I really like that now the asssist works on tuple pats for some cases (a bit weird that not for all of them, I've pointed out the cases below).

One funny thing that we might want to avoid is not to add an unreacheable pats like in this case:

Before:

fn main() {
    match Some((22, 33)) {
        None => (),
        Some(i) => (),
    }
}

After:

fn main() {
    match Some((22, 33)) {
        None => (),
        Some(i) => (),
        _ => (),
    }
}

Feels like the assist should not be proposed at all for the cases like this.

@matklad
Copy link
Member

matklad commented Mar 18, 2020

Status: we should remove _, and we should add a test that the assist does not trigger if all branches are already covered.

@matklad
Copy link
Member

matklad commented Mar 18, 2020 via email

@matklad
Copy link
Member

matklad commented Mar 19, 2020

bors r+

Thanks!

@matklad
Copy link
Member

matklad commented Mar 19, 2020

Ah, looks like this needs a rebase now

Addresses rust-lang#3039

This essentially adds missing match arms. The algorithm for this
can get complicated rather quickly so bail in certain conditions
and rely on a PlaceholderPat.

The algorighm works as such:
- Iterate through the Enum Def Variants
- Attempt to see if the variant already exists as a match arm
- If yes, skip the enum variant. If no, include it.
- If it becomes complicated, rather than exhaustively deal with every
  branch, mark it as a "partial match" and simply include the
  placeholder.

  Conditions for "complication":
  - The match arm contains a match guard
  - Any kind of nested destrucuring

Order the resulting merged match branches as such:
1. Provided match arms
2. Missing enum variant branch arms
3. End with Placeholder if required

- Add extra tests
Simplify the logic a lot by removing the check for a placeholder pat.
This means the auto-fill no longer returns a compile-able value.
@matklad
Copy link
Member

matklad commented Mar 20, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Mar 20, 2020

@bors bors bot merged commit 467a040 into rust-lang:master Mar 20, 2020
bors added a commit that referenced this pull request Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants