Skip to content

Commit

Permalink
Short-circuit StatementSwitchToExpressionSwitch analysis when present…
Browse files Browse the repository at this point in the history
…ed with expression switch; add non-empty expression switch test case

PiperOrigin-RevId: 499480786
  • Loading branch information
java-team-github-bot authored and Error Prone Team committed Jan 4, 2023
1 parent 836caf2 commit a604633
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ private static AnalysisResult analyzeSwitchTree(SwitchTree switchTree) {

List<? extends StatementTree> statements = caseTree.getStatements();
CaseFallThru caseFallThru = CaseFallThru.MAYBE_FALLS_THRU;
if (statements == null || statements.isEmpty()) {
if (statements == null) {
// This case must be of kind CaseTree.CaseKind.RULE, and thus this is already an expression
// switch; no need to continue analysis.
return AnalysisResult.of(
/* canConvertDirectlyToExpressionSwitch= */ false, ImmutableList.of());
} else if (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 @@ -1017,7 +1017,7 @@ public void singleCaseConvertible_error() {
}

@Test
public void emptySwitchCases_noMatch() {
public void emptyExpressionSwitchCases_noMatch() {
assumeTrue(RuntimeVersion.isAtLeast14());
helper
.addSourceLines(
Expand All @@ -1033,6 +1033,23 @@ public void emptySwitchCases_noMatch() {
.doTest();
}

@Test
public void nonEmptyExpressionSwitchCases_noMatch() {
assumeTrue(RuntimeVersion.isAtLeast14());
helper
.addSourceLines(
"Test.java",
"class Test {",
" void foo(int value) { ",
" switch (value) {",
" case 0 -> System.out.println(\"zero\");",
" default -> {System.out.println(\"non-zero\");}",
" }",
" }",
"}")
.doTest();
}

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

0 comments on commit a604633

Please sign in to comment.