From b0632d031590e93a7f149b4ff2af26d3413e54c6 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 2 Jan 2023 21:53:53 +0100 Subject: [PATCH] StatementSwitchToExpressionSwitch: skip rule cases The cases in a switch statement must all be of kind `STATEMENT` or `RULE`. In the latter case `StatementSwitchToExpressionSwitch` does not apply. --- .../StatementSwitchToExpressionSwitch.java | 16 ++++++++++++-- ...StatementSwitchToExpressionSwitchTest.java | 21 ++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java b/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java index b9181a5091f..cca422b8247 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java @@ -86,7 +86,7 @@ public StatementSwitchToExpressionSwitch(ErrorProneFlags flags) { @Override public Description matchSwitch(SwitchTree switchTree, VisitorState state) { - if (!SourceVersion.supportsSwitchExpressions(state.context)) { + if (!SourceVersion.supportsSwitchExpressions(state.context) || isRuleSwitch(switchTree)) { return NO_MATCH; } @@ -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 @@ -122,7 +134,7 @@ private static AnalysisResult analyzeSwitchTree(SwitchTree switchTree) { List 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) diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitchTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitchTest.java index 623ee82f4b7..d7e15766beb 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitchTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitchTest.java @@ -1017,7 +1017,7 @@ public void singleCaseConvertible_error() { } @Test - public void emptySwitchCases_noMatch() { + public void emptyRuleSwitchCases_noMatch() { assumeTrue(RuntimeVersion.isAtLeast14()); helper .addSourceLines( @@ -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(); }