Keep the space between a unary minus and a negative literal - #64
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Brings over google/google-java-format#576, a fix from google-java-format 1.10.
The bug
int a = - -1;came out asint 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- -1parses 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.- -xwas fine, the literal case was not.From upstream, with the author kept
-now counts as a unary minus in that check, so the space stays. The upstream goldenB183431894comes with it, with the expected output in this project's style.Checked
--1) and passes with the change../gradlew :open-java-format:teston JDK 21: 1501 tests, all green.