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

MissingCasesInEnumSwitch handles case labels with multiple expressions #2026

Closed
wants to merge 1 commit into from

Conversation

pkoenig10
Copy link
Contributor

@pkoenig10 pkoenig10 commented Dec 18, 2020

MissingCasesInEnumSwitch does not correctly handle multi-expression case labels in switch expressions. It uses the deprecated CaseTree#getExpression method, which only returns the first expression in the case label.

A simple example is shown below:

enum Case { ONE, TWO }
void m(Case c) {
  switch (c) {
    case ONE, TWO -> {}
  }
}

This code will produce the following error:

[MissingCasesInEnumSwitch] Non-exhaustive switch; either add a default or handle the remaining cases: TWO

This PR updates MissingCasesInEnumSwitch to instead use the non-deprecated CaseTree#getExpressions method, which returns all expressions in the case label.

Unfortunately this requires using reflection because the CaseTree#getExpressions method was not added until Java 12.

@google-cla google-cla bot added the cla: yes label Dec 18, 2020
@pkoenig10 pkoenig10 changed the title Correctly handle case labels with multiple expressions MissingCasesInEnumSwitch handles case labels with multiple expressions Dec 18, 2020
@pkoenig10 pkoenig10 deleted the switch branch January 4, 2021 20:56
@pkoenig10
Copy link
Contributor Author

@cushon Any plans to cut a new release soon? I'm eager to pick up this fix.

See also #1415.

@cushon
Copy link
Collaborator

cushon commented Jan 6, 2021

@pkoenig10 I'm planning to do one soon (like in the next week or so): #2061

stevie400 pushed a commit to HubSpot/error-prone that referenced this pull request Jan 15, 2021
`MissingCasesInEnumSwitch` does not correctly handle multi-expression case labels in switch expressions. It uses the deprecated `CaseTree#getExpression` method, which only returns the first expression in the case label.

A simple example is shown below:
```
enum Case { ONE, TWO }
void m(Case c) {
  switch (c) {
    case ONE, TWO -> {}
  }
}
```

This code will produce the following error:
```
[MissingCasesInEnumSwitch] Non-exhaustive switch; either add a default or handle the remaining cases: TWO
```

This PR updates `MissingCasesInEnumSwitch` to instead use the non-deprecated `CaseTree#getExpressions` method, which returns all expressions in the case label.

Unfortunately this requires using reflection because the `CaseTree#getExpressions` method was not added until Java 12.

Fixes google#2026

COPYBARA_INTEGRATE_REVIEW=google#2026 from pkoenig10:switch 5e22d7b
PiperOrigin-RevId: 348534859
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants