Title: Bring over upstream PR #1789: start a switch expression on the line of the declaration it initializes · Issue #63 · openjavaformat/open-java-format · GitHub
Open Graph Title: Bring over upstream PR #1789: start a switch expression on the line of the declaration it initializes · Issue #63 · openjavaformat/open-java-format
X Title: Bring over upstream PR #1789: start a switch expression on the line of the declaration it initializes · Issue #63 · openjavaformat/open-java-format
Description: 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 i...
Open Graph Description: 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...
X Description: 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...
Opengraph URL: https://github.com/openjavaformat/open-java-format/issues/63
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bring over upstream PR #1789: start a switch expression on the line of the declaration it initializes","articleBody":"### What\n\n[palantir/palantir-java-format#1789](https://github.com/palantir/palantir-java-format/pull/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](https://github.com/palantir/palantir-java-format/issues/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.\n\nMeasured here with a jar of `main` (a079cbbc, whose formatter code is unchanged since 86cdc74e) 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.\n\n```java\n// today: a declaration\n int x =\n switch (y) {\n case 1 -> 1;\n case 2 -> throw new IllegalArgumentException();\n default -> throw new IllegalStateException();\n };\n\n// with #1789\n int x = switch (y) {\n case 1 -> 1;\n case 2 -> throw new IllegalArgumentException();\n default -> throw new IllegalStateException();\n };\n\n// today and with #1789: a plain assignment\n label = switch (y) {\n case 1 -> \"one\";\n default -> \"many\";\n };\n```\n\nThe 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`.\n\n### Why not in 2.x\n\nIt 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.\n\nHow much: in the 15,747 files of the JDK 21 sources, `= switch (` occurs 108 times outside comments.\n\n| Shape | Occurrences | Files | Moves with #1789 |\n| --- | --- | --- | --- |\n| declaration, `int x = switch (y) {` | 57 | 51 | all of them |\n| plain assignment, `x = switch (y) {` | 51 | 46 | none, already on one line |\n\nThe counts are a grep; the corpus was not formatted.\n\n### To decide first\n\n- **Do we want it?** A declaration and an assignment of the same switch come out differently today, and the difference says nothing to a reader. Under the manifesto it is decided once for everyone: no option, no flag.\n- **Which release.** The same version-number question as #22 and #31: it goes with the other output changes waiting for 3.0.\n- **Whether to wait for the next upstream sync.** It is merged and released upstream, so the next sync brings it anyway; carrying it alone only matters if 3.0 ships before that sync.\n\n### How\n\n- Cherry-pick with the original author, as in #22 and #31: the fork shares history with upstream, so rename detection maps `palantir-java-format/` to `open-java-format/`. Not tried here.\n- The fork's `ExpressionSwitch.output` golden moves with it, in the same two methods as upstream's.\n\nFound by the daily upstream check on 2026-09-24.\n","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-09-24T14:25:44Z","url":"https://github.com/openjavaformat/open-java-format/issues/63"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ee11710e-de2a-dc26-bd3e-38294362d88e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 81DC:20E5B9:5F4F64:7F29F5:6AFBC731 |
| html-safe-nonce | 232a0561e2f791b13e9384cc09f56a224ff500e4f4f5180e0f3512b611b740a7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MURDOjIwRTVCOTo1RjRGNjQ6N0YyOUY1OjZBRkJDNzMxIiwidmlzaXRvcl9pZCI6IjQ2NDcwOTEwNjY3MDQ4NzM0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 58f44bacb583b8ee55f9b4700ce9484112bb9258973f5697f82a8bc17cb5a763 |
| hovercard-subject-tag | issue:5569838970 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/openjavaformat/open-java-format/63/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c2555774c17adb2749b57a92637ac081b01cc6230794a217249ab1d1602d003c/openjavaformat/open-java-format/issues/63 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c2555774c17adb2749b57a92637ac081b01cc6230794a217249ab1d1602d003c/openjavaformat/open-java-format/issues/63 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abashev |
| hostname | github.com |
| expected-hostname | github.com |
| None | d440bda0d812801778b27bfe9f7c1ce9ff22420c73f6f95150c4fcbca311c50b |
| turbo-cache-control | no-preview |
| go-import | github.com/openjavaformat/open-java-format git https://github.com/openjavaformat/open-java-format.git |
| octolytics-dimension-user_id | 325177626 |
| octolytics-dimension-user_login | openjavaformat |
| octolytics-dimension-repository_id | 1288055459 |
| octolytics-dimension-repository_nwo | openjavaformat/open-java-format |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | true |
| octolytics-dimension-repository_parent_id | 214273098 |
| octolytics-dimension-repository_parent_nwo | palantir/palantir-java-format |
| octolytics-dimension-repository_network_root_id | 214273098 |
| octolytics-dimension-repository_network_root_nwo | palantir/palantir-java-format |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2c43021aed8695990c86c0eaaedc600bfeffec36 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width