Skip to content

[2.9.7] Switch expression: enum constant case labels are wrapped in parentheses, producing non-compiling code #946

Closed

Description

@ArLau

Environment

  • prettier-plugin-java: 2.10.0
  • prettier: 3.8.3
  • JDK used to compile output: 21.0.10

Execution

"format": "prettier \"**/*.(yaml|yml|md|ts|tsx|java)\" --write"

Description

When formatting a switch expression whose case labels are bare enum constants (constant case labels), the plugin wraps each label in parentheses, e.g. case BOOLEAN -> becomes case (BOOLEAN) ->.

This output does not compile: Fehler: Symbol nicht gefunden case (BOOLEAN) -> value -> writeBoolean((boolean) value);

This worked correctly before 2.9.7 (tested in 2.8.1) and is still broken in 2.10.0 (tested).

Input

public enum Type {
    BOOLEAN,
    DOUBLE,
    LONG,
    NULL,
    STRING,
}

private interface Writer {
   void write(Object value) throws IOException;
}

private Writer getRawValueWriter(final Type type) {
  return switch (type) {
    case BOOLEAN -> value -> writeBoolean((boolean) value);
    case DOUBLE -> value -> writeDouble((double) value);
    case LONG -> value -> writeLong((long) value);
    case NULL -> ignored -> writeNull();
    case STRING -> value -> writeString((String) value);
  };
}

Actual output (does not compile)

private Writer getRawValueWriter(final Type type) {
  return switch (type) {
    case (BOOLEAN) -> value -> writeBoolean((boolean) value);
    case (DOUBLE) -> value -> writeDouble((double) value);
    case (LONG) -> value -> writeLong((long) value);
    case (NULL) -> ignored -> writeNull();
    case (STRING) -> value -> writeString((String) value);
  };
}

Expected output

Identical to the input — constant case labels should not be parenthesized.

Activity

  1. ArLau commented on Jun 26, 2026

    @ArLau
    Author

    @jtkiesel Thx for the quick fix!

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