Skip to content

Format a class body that holds nothing but semicolons - #68

Merged
abashev merged 2 commits into
mainfrom
semicolon-only-class-body
Sep 25, 2026
Merged

abashev merged 2 commits into
mainfrom
semicolon-only-class-body

Conversation

@abashev

@abashev abashev commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Brings over google/google-java-format#1104, a fix from google-java-format 1.23.

The bug

class A { ; } failed with error: expected token: ';'; generated } instead. javac drops a stray semicolon from the member list, so the body looked empty and the formatter wrote {} without ever emitting the ; that was still in the input. A semicolon between real members was already handled; only a body with no member at all lost it. The same happened to interface B { ; }, record R(int a) { ; }, @interface H { ; } and new Object() { ; }.

From upstream, with the author kept

Added here

  • The golden ojf-semicolon-only-body: an interface, an enum, a record, an annotation type and an anonymous class with only semicolons in the body, two semicolons in a row, a comment after the opening brace, and a semicolon in front of a real member, which took the existing path all along.

Checked

  • Both goldens fail on main and pass with the change; the output is stable on a second run.
  • ./gradlew :open-java-format:test on JDK 21: 1505 tests, all green.
  • The 15,747 files of the JDK 21 sources format exactly as before.

cushon and others added 2 commits September 24, 2026 22:59
"class A { ; }" failed with "expected token: ';'; generated } instead".
javac drops a stray semicolon from the member list, so the body looked
empty and the formatter wrote "{}" without ever emitting the ";" token
that was still in the input. A semicolon between real members was
already handled; only a body with no member at all lost it.

This ports google/google-java-format#1104 by Liam Miller-Cushon: an
empty body whose next token is ";" writes those semicolons one per
line at the member indent before the closing brace, as the member list
does. The golden SemicolonInClass comes from upstream, with the
expected output in this project's style.

The 15,747 files of the JDK 21 sources format exactly as before.
Interfaces, enums, records, annotation types and anonymous classes go
through the same member-list code as classes, so the fix covers them
too; the golden pins that, together with two semicolons in a row, a
comment after the opening brace, and a semicolon in front of a real
member, which took the existing path all along.
@abashev
abashev merged commit 34ddeaa into main Sep 25, 2026
14 of 15 checks passed
@abashev
abashev deleted the semicolon-only-class-body branch September 25, 2026 07:53
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.

2 participants