Skip to content

Commit 6da736d

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Add a space between unary minus and negative literals
PiperOrigin-RevId: 365063449
1 parent 189c381 commit 6da736d

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -1599,18 +1599,22 @@ public Void visitMemberSelect(MemberSelectTree node, Void unused) {
15991599
public Void visitLiteral(LiteralTree node, Void unused) {
16001600
sync(node);
16011601
String sourceForNode = getSourceForNode(node, getCurrentPath());
1602-
// A negative numeric literal -n is usually represented as unary minus on n,
1603-
// but that doesn't work for integer or long MIN_VALUE. The parser works
1604-
// around that by representing it directly as a signed literal (with no
1605-
// unary minus), but the lexer still expects two tokens.
1606-
if (sourceForNode.startsWith("-")) {
1602+
if (isUnaryMinusLiteral(sourceForNode)) {
16071603
token("-");
16081604
sourceForNode = sourceForNode.substring(1).trim();
16091605
}
16101606
token(sourceForNode);
16111607
return null;
16121608
}
16131609

1610+
// A negative numeric literal -n is usually represented as unary minus on n,
1611+
// but that doesn't work for integer or long MIN_VALUE. The parser works
1612+
// around that by representing it directly as a signed literal (with no
1613+
// unary minus), but the lexer still expects two tokens.
1614+
private static boolean isUnaryMinusLiteral(String literalTreeSource) {
1615+
return literalTreeSource.startsWith("-");
1616+
}
1617+
16141618
private void visitPackage(
16151619
ExpressionTree packageName, List<? extends AnnotationTree> packageAnnotations) {
16161620
if (!packageAnnotations.isEmpty()) {
@@ -1696,10 +1700,10 @@ private boolean ambiguousUnaryOperator(UnaryTree node, String operatorName) {
16961700
default:
16971701
return false;
16981702
}
1699-
if (!(node.getExpression() instanceof UnaryTree)) {
1703+
JCTree.Tag tag = unaryTag(node.getExpression());
1704+
if (tag == null) {
17001705
return false;
17011706
}
1702-
JCTree.Tag tag = ((JCTree) node.getExpression()).getTag();
17031707
if (tag.isPostUnaryOp()) {
17041708
return false;
17051709
}
@@ -1709,6 +1713,17 @@ private boolean ambiguousUnaryOperator(UnaryTree node, String operatorName) {
17091713
return true;
17101714
}
17111715

1716+
private JCTree.Tag unaryTag(ExpressionTree expression) {
1717+
if (expression instanceof UnaryTree) {
1718+
return ((JCTree) expression).getTag();
1719+
}
1720+
if (expression instanceof LiteralTree
1721+
&& isUnaryMinusLiteral(getSourceForNode(expression, getCurrentPath()))) {
1722+
return JCTree.Tag.MINUS;
1723+
}
1724+
return null;
1725+
}
1726+
17121727
@Override
17131728
public Void visitPrimitiveType(PrimitiveTypeTree node, Void unused) {
17141729
sync(node);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class B183431894 {
2+
int a = - -1;
3+
int d = + +1;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class B183431894 {
2+
int a = - -1;
3+
int d = + +1;
4+
}

0 commit comments

Comments
 (0)