Skip to content

Commit

Permalink
Support unnamed variables
Browse files Browse the repository at this point in the history
Fixes #978

PiperOrigin-RevId: 589179786
  • Loading branch information
cushon authored and google-java-format Team committed Dec 8, 2023
1 parent 430ba3b commit b92435a
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3563,7 +3563,7 @@ int declareOne(
if (receiverExpression.isPresent()) {
scan(receiverExpression.get(), null);
} else {
visit(name);
variableName(name);
}
builder.op(op);
}
Expand Down Expand Up @@ -3606,6 +3606,10 @@ int declareOne(
return baseDims;
}

protected void variableName(Name name) {
visit(name);
}

private void maybeAddDims(Deque<List<? extends AnnotationTree>> annotations) {
maybeAddDims(new ArrayDeque<>(), annotations);
}
Expand Down Expand Up @@ -3696,7 +3700,7 @@ private void declareMany(List<VariableTree> fragments, Direction annotationDirec
builder.breakOp(" ");
builder.open(ZERO);
maybeAddDims(dims);
visit(fragment.getName());
variableName(fragment.getName());
maybeAddDims(dims);
ExpressionTree initializer = fragment.getInitializer();
if (initializer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.PatternCaseLabelTree;
import com.sun.source.tree.PatternTree;
import javax.lang.model.element.Name;

/**
* Extends {@link Java17InputAstVisitor} with support for AST nodes that were added or modified in
Expand Down Expand Up @@ -76,4 +77,13 @@ public Void visitDeconstructionPattern(DeconstructionPatternTree node, Void unus
token(")");
return null;
}

@Override
protected void variableName(Name name) {
if (name.isEmpty()) {
token("_");
} else {
visit(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ public class FormatterIntegrationTest {
.putAll(16, "I588")
.putAll(17, "I683", "I684", "I696")
.putAll(
21, "SwitchGuardClause", "SwitchRecord", "SwitchDouble", "SwitchUnderscore", "I880")
21,
"SwitchGuardClause",
"SwitchRecord",
"SwitchDouble",
"SwitchUnderscore",
"I880",
"Unnamed")
.build();

@Parameters(name = "{index}: {0}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Unnamed {
{
int acc = 0;
for (Order _ : orders) {
if (acc < LIMIT) {
acc++;
}
}

for (int i = 0, _ = sideEffect(); i < 10; i++) { }

Queue<Integer> q = null;
while (q.size() >= 3) {
var x = q.remove();
var y = q.remove();
var _ = q.remove();
new Point(x, y);
}

while (q.size() >= 3) {
var x = q.remove();
var _ = q.remove();
var _ = q.remove();
new Point(x, 0) ;
}

String s = null;
try {
int i = Integer.parseInt(s);
} catch (NumberFormatException _) {
System.out.println("Bad number: " + s);
}

try { doSomething(); }
catch (Exception _) { doSomething(); }
catch (Throwable _) { doSomething(); }

try (var _ = ScopedContext.acquire()) {
doSomething();
}

stream.collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Unnamed {
{
int acc = 0;
for (Order _ : orders) {
if (acc < LIMIT) {
acc++;
}
}

for (int i = 0, _ = sideEffect(); i < 10; i++) {}

Queue<Integer> q = null;
while (q.size() >= 3) {
var x = q.remove();
var y = q.remove();
var _ = q.remove();
new Point(x, y);
}

while (q.size() >= 3) {
var x = q.remove();
var _ = q.remove();
var _ = q.remove();
new Point(x, 0);
}

String s = null;
try {
int i = Integer.parseInt(s);
} catch (NumberFormatException _) {
System.out.println("Bad number: " + s);
}

try {
doSomething();
} catch (Exception _) {
doSomething();
} catch (Throwable _) {
doSomething();
}

try (var _ = ScopedContext.acquire()) {
doSomething();
}

stream.collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA"));
}
}

0 comments on commit b92435a

Please sign in to comment.