Skip to content

Formatter fails when complex case is part of switch expression #922

@wk-tw

Description

@wk-tw

Environment :

  • Java 17 Preview
  • Gradle 7.6
  • SpotLess plugin version : id("com.diffplug.spotless") version "6.15.0"

SpotLess configuration :

   spotless {
        java {
            googleJavaFormat("1.15.0")
        }
    }

Issue with complex case :

case ReachableByEmail c && (c instanceof ReachableByPhone)

public class PurchaseService {

  String postPurchaseOperation(Customer customer) {
    return switch (customer) {
      case ReachableByEmail c && (c instanceof ReachableByPhone) ->
          String.format("Send message on both email %s and phone number %s",
              c.getEmail(),
              ((ReachableByPhone) c).getPhoneNumber());
      case ReachableByEmail customerWithEmail -> "Send message to his address email : "
          + customerWithEmail.getEmail();
      case ReachableByPhone customerWithPhone -> "Send message to his phone number : "
          + customerWithPhone.getPhoneNumber();
      default -> "Not reachable";
    };
  }
}

Thrown errors

Caused by: com.google.googlejavaformat.java.FormatterException: 8:13: error: expected token: '&'; generated ( instead and java.lang.reflect.InvocationTargetException

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions