Skip to content

Commit

Permalink
Fix a crash in expression switches
Browse files Browse the repository at this point in the history
`throws` statements are allows to occur in non-block expression switch cases,
which was causing the trailing `;` to be printed twice.

Fixes #477

PiperOrigin-RevId: 310735686
  • Loading branch information
cushon authored and google-java-format Team committed May 11, 2020
1 parent 0354f02 commit d631b77
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public Void visitCase(CaseTree node, Void unused) {
token(">");
builder.space();
scan(node.getBody(), null);
token(";");
builder.guessToken(";");
break;
default:
throw new AssertionError(node.getCaseKind());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
@RunWith(Parameterized.class)
public class FormatterIntegrationTest {

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

@Parameters(name = "{index}: {0}")
public static Iterable<Object[]> data() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class I477 {
public static String foo(int in) {
return switch (in) {
case 1 -> "A";
case 2 -> "B";
default -> throw new IllegalStateException("Unknown input " + in);
};
}

public static String foo(int in) {
return switch (in) {
case 1 -> "A";
case 2 -> "B";
default -> "C";
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class I477 {
public static String foo(int in) {
return switch (in) {
case 1 -> "A";
case 2 -> "B";
default -> throw new IllegalStateException("Unknown input " + in);
};
}

public static String foo(int in) {
return switch (in) {
case 1 -> "A";
case 2 -> "B";
default -> "C";
};
}
}

0 comments on commit d631b77

Please sign in to comment.