Skip to content

Commit 21a7177

Browse files
authored
Support for var in record patterns (#1312)
1 parent 5d6d68a commit 21a7177

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: fix
2+
fix:
3+
description: Support for var in record patterns
4+
links:
5+
- https://github.com/palantir/palantir-java-format/pull/1312

palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name)
120120
if (modifiers != null) {
121121
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
122122
}
123-
scan(type, null);
123+
if (type == null) {
124+
token("var");
125+
} else {
126+
scan(type, null);
127+
}
124128
builder.breakOp(" ");
125129
if (name.isEmpty()) {
126130
token("_");

palantir-java-format/src/test/java/com/palantir/javaformat/java/FileBasedTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ public final class FileBasedTests {
5050
.putAll(15, "I603")
5151
.putAll(16, "I588")
5252
.putAll(17, "I683", "I684", "I696")
53-
.putAll(21, "SwitchGuardClause", "SwitchRecord", "SwitchDouble", "SwitchUnderscore", "I880")
53+
.putAll(
54+
21,
55+
"SwitchGuardClause",
56+
"SwitchRecord",
57+
"SwitchDouble",
58+
"SwitchUnderscore",
59+
"I880",
60+
"I1309")
5461
.build();
5562

5663
private final Class<?> testClass;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class I1309 {
2+
void x(Object o) {
3+
if (o instanceof Record(Nested(var i), var j)) {}
4+
switch (o) {
5+
case Record(Nested(var i), var j, int k, char l, java.lang.String m, var n, var o, var p, var q, var r, var s, var t, var u, var v, var w, var x, var y, var z):
6+
default:
7+
}
8+
}
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class I1309 {
2+
void x(Object o) {
3+
if (o instanceof Record(Nested(var i), var j)) {}
4+
switch (o) {
5+
case Record(
6+
Nested(var i),
7+
var j,
8+
int k,
9+
char l,
10+
java.lang.String m,
11+
var n,
12+
var o,
13+
var p,
14+
var q,
15+
var r,
16+
var s,
17+
var t,
18+
var u,
19+
var v,
20+
var w,
21+
var x,
22+
var y,
23+
var z):
24+
default:
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)