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

UnnecessaryDefaultInEnumSwitch: NPE with default case in switch expression #2029

Closed
pkoenig10 opened this issue Dec 18, 2020 · 0 comments · Fixed by #2081
Closed

UnnecessaryDefaultInEnumSwitch: NPE with default case in switch expression #2029

pkoenig10 opened this issue Dec 18, 2020 · 0 comments · Fixed by #2081

Comments

@pkoenig10
Copy link
Contributor

Description of the problem / feature request:

When using a switch expression with an unnecessary default case, UnnecessaryDefaultInEnumSwitch throws a NullPointerException.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

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

This results in the following error:

An unhandled exception was thrown by the Error Prone static analysis plugin.
     Please report this at https://github.com/google/error-prone/issues/new and include the following:
  
     error-prone version: unknown version
     BugPattern: UnnecessaryDefaultInEnumSwitch
     Stack Trace:
     java.lang.NullPointerException: Cannot invoke "java.util.List.isEmpty()" because "defaultStatements" is null
  	at com.google.errorprone.bugpatterns.UnnecessaryDefaultInEnumSwitch.trivialDefault(UnnecessaryDefaultInEnumSwitch.java:218)
  	at com.google.errorprone.bugpatterns.UnnecessaryDefaultInEnumSwitch.fixDefault(UnnecessaryDefaultInEnumSwitch.java:111)
  	at com.google.errorprone.bugpatterns.UnnecessaryDefaultInEnumSwitch.matchSwitch(UnnecessaryDefaultInEnumSwitch.java:102)
  	at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:450)

This is caused by the fact that CaseTree#getStatements returns null for cases with CaseKind.RULE.

private Description fixDefault(
SwitchTree switchTree, CaseTree caseBeforeDefault, CaseTree defaultCase, VisitorState state) {
List<? extends StatementTree> defaultStatements = defaultCase.getStatements();

private Description fixUnrecognized(
SwitchTree switchTree, CaseTree defaultCase, VisitorState state) {
List<? extends StatementTree> defaultStatements = defaultCase.getStatements();

The logic in UnnecessaryDefaultInEnumSwitch needs to be updated to support cases with CaseKind.RULE, where callers need to use CaseTree#getBody instead.

There's also some required work to correctly handle case labels with multiple expressions, similar to what #2026 does for MissingCasesInEnumSwitch.

What version of Error Prone are you using?

2.4.0

copybara-service bot pushed a commit that referenced this issue Jan 8, 2021
copybara-service bot pushed a commit that referenced this issue Jan 8, 2021
copybara-service bot pushed a commit that referenced this issue Jan 8, 2021
copybara-service bot pushed a commit that referenced this issue Jan 11, 2021
copybara-service bot pushed a commit that referenced this issue Jan 11, 2021
copybara-service bot pushed a commit that referenced this issue Jan 11, 2021
stevie400 pushed a commit to HubSpot/error-prone that referenced this issue Jan 15, 2021
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 a pull request may close this issue.

1 participant