Skip to content

Commit

Permalink
StatementSwitchToExpressionSwitch: handle empty statement blocks
Browse files Browse the repository at this point in the history
Fixes #3638.

Fixes #3645

COPYBARA_INTEGRATE_REVIEW=#3645 from PicnicSupermarket:bugfix/issue-3638 773f647
PiperOrigin-RevId: 498743647
  • Loading branch information
Stephan202 authored and Error Prone Team committed Dec 31, 2022
1 parent 4fa8720 commit 96cd000
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static AnalysisResult analyzeSwitchTree(SwitchTree switchTree) {

List<? extends StatementTree> statements = caseTree.getStatements();
CaseFallThru caseFallThru = CaseFallThru.MAYBE_FALLS_THRU;
if (statements.isEmpty()) {
if (statements == null || statements.isEmpty()) {
// If the code for this case is just an empty block, then it must fall thru
caseFallThru = CaseFallThru.DEFINITELY_DOES_FALL_THRU;
// Can group with the next case (unless this is the last case)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,23 @@ public void singleCaseConvertible_error() {
.doTest();
}

@Test
public void emptySwitchCases_noMatch() {
assumeTrue(RuntimeVersion.isAtLeast14());
helper
.addSourceLines(
"Test.java",
"class Test {",
" void foo(int value) { ",
" switch (value) {",
" case 0 -> {}",
" default -> {}",
" }",
" }",
"}")
.doTest();
}

@Test
public void dynamicWithThrowableDuringInitializationFromMethod_noMatch() {
assumeTrue(RuntimeVersion.isAtLeast14());
Expand Down

0 comments on commit 96cd000

Please sign in to comment.