From 4dd1c78e67086b1232282325d5aa9b6288ab08fb Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 15 Sep 2023 11:42:17 -0700 Subject: [PATCH] Don't require `// fall out ` comments on expression switches https://github.com/google/error-prone/issues/2709 PiperOrigin-RevId: 565736512 --- .../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 e77472ff2200..d3129dd9f19d 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 05f77cab6c87..9799b78fe4fc 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(); + } }