Skip to content

Commit f5a9280

Browse files
cushongoogle-java-format Team
authored andcommitted
Fix a crash in expression switches
`throws` statements are allows to occur in non-block expression switch cases, which was causing the trailing `;` to be printed twice. Fixes google#477 PiperOrigin-RevId: 310974906
1 parent 0354f02 commit f5a9280

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public Void visitCase(CaseTree node, Void unused) {
223223
token(">");
224224
builder.space();
225225
scan(node.getBody(), null);
226-
token(";");
226+
builder.guessToken(";");
227227
break;
228228
default:
229229
throw new AssertionError(node.getCaseKind());

core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
@RunWith(Parameterized.class)
4848
public class FormatterIntegrationTest {
4949

50-
private static final ImmutableSet<String> JAVA14_TESTS = ImmutableSet.of("java14");
50+
private static final ImmutableSet<String> JAVA14_TESTS = ImmutableSet.of("java14", "I477");
5151

5252
@Parameters(name = "{index}: {0}")
5353
public static Iterable<Object[]> data() throws IOException {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class I477 {
2+
public static String foo(int in) {
3+
return switch (in) {
4+
case 1 -> "A";
5+
case 2 -> "B";
6+
default -> throw new IllegalStateException("Unknown input " + in);
7+
};
8+
}
9+
10+
public static String foo(int in) {
11+
return switch (in) {
12+
case 1 -> "A";
13+
case 2 -> "B";
14+
default -> "C";
15+
};
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class I477 {
2+
public static String foo(int in) {
3+
return switch (in) {
4+
case 1 -> "A";
5+
case 2 -> "B";
6+
default -> throw new IllegalStateException("Unknown input " + in);
7+
};
8+
}
9+
10+
public static String foo(int in) {
11+
return switch (in) {
12+
case 1 -> "A";
13+
case 2 -> "B";
14+
default -> "C";
15+
};
16+
}
17+
}

0 commit comments

Comments
 (0)