From b8c865c3c9a99bdd27992642822e24b66d30e4da Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 15 Sep 2023 16:09:35 -0700 Subject: [PATCH] Don't require `// fall out ` comments on expression switches https://github.com/google/error-prone/issues/2709 PiperOrigin-RevId: 565802684 --- .../bugpatterns/MissingDefault.java | 2 +- .../bugpatterns/MissingDefaultTest.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MissingDefault.java b/core/src/main/java/com/google/errorprone/bugpatterns/MissingDefault.java index e77472ff220..d3129dd9f19 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MissingDefault.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MissingDefault.java @@ -75,7 +75,7 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) { } CaseTree defaultCase = maybeDefault.get(); List statements = defaultCase.getStatements(); - if (statements != null && !statements.isEmpty()) { + if (statements == null || !statements.isEmpty()) { return NO_MATCH; } // If `default` case is empty, and last in switch, add `// fall out` comment diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/MissingDefaultTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/MissingDefaultTest.java index 05f77cab6c8..9799b78fe4f 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/MissingDefaultTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/MissingDefaultTest.java @@ -263,4 +263,23 @@ public void arrowSwitchNegative() { "}") .doTest(); } + + @Test + public void arrowComment() { + assumeTrue(RuntimeVersion.isAtLeast14()); + compilationHelper + .addSourceLines( + "Test.java", + "class Test {", + " void f() {}", + " void m(int i) {", + " switch (i) {", + " case 0 -> f();", + " case 1 -> f();", + " default -> f();", + " }", + " }", + "}") + .doTest(); + } }