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

False positive in SwitchDefault in Java 21 CaseLabelTree #4266

Closed
oxkitsune opened this issue Jan 28, 2024 · 2 comments · Fixed by #4299
Closed

False positive in SwitchDefault in Java 21 CaseLabelTree #4266

oxkitsune opened this issue Jan 28, 2024 · 2 comments · Fixed by #4299

Comments

@oxkitsune
Copy link
Contributor

There's a false positive in the SwitchDefault checker when using Java 21's pattern matching in switch cases.

Simple example

sealed interface Foo {
  final class Bar implements Foo {}
}

class Test {
  void f(Foo i) {
    switch (i) {
      case Foo.Bar bar -> {}
    }
  }
}

This results in:

warning: [SwitchDefault] The default case of a switch should appear at the end of the last statement group
            case Foo.Bar bar -> {}
            ^
    (see https://errorprone.info/bugpattern/SwitchDefault)
  Did you mean to remove this line?

This happens because of the way default cases are detected in the SwitchDefault checker at Line 47. CaseTree#getExpression() will return null for the default case, but also for the new CaseLabelTree cases that are used for the pattern matching.

In JDK21 there's CaseTree#getLabels() which will return a list containing a DefaultCaseLabelTree, which could be used to differentiate between these cases.
I haven't been able to find a reliable method to differentiate between these two without relying on JDK21 features. Perhaps we could do some naive string checks? E.g.

Optional<? extends CaseTree> maybeDefault =
    tree.getCases().stream()
        .filter(c -> state.getSourceForNode(c).contains("default"))
        .findAny();

This example will need to handle the different syntax for CaseKind.RULE and CaseKind.STATEMENT ofcourse.

@oxkitsune oxkitsune changed the title False positive in SwitchDefault on Java 21 pattern matching. False positive in SwitchDefault in Java 21 pattern matching Jan 28, 2024
@oxkitsune oxkitsune changed the title False positive in SwitchDefault in Java 21 pattern matching False positive in SwitchDefault in Java 21 CaseLabelTree Jan 28, 2024
@rickie
Copy link
Contributor

rickie commented Feb 22, 2024

@cushon Do you have an opinion / suggestion on how to best solve this one?

We also have problems with this one internally.

@cushon
Copy link
Collaborator

cushon commented Feb 22, 2024

I'm working on a fix for this, thanks for the report.

The tentative plan is to use reflection to call CaseTree#getLabels(), we're already using reflection to access other new APIs related to the changes to switch.

copybara-service bot pushed a commit that referenced this issue Feb 22, 2024
And consolidate some reflective workarounds for switch API changes.

Fixes #4266

PiperOrigin-RevId: 609438925
copybara-service bot pushed a commit that referenced this issue Feb 22, 2024
And consolidate some reflective workarounds for switch API changes.

Fixes #4266

PiperOrigin-RevId: 609438925
Stephan202 pushed a commit to PicnicSupermarket/error-prone that referenced this issue Mar 4, 2024
And consolidate some reflective workarounds for switch API changes.

Fixes google#4266

PiperOrigin-RevId: 609484284

(cherry picked from commit f768b0b)
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.

3 participants