Skip to content

Commit

Permalink
Omit some unnecessary break statements when translating to -> switches
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 658578239
  • Loading branch information
cushon authored and Error Prone Team committed Aug 1, 2024
1 parent fe07236 commit d887307
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Streams;
import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
Expand Down Expand Up @@ -585,7 +584,7 @@ private static SuggestedFix convertDirectlyToExpressionSwitch(
groupedCaseCommentsAccumulator.length() == 0
? null
: groupedCaseCommentsAccumulator.toString(),
transformedBlockSource.isEmpty() ? null : transformedBlockSource,
transformedBlockSource.isEmpty() ? null : transformedBlockSource.trim(),
commentsBeforeRemovedBreak.orElse(null),
commentsAfterCaseOptional.orElse(null));
}
Expand Down Expand Up @@ -902,7 +901,7 @@ && getStatements(caseTree).size() > filteredStatements.size()) {
state
.getSourceCode()
.subSequence(
state.getEndPosition(Iterables.getLast(filteredStatements)),
state.getEndPosition(getLast(filteredStatements)),
getStartPosition(getStatements(caseTree).get(getStatements(caseTree).size() - 1)))
.toString()
.trim();
Expand Down Expand Up @@ -952,11 +951,10 @@ private static String transformBlock(

StringBuilder transformedBlockBuilder = new StringBuilder();
int codeBlockStart = extractLhsComments(caseTree, state, transformedBlockBuilder);
int codeBlockEnd =
filteredStatements.isEmpty()
? getBlockEnd(state, caseTree)
: state.getEndPosition(Streams.findLast(filteredStatements.stream()).get());
transformedBlockBuilder.append(state.getSourceCode(), codeBlockStart, codeBlockEnd);
if (!filteredStatements.isEmpty()) {
int codeBlockEnd = state.getEndPosition(getLast(filteredStatements));
transformedBlockBuilder.append(state.getSourceCode(), codeBlockStart, codeBlockEnd);
}

return transformedBlockBuilder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ public void switchByEnumCard2_removesRedundantBreaks_error() {
" // Post break comment",
" case DIAMOND -> {",
" // Diamond break comment",
" break;",
" }",
" case SPADE, CLUB -> System.out.println(\"everything else\");",
" }",
Expand Down Expand Up @@ -1385,7 +1384,6 @@ public void switchByEnum_surroundingBracesCannotRemove_error() {
" switch(side) {",
" case OBVERSE -> {",
" // The quick brown fox, jumps over the lazy dog, etc.",
" break;",
" }",
" default -> ",
" throw new RuntimeException(\"Invalid type.\");",
Expand Down Expand Up @@ -3623,4 +3621,40 @@ public void i4222() {
.setArgs("-XepOpt:StatementSwitchToExpressionSwitch:EnableDirectConversion=true")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}

@Test
public void unnecessaryBreaks() {
assumeTrue(RuntimeVersion.isAtLeast14());
refactoringHelper
.addInputLines(
"Test.java",
"public class Test {",
" public static void main(String[] args) {",
" switch (args.length) {",
" case 0:",
" System.out.println(0);",
" break;",
" default:",
" // hello",
" // world",
" break;",
" }",
" }",
"}")
.addOutputLines(
"Test.java",
"public class Test {",
" public static void main(String[] args) {",
" switch (args.length) {",
" case 0 -> System.out.println(0);",
" default -> {",
" // hello",
" // world",
" }",
" }",
" }",
"}")
.setArgs("-XepOpt:StatementSwitchToExpressionSwitch:EnableDirectConversion=true")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}
}

0 comments on commit d887307

Please sign in to comment.