palantir-java-format icon indicating copy to clipboard operation
palantir-java-format copied to clipboard

Formatter generates switch expression incompatible with baseline checkstyle

Open j-baker opened this issue 1 year ago • 4 comments

What happened?

The formatter will format

    public void initialize(SchedulerBackend newBackend) {
        this.backend = newBackend;
        schedulableBuilder = switch (schedulingMode) {
            case FIFO -> new ReFifoSchedulableBuilder(rootPool);
            case FAIR -> new ReFairSchedulableBuilder(rootPool, sc);
            default -> throw new SafeIllegalArgumentException(
                    "Unsupported scheduler mode", SafeArg.of("schedulingMode", schedulingMode));
        };
        schedulableBuilder.buildPools();
    }

To

    public void initialize(SchedulerBackend newBackend) {
        this.backend = newBackend;
        schedulableBuilder = switch (schedulingMode) {
            case FIFO -> new ReFifoSchedulableBuilder(rootPool);
            case FAIR -> new ReFairSchedulableBuilder(rootPool, sc);
            default -> throw new SafeIllegalArgumentException(
                    "Unsupported scheduler mode", SafeArg.of("schedulingMode", schedulingMode));};
        schedulableBuilder.buildPools();
    }

which violates checkstyle rules.

What did you want to happen?

In that specific case, the formatter should have been a no-op.

j-baker avatar Aug 02 '22 13:08 j-baker

I just hit this problem as well. The formatter produces the following (note the weird trailing curly):

        this.environmentReadStore = switch (readMode) {
            case OLD_TABLES -> new AaaaEnvironmentReadStore(
                    environmentTable, environmentRidByDeploymentIdTable, recommendationV5StatusTable);
            case NEW_TABLES -> new AaaaEnvironmentV2ReadStore(
                    environmentV2Table, environmentV2IndexTable, recommendationV5StatusTable);};

And then I get the following error:

> Task :my-project:checkstyleMain
[ant:checkstyle] [ERROR] /Volumes/git/...../FooClass.java:80:94: ';' is not followed by whitespace. [WhitespaceAfter]

This happens with gradle-palantir-java-format 2.26.0

iamdanfox avatar Sep 28 '22 18:09 iamdanfox

Adding yield keyword still has the weird dense curly:

        this.environmentReadStore = switch (readMode) {
            case OLD_TABLES -> {
                yield new AaaaEnvironmentReadStore(
                        environmentTable, environmentRidByDeploymentIdTable, recommendationV5StatusTable);
            }
            case NEW_TABLES -> {
                yield new AaaaEnvironmentV2ReadStore(
                        environmentV2Table, environmentV2IndexTable, recommendationV5StatusTable);
            }};

iamdanfox avatar Sep 28 '22 18:09 iamdanfox

This is still happening and took me by surprise when it happened, is there interest from the project in changing it?

busches avatar Mar 08 '24 02:03 busches

Looks like it was fixed in #1069

busches avatar Apr 07 '24 01:04 busches