Skip to content

Commit

Permalink
StatementSwitchToExpressionSwitch: skip rule cases
Browse files Browse the repository at this point in the history
The cases in a switch statement must all be of kind `STATEMENT` or
`RULE`. In the latter case `StatementSwitchToExpressionSwitch` does not
apply.
  • Loading branch information
Stephan202 committed Jan 2, 2023
1 parent 8eae200 commit 4a7b6bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public StatementSwitchToExpressionSwitch(ErrorProneFlags flags) {
@Override
public Description matchSwitch(SwitchTree switchTree, VisitorState state) {
// Expression switches finalized in Java 14
if (!RuntimeVersion.isAtLeast14()) {
if (!RuntimeVersion.isAtLeast14() || isRuleSwitch(switchTree)) {
return NO_MATCH;
}

Expand All @@ -99,6 +99,18 @@ public Description matchSwitch(SwitchTree switchTree, VisitorState state) {
return NO_MATCH;
}

private static boolean isRuleSwitch(SwitchTree switchTree) {
return switchTree.getCases().stream().anyMatch(StatementSwitchToExpressionSwitch::isRuleCase);
}

private static boolean isRuleCase(CaseTree caseTree) {
try {
return "RULE".equals(CaseTree.class.getMethod("getCaseKind").invoke(caseTree).toString());
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

/**
* Analyzes a {@code SwitchTree}, and determines any possible findings and suggested fixes related
* to expression switches that can be made. Does not report any findings or suggested fixes up to
Expand All @@ -122,7 +134,7 @@ 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.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 emptyRuleSwitchCases_noMatch() {
assumeTrue(RuntimeVersion.isAtLeast14());
helper
.addSourceLines(
Expand All @@ -1030,6 +1030,25 @@ public void emptySwitchCases_noMatch() {
" }",
" }",
"}")
.setArgs("-XepOpt:StatementSwitchToExpressionSwitch:EnableDirectConversion")
.doTest();
}

@Test
public void voidRuleSwitchCases_noMatch() {
assumeTrue(RuntimeVersion.isAtLeast14());
helper
.addSourceLines(
"Test.java",
"class Test {",
" void foo(int value) { ",
" switch (value) {",
" case 0 -> { System.out.println(\"zero\"); }",
" case 1 -> { System.out.println(\"one\"); }",
" }",
" }",
"}")
.setArgs("-XepOpt:StatementSwitchToExpressionSwitch:EnableDirectConversion")
.doTest();
}

Expand Down

0 comments on commit 4a7b6bf

Please sign in to comment.