palantir / palantir-java-format

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

Poor formatting around expression switches #1025

Closed marquiswang closed 3 months ago

marquiswang commented 4 months ago

What happened?

Pretty interesting, seems to only happen when the expression is assigned to a variable that was already declared.

    public void test1(int y) {
        int x =
                switch (y) {
                    case 1 -> 1;
                    case 2 -> throw new IllegalArgumentException();
                    default -> throw new IllegalStateException();
                };
    }

    public void test2(int y) {
        int x;
        x = switch (y) {
            case 1 -> 1;
            case 2 -> throw new IllegalArgumentException();
            default -> throw new IllegalStateException();};
    }

What did you want to happen?

Probably this?

    public void test2(int y) {
        int x;
        x = switch (y) {
            case 1 -> 1;
            case 2 -> throw new IllegalArgumentException();
            default -> throw new IllegalStateException();
        };
    }