Skip to content

Bring over upstream PR #1789: start a switch expression on the line of the declaration it initializes #63

Description

@abashev

What

palantir/palantir-java-format#1789 by @pkoenig10 keeps a switch expression on the line of the declaration it initializes. Today a local variable or a field initialized with a switch expression breaks after the =, and the switch is indented by eight; a plain assignment already keeps the switch on its line. It closes upstream #1258, is 4 files and +19 −28, was merged on 2026-09-23, the day it was opened, and the release notes of palantir-java-format 2.99.0 (2026-09-23) list it as the release's one improvement.

Measured here with a jar of main (a079cbb, whose formatter code is unchanged since 86cdc74) on the PR's ExpressionSwitch.input: the fork's output is byte for byte the .output the PR replaces, and formatting it again changes nothing.

// today: a declaration
        int x =
                switch (y) {
                    case 1 -> 1;
                    case 2 -> throw new IllegalArgumentException();
                    default -> throw new IllegalStateException();
                };

// with #1789
        int x = switch (y) {
            case 1 -> 1;
            case 2 -> throw new IllegalArgumentException();
            default -> throw new IllegalStateException();
        };

// today and with #1789: a plain assignment
        label = switch (y) {
            case 1 -> "one";
            default -> "many";
        };

The change: declareOne in JavaInputAstVisitor loses its special case for an initializer that starts with switch (a plain level, marked TODO(fawind): Don't break switch expression assignment), so the initializer takes the same breakOnlyIfInnerLevelsThenFitOnOneLine level as any other; and visitCase in Java14InputAstVisitor drops the forcedBreak(minusTwo) after a rule case's ;. The PR's testdata moves two methods of ExpressionSwitch.

Why not in 2.x

It changes the output of files that format correctly today, which the 2.x line promises not to do: the README keeps the output of palantir-java-format 2.98.0 except for bug fixes, and other output changes wait for 3.0.

How much: in the 15,747 files of the JDK 21 sources, = switch ( occurs 108 times outside comments.

Shape Occurrences Files Moves with palantir#1789
declaration, int x = switch (y) { 57 51 all of them
plain assignment, x = switch (y) { 51 46 none, already on one line

The counts are a grep; the corpus was not formatted.

To decide first

How

Found by the daily upstream check on 2026-09-24.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions