Skip to content

Cannot format switch with case null, default -> #1008

Description

@djavanci

I created a small gradle project in IntelliJ, configured the plugin to use last toolVersion (1.18.1), gradle 8.5 and java 21. In the project I have only the Main and I added the following:

  public static String useSwitch(final Integer value) {
    return switch (value) {
      case 0 -> "it was 0";
      case 1 -> "it was 1";
      case 2 -> "it was 2";
      case null, default -> "unexpected value";
    };
  }

  public static void main(String[] args) {

    System.out.println("Hello world!");

    final String retStr = useSwitch(Integer.valueOf(args[1]));
    System.out.println("retStr = " + retStr);
  }

When I run in terminal gradle goJF it just says that

Detected Java syntax errors in the following files (you can exclude them from this task, see "https://github.com/sherter/google-java-format-gradle-plugin" for details):

<...>/TestSwitch/src/main/java/com/switchtest/Main.java

If I change the switch to have null and default in different lines, like below

      case null -> "null value";
      default -> "unexpected value";

it works.
I'm attaching the project here, for easier understanding.

TestSwitch.tgz

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions