Title: Insert missing braces around single-statement if/else/for/while/do bodies (--cleanup) · Issue #147 · openjavaformat/open-java-format · GitHub
Open Graph Title: Insert missing braces around single-statement if/else/for/while/do bodies (--cleanup) · Issue #147 · openjavaformat/open-java-format
X Title: Insert missing braces around single-statement if/else/for/while/do bodies (--cleanup) · Issue #147 · openjavaformat/open-java-format
Description: Upstream: google/google-java-format#1492 Kind: feature | Priority: medium google-java-format#1492 (merged 2026-10-22, a Google-internal change exported by copybara) adds a --cleanup[=true|false] flag and a matching JavaFormatterOptions.B...
Open Graph Description: Upstream: google/google-java-format#1492 Kind: feature | Priority: medium google-java-format#1492 (merged 2026-10-22, a Google-internal change exported by copybara) adds a --cleanup[=true|false] fl...
X Description: Upstream: google/google-java-format#1492 Kind: feature | Priority: medium google-java-format#1492 (merged 2026-10-22, a Google-internal change exported by copybara) adds a --cleanup[=true|false] fl...
Opengraph URL: https://github.com/openjavaformat/open-java-format/issues/147
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Insert missing braces around single-statement if/else/for/while/do bodies (--cleanup)","articleBody":"**Upstream:** https://github.com/google/google-java-format/pull/1492 \n**Kind:** feature | **Priority:** medium\n\n[google-java-format#1492](https://github.com/google/google-java-format/pull/1492) (merged 2026-10-22, a Google-internal change exported by copybara) adds a `--cleanup[=true|false]` flag and a matching `JavaFormatterOptions.Builder.cleanup(boolean)` option, on by default. It gates a new AST cleanup pass (`CleanupScanner`) that wraps unbraced bodies of `if`, `else`, `for`, `do` and `while` in braces, keeps trailing comments on the opening-brace line, and handles nested if/else chains. The pass runs inside the cleanups pipeline introduced by [google-java-format#1488](https://github.com/google/google-java-format/pull/1488).\n\nWhy it matters: it is the first non-whitespace rewrite google-java-format applies by default, so users of our fork who also use that tool may expect the same result. It changes formatted output for existing code bases, which is why upstream made it switchable. A follow-up in [google-java-format#1491](https://github.com/google/google-java-format/pull/1491) adds redundant-semicolon removal to the same pass. We have no brace insertion today (no scanner of this kind in our sources).\n\n**Before** (how it is formatted today, our formatter keeps the unbraced body):\n```java\nclass T {\n void f(boolean a, boolean b) {\n if (a)\n if (b) doA();\n else doB();\n while (a) doA();\n }\n}\n```\n**After** (with the change, from upstream test expectations):\n```java\nclass T {\n void f(boolean a, boolean b) {\n if (a) {\n if (b) {\n doA();\n } else {\n doB();\n }\n }\n while (a) {\n doA();\n }\n }\n}\n```\n(Indentation shown at two spaces for brevity; ours would use the project's four-space layout.)\n\nAffected files in our repo:\n- open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java (pipeline entry points)\n- open-java-format/src/main/java/com/palantir/javaformat/java/CommandLineOptions.java and CommandLineOptionsParser.java (new flag)\n- open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java (statement visiting, for context)\n- open-java-format/src/test/resources/com/palantir/javaformat/java/testdata (goldens would need an opt-out)\n\nSuggested action: investigate. Decide whether we want any non-whitespace rewrite, and if so make it opt-in (default off) so existing output does not change. Depends on the cleanups-pipeline decision in the existing ticket for google-java-format#1488. Otherwise ignore-with-reason (\"formatter should not rewrite code\").\n\n---\n_Filed from an upstream-watch ticket after manual review._","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-10-23T18:43:57Z","url":"https://github.com/openjavaformat/open-java-format/issues/147"}
| 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:6438c9d0-5d59-7879-9891-18c2fe0fb087 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D73C:C4A52:2C6A22:3AC080:6AFBB793 |
| html-safe-nonce | f4021e5bd2aff06536b8c3c907c693e900023986b8d895f22f3695fcca5de208 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzNDOkM0QTUyOjJDNkEyMjozQUMwODA6NkFGQkI3OTMiLCJ2aXNpdG9yX2lkIjoiMjYzMDA3ODU1NzU2NDE1NTc5NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5295741c00efb5d18fea87fe81d249fb3d8653969ebd2e03d19f39defccf90a3 |
| hovercard-subject-tag | issue:6003566599 |
| 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/147/issue_layout |
| twitter:image | https://opengraph.githubassets.com/14fbcc2a7dd67aa626f980af8e1194c0cd44967d42434a89c2f7703b239c4cde/openjavaformat/open-java-format/issues/147 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/14fbcc2a7dd67aa626f980af8e1194c0cd44967d42434a89c2f7703b239c4cde/openjavaformat/open-java-format/issues/147 |
| og:image:alt | Upstream: google/google-java-format#1492 Kind: feature | Priority: medium google-java-format#1492 (merged 2026-10-22, a Google-internal change exported by copybara) adds a --cleanup[=true|false] fl... |
| 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 | 2b2ef9c1578c18d8ed27027940544c958743213cf4461659f14d3ccdf51e162c |
| 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 | e4cce0e99cf61481a318ff12c2e8580153a6c7c6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width