palantir / palantir-java-format

A modern, lambda-friendly, 120 character Java formatter.
Apache License 2.0
438 stars 46 forks source link

FormatterException on combined `null, default` switch expression branch #1096

Open hennadii-datadog opened 3 months ago

hennadii-datadog commented 3 months ago

Hi team, case null, default -> is a common pattern in switch expressions (explicit null is required for compatibility with older switch statements). But it crashes the formatter, e.g. the code below results in /YourFile.java: com.palantir.javaformat.java.FormatterException: 48:13: error: expected token: 'default'; generated - instead -> [Help 1]

public void onReceive(Object message) {
    switch (message) {
        //...
        case null, default -> super.unhandled(message);
    }
}

Splitting the case into two separated cases with identical bodies helps, but it's obviously a subpar workaround because it causes static analyzer warnings + prevents switching to palantir-java-formatter without migrating all affected switches in the codebase first.

Thanks !

singron commented 3 months ago

See https://github.com/palantir/palantir-java-format/issues/952

thijmenketel commented 2 months ago

Any progress on this? I was hoping that with the release of 2.48.0 it would be fixed... Java 21 has been out for quite a while now.