forked from google/error-prone
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disallow using traditional
:
-style switches as switch expressions
PiperOrigin-RevId: 653064183
- Loading branch information
Showing
4 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
core/src/main/java/com/google/errorprone/bugpatterns/TraditionalSwitchExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2024 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
import static com.google.errorprone.matchers.Description.NO_MATCH; | ||
import static com.google.errorprone.util.ASTHelpers.isRuleKind; | ||
|
||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.matchers.Description; | ||
import com.sun.source.tree.CaseTree; | ||
import com.sun.source.tree.Tree; | ||
|
||
/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ | ||
@BugPattern(summary = "Prefer -> switches for switch expressions", severity = WARNING) | ||
public class TraditionalSwitchExpression extends BugChecker implements BugChecker.CaseTreeMatcher { | ||
|
||
@Override | ||
public Description matchCase(CaseTree tree, VisitorState state) { | ||
if (isRuleKind(tree)) { | ||
return NO_MATCH; | ||
} | ||
Tree parent = state.getPath().getParentPath().getLeaf(); | ||
if (!parent.getKind().name().equals("SWITCH_EXPRESSION")) { | ||
return NO_MATCH; | ||
} | ||
return describeMatch(parent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
core/src/test/java/com/google/errorprone/bugpatterns/TraditionalSwitchExpressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2024 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.bugpatterns; | ||
|
||
import static org.junit.Assume.assumeTrue; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import com.google.errorprone.util.RuntimeVersion; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class TraditionalSwitchExpressionTest { | ||
|
||
private final CompilationTestHelper testHelper = | ||
CompilationTestHelper.newInstance(TraditionalSwitchExpression.class, getClass()); | ||
|
||
@Test | ||
public void positive() { | ||
assumeTrue(RuntimeVersion.isAtLeast14()); | ||
testHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"class Test {", | ||
" int f(int i) {", | ||
" // BUG: Diagnostic contains: Prefer -> switches for switch expressions", | ||
" return switch (i) {", | ||
" default:", | ||
" yield -1;", | ||
" };", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void negativeStatement() { | ||
assumeTrue(RuntimeVersion.isAtLeast14()); | ||
testHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"class Test {", | ||
" void f(int i) {", | ||
" switch (i) {", | ||
" default:", | ||
" return;", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void negativeArrowStatement() { | ||
assumeTrue(RuntimeVersion.isAtLeast14()); | ||
testHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"class Test {", | ||
" void f(int i) {", | ||
" switch (i) {", | ||
" default -> System.err.println();", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void negativeArrow() { | ||
assumeTrue(RuntimeVersion.isAtLeast14()); | ||
testHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"class Test {", | ||
" int f(int i) {", | ||
" return switch (i) {", | ||
" default -> -1;", | ||
" };", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |