Skip to content

Start a switch expression on the line of the declaration it initializes - #76

Merged
abashev merged 2 commits into
mainfrom
switch-expression-declaration
Sep 25, 2026
Merged

abashev merged 2 commits into
mainfrom
switch-expression-declaration

Conversation

@abashev

@abashev abashev commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #63. Brings over palantir#1789 by Patrick Koenig, which fixes palantir#1258 and shipped in palantir-java-format 2.99.0.

What changes

A local variable or a field initialized with a switch expression broke after the =, with the switch indented by eight, while a plain assignment of the same switch already kept it on the line:

// before
        int x =
                switch (y) {
                    case 1 -> 1;
                    default -> throw new IllegalStateException();
                };

// after, the same shape as `x = switch (y) {` had all along
        int x = switch (y) {
            case 1 -> 1;
            default -> throw new IllegalStateException();
        };

From upstream, with the author kept

Added here

  • The upstream commit rewrote the input of test1 into the new shape, so both declarations in the golden started from the target form and nothing checked the move itself. test1's input goes back to the old shape, =, a line break and the switch, with the same expected output; a field initialized with a switch expression, written in the same old shape, is added too, since no golden covered a field. Both fail on main.

Checked

  • The golden fails on main and passes with the change: assignment and the upstream test1 output pin the new shape, the restored test1 input and the new field pin the move from the old one.
  • ./gradlew :open-java-format:test on JDK 21: 1497 tests, all green.
  • Of the 15,747 files of the JDK 21 sources, 51 format differently: the 57 declarations initialized with a switch expression that Bring over upstream PR #1789: start a switch expression on the line of the declaration it initializes #63 counted, every one of them. The switch moves onto the = line and its body eight columns left; in 9 of the files a case body or a throw argument that had to wrap at the deeper indent now fits on one line. Nothing else changes.
  • The new output is stable on a second run, apart from MethodHandleImpl.java and Calendar.java, which change on a second run on main already, in the same way (a trailing comment after a call, and a block comment before a case).

Version

This changes the output of files that format correctly today, which the README reserves for 3.0 (#22 and #31 are in the same position). Merging is the call on that, not this PR.

pkoenig10 and others added 2 commits September 25, 2026 09:17
The upstream commit rewrote the input of test1 into the new shape, so
both inputs of the golden that declare a variable with a switch
expression already started from the target form, and the golden
checked re-indentation and stability only. Nothing started from "=", a
line break and the switch, which is what the change moves.

test1's input goes back to that shape, as it was before; its expected
output does not change. A field initialized with a switch expression,
written in the same old shape, is added as well, since no golden
covered a field.
@abashev
abashev merged commit 98a80c6 into main Sep 25, 2026
15 checks passed
@abashev
abashev deleted the switch-expression-declaration branch September 25, 2026 08:15
abashev added a commit to openjavaformat/docs that referenced this pull request Sep 25, 2026
…grate

Two more changes on the formatter's main branch move output away from
palantir-java-format 2.98.0, and Migrate lists every such difference.
openjavaformat/open-java-format#76 brings over palantir 2.99.0's one
change: a switch expression that initializes a variable or a field
starts on the declaration's line, 57 declarations in 51 files of the JDK
21 sources. openjavaformat/open-java-format#74 stops counting a comment
that opens with /*** as javadoc, so a blank line between such a banner
and the declaration after it stays: 70 blank lines in 43 files, all of
them in the source. Both join the cast fix as not yet released, and the
corpus paragraph gives their numbers.
abashev added a commit to openjavaformat/docs that referenced this pull request Sep 25, 2026
2.98.0.4 is out on GitHub, Maven Central and the Gradle Plugin Portal,
so the pages now install it. It carries the three output changes that
Migrate listed as not yet in a release: the cast fix
(openjavaformat/open-java-format#78), the switch expression on the
declaration's line (openjavaformat/open-java-format#76) and the /***
banner that no longer counts as javadoc
(openjavaformat/open-java-format#74). Each now says it applies since
2.98.0.4, and the corpus paragraph no longer calls them unreleased.

The GitHub Action page keeps 2.98.0.3 as the default of the action's
version input, because the action itself still defaults to it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bring over upstream PR #1789: start a switch expression on the line of the declaration it initializes Unexpected new line in switch assignements

2 participants