Skip to content

Keep the space between a unary minus and a negative literal - #64

Merged
abashev merged 1 commit into
mainfrom
unary-minus-literal
Sep 25, 2026
Merged

abashev merged 1 commit into
mainfrom
unary-minus-literal

Conversation

@abashev

@abashev abashev commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Brings over google/google-java-format#576, a fix from google-java-format 1.10.

The bug

int a = - -1; came out as int a = --1;, which does not compile: -- is the decrement operator. javac folds a minus in front of a decimal literal into the literal itself, so - -1 parses as one unary minus applied to the literal -1, and the check that keeps a space between two prefix operators only looked for a nested unary expression. - -x was fine, the literal case was not.

From upstream, with the author kept

Checked

  • The golden fails on main (--1) and passes with the change.
  • ./gradlew :open-java-format:test on JDK 21: 1501 tests, all green.
  • The 15,747 files of the JDK 21 sources format exactly as before.

javac folds a minus in front of a decimal literal into the literal
itself, so "- -1" parses as one unary minus applied to the literal
"-1". The check that keeps a space between two prefix operators only
looked for a nested unary expression, so the output was "--1": the
decrement operator on a literal, which does not compile. "- -x" was
fine, the literal case was not.

This ports google/google-java-format#576 by Liam Miller-Cushon: a
literal whose source starts with "-" counts as a unary minus in that
check. The golden B183431894 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.
@abashev
abashev merged commit d6d4a71 into main Sep 25, 2026
15 checks passed
@abashev
abashev deleted the unary-minus-literal branch September 25, 2026 07:47
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