From d631b77724f93d153ae253b9ff742ed8163c8d12 Mon Sep 17 00:00:00 2001 From: cushon Date: Sat, 9 May 2020 13:16:43 -0700 Subject: [PATCH] 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 https://github.com/google/google-java-format/issues/477 PiperOrigin-RevId: 310735686 --- .../java/java14/Java14InputAstVisitor.java | 2 +- .../java/FormatterIntegrationTest.java | 2 +- .../googlejavaformat/java/testdata/I477.input | 17 +++++++++++++++++ .../googlejavaformat/java/testdata/I477.output | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I477.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I477.output diff --git a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java index 28a110395..6585c6f6e 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java @@ -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()); diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index 289ea1baf..0e0ab9f49 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -47,7 +47,7 @@ @RunWith(Parameterized.class) public class FormatterIntegrationTest { - private static final ImmutableSet JAVA14_TESTS = ImmutableSet.of("java14"); + private static final ImmutableSet JAVA14_TESTS = ImmutableSet.of("java14", "I477"); @Parameters(name = "{index}: {0}") public static Iterable data() throws IOException { diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I477.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I477.input new file mode 100644 index 000000000..ecd8658b3 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I477.input @@ -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"; + }; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I477.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I477.output new file mode 100644 index 000000000..ecd8658b3 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I477.output @@ -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"; + }; + } +}