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

Java 17 Pattern Matching for Switch (JEP-406) compatibility issue #684

Closed
mcwhitak opened this issue Nov 9, 2021 · 2 comments
Closed

Java 17 Pattern Matching for Switch (JEP-406) compatibility issue #684

mcwhitak opened this issue Nov 9, 2021 · 2 comments

Comments

@mcwhitak
Copy link

mcwhitak commented Nov 9, 2021

With version 1.12.0 google-java-format does not properly recognize imports utilized only during pattern matched switches.

As an example, for the following hierarchy

package example.model;

public sealed interface SealedInterface {}

public non-sealed interface TypeA {}
public non-sealed interface TypeB {}

and a Java file using this hierarchy with a pattern matched switch

package example;

import example.model.SealedInterface;
import example.model.TypeA;
import example.model.TypeB;

public class Main {
  public void apply(SealedInterface sealedInterface) {
    switch(sealedInterface) {
      case TypeA a -> System.out.println("A!");
      case TypeB b -> System.out.println("B!");
    }
  }
}

google-java-format will flag import example.model.TypeA and import example.model.TypeB as unused and candidates for removal.

I've recreated this bug in a small repo that illustrates the issue.

@lorenzleutgeb
Copy link

lorenzleutgeb commented Nov 19, 2021

This appears to be a duplicate of #683 (which turns out to not only apply when throwing exceptions).

copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

#683
#684

PiperOrigin-RevId: 411074295
copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

#683
#684

PiperOrigin-RevId: 411074295
@cushon
Copy link
Collaborator

cushon commented Nov 19, 2021

This is fixed now: the formatter didn't support pattern matching in switches on Java 17 at all, and there was a separate issue specifically about unused imports. Thanks for the report!

@cushon cushon closed this as completed Nov 19, 2021
copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

#683
#684

PiperOrigin-RevId: 411074295
copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

#683
#684

PiperOrigin-RevId: 411146137
fawind pushed a commit to palantir/palantir-java-format that referenced this issue Jan 7, 2022
fawind pushed a commit to palantir/palantir-java-format that referenced this issue Jan 7, 2022
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

google/google-java-format#683
google/google-java-format#684

PiperOrigin-RevId: 411146137
fawind pushed a commit to palantir/palantir-java-format that referenced this issue Jan 10, 2022
fawind pushed a commit to palantir/palantir-java-format that referenced this issue Jan 10, 2022
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

google/google-java-format#683
google/google-java-format#684

PiperOrigin-RevId: 411146137
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

No branches or pull requests

3 participants