Skip to content

Commit

Permalink
[StatementSwitchToExpressionSwitch] Disable checker when Java21-style…
Browse files Browse the repository at this point in the history
… case patterns are detected, because these are not currently supported and produce incorrect suggested fixes. In the future, support may be added.

PiperOrigin-RevId: 727103506
  • Loading branch information
markhbrady authored and Error Prone Team committed Feb 15, 2025
1 parent dbe6d47 commit 711d705
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ private static AnalysisResult analyzeSwitchTree(SwitchTree switchTree, VisitorSt
// One-pass scan through each case in switch
for (int caseIndex = 0; caseIndex < cases.size(); caseIndex++) {
CaseTree caseTree = cases.get(caseIndex);
boolean hasCasePattern =
caseTree.getLabels().stream()
.anyMatch(
caseLabelTree -> caseLabelTree.getKind().name().equals("PATTERN_CASE_LABEL"));
if (hasCasePattern) {
// Case patterns are not currently supported by the checker.
return DEFAULT_ANALYSIS_RESULT;
}
boolean isDefaultCase = caseTree.getExpressions().isEmpty();
hasDefaultCase |= isDefaultCase;
// Accumulate enum values included in this case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.errorprone.bugpatterns;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
Expand Down Expand Up @@ -5465,6 +5466,73 @@ String f(int x) {
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}

@Test
public void directConversion_casePatternWithGuard_noError() {
// Case patterns are not currently supported by the checker.
assume().that(Runtime.version().feature()).isAtLeast(21);

helper
.addSourceLines(
"Test.java",
"""
class Test {
int[] x;
public Test(int foo) {
x = null;
}
public int[] foo(String s) {
switch (s) {
case String str
when str.equals("good"):
break;
case String str
when str.equals("bad"):
break;
default:
throw new RuntimeException();
}
return x;
}
}
""")
.setArgs("-XepOpt:StatementSwitchToExpressionSwitch:EnableDirectConversion")
.doTest();
}

@Test
public void directConversion_casePatternWithoutGuard_noError() {
// Case patterns are not currently supported by the checker.
assume().that(Runtime.version().feature()).isAtLeast(21);

helper
.addSourceLines(
"Test.java",
"""
class Test {
int[] x;
public Test(int foo) {
x = null;
}
public int[] foo(String s) {
switch (s) {
case String str:
String[] foo = {"hello", "world"};
break;
}
return x;
}
}
""")
.setArgs("-XepOpt:StatementSwitchToExpressionSwitch:EnableDirectConversion")
.doTest();
}

/**
* Asserts that there is exactly one suggested fix and returns it.
*
Expand Down

0 comments on commit 711d705

Please sign in to comment.