Skip to content

Commit

Permalink
Handle arrow switches in MissingDefault
Browse files Browse the repository at this point in the history
Fixes #2256

PiperOrigin-RevId: 376482444
  • Loading branch information
cushon authored and Error Prone Team committed Jun 1, 2021
1 parent 9aa812b commit 090360e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
Expand Up @@ -30,8 +30,10 @@
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.Reachability;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.SwitchTree;
import com.sun.tools.javac.code.Type;
import java.util.List;
import java.util.Optional;
import javax.lang.model.element.ElementKind;

Expand Down Expand Up @@ -62,8 +64,10 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
// for the switch statement. Hopefully we don't often see switches with zero cases.
CaseTree lastCase = getLast(tree.getCases());
String replacement;
if (lastCase.getStatements().isEmpty()
|| Reachability.canCompleteNormally(Iterables.getLast(lastCase.getStatements()))) {
List<? extends StatementTree> statements = lastCase.getStatements();
if (statements == null
|| statements.isEmpty()
|| Reachability.canCompleteNormally(Iterables.getLast(statements))) {
replacement = "\nbreak;\ndefault: // fall out\n";
} else {
replacement = "\ndefault: // fall out\n";
Expand All @@ -73,7 +77,8 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
return description.build();
}
CaseTree defaultCase = maybeDefault.get();
if (!defaultCase.getStatements().isEmpty()) {
List<? extends StatementTree> statements = defaultCase.getStatements();
if (statements != null && !statements.isEmpty()) {
return NO_MATCH;
}
// If `default` case is empty, and last in switch, add `// fall out` comment
Expand Down
Expand Up @@ -20,6 +20,7 @@

import com.google.errorprone.BugCheckerRefactoringTestHelper;
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;
Expand Down Expand Up @@ -225,4 +226,40 @@ public void multipleStatementsInGroup() {
"}")
.doTest(TEXT_MATCH);
}

@Test
public void arrowSwitch() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void m(int i) {",
" // BUG: Diagnostic contains:",
" switch (i) {",
" case 1 -> {}",
" case 2 -> {}",
" }",
" }",
"}")
.doTest();
}

@Test
public void arrowSwitchNegative() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void m(int i) {",
" switch (i) {",
" case 1 -> {}",
" case 2 -> {}",
" default -> {} // fall out",
" }",
" }",
"}")
.doTest();
}
}
Expand Up @@ -23,6 +23,7 @@
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;
import com.google.errorprone.annotations.MustBeClosed;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
Expand All @@ -47,8 +48,8 @@ public static JavaFileObject forResource(Class<?> clazz, String resourceName) {
URI.create(
"file:///" + clazz.getPackage().getName().replace('.', '/') + "/" + resourceName);
String content;
try {
content = new String(ByteStreams.toByteArray(findResource(clazz, resourceName)), UTF_8);
try (InputStream inputStream = findResource(clazz, resourceName)) {
content = new String(ByteStreams.toByteArray(inputStream), UTF_8);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -61,6 +62,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
}

// TODO(b/176096448): the testdata/ fallback is a hack, fix affected tests and remove it
@MustBeClosed
private static InputStream findResource(Class<?> clazz, String name) {
InputStream is = clazz.getResourceAsStream(name);
if (is != null) {
Expand Down

0 comments on commit 090360e

Please sign in to comment.