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(); + } }