|
41 | 41 | import com.google.common.collect.ImmutableMap;
|
42 | 42 | import com.google.common.collect.ImmutableSet;
|
43 | 43 | import com.google.common.collect.Iterables;
|
44 |
| -import com.google.common.collect.Range; |
45 | 44 | import com.google.common.collect.Streams;
|
46 | 45 | import com.google.errorprone.BugPattern;
|
47 | 46 | import com.google.errorprone.ErrorProneFlags;
|
@@ -1021,7 +1020,7 @@ private static SuggestedFix convertToReturnSwitch(
|
1021 | 1020 | AnalysisResult analysisResult,
|
1022 | 1021 | boolean removeDefault) {
|
1023 | 1022 |
|
1024 |
| - List<Range<Integer>> regionsToDelete = new ArrayList<>(); |
| 1023 | + SuggestedFix.Builder suggestedFixBuilder = SuggestedFix.builder(); |
1025 | 1024 | List<? extends CaseTree> cases = switchTree.getCases();
|
1026 | 1025 | ImmutableList<ErrorProneComment> allSwitchComments =
|
1027 | 1026 | state.getTokensForNode(switchTree).stream()
|
@@ -1119,39 +1118,50 @@ private static SuggestedFix convertToReturnSwitch(
|
1119 | 1118 | if (tree instanceof BlockTree blockTree) {
|
1120 | 1119 | TreePath rootToCurrentPath = TreePath.getPath(state.getPath(), switchTree);
|
1121 | 1120 | int indexInBlock = findBlockStatementIndex(rootToCurrentPath, blockTree);
|
| 1121 | + var statements = blockTree.getStatements(); |
1122 | 1122 | // A single mock of the immediate child statement block (or switch) is sufficient to
|
1123 | 1123 | // analyze reachability here; deeper-nested statements are not relevant.
|
1124 | 1124 | boolean nextStatementReachable =
|
1125 | 1125 | Reachability.canCompleteNormally(
|
1126 |
| - blockTree.getStatements().get(indexInBlock), |
1127 |
| - ImmutableMap.of(cannotCompleteNormallyTree, false)); |
| 1126 | + statements.get(indexInBlock), ImmutableMap.of(cannotCompleteNormallyTree, false)); |
1128 | 1127 | // If we continue to the ancestor statement block, it will be because the end of this
|
1129 | 1128 | // statement block is not reachable
|
1130 | 1129 | cannotCompleteNormallyTree = blockTree;
|
1131 | 1130 | if (nextStatementReachable) {
|
1132 | 1131 | break;
|
1133 | 1132 | }
|
1134 | 1133 |
|
1135 |
| - // If a next statement in this block exists, then it is not reachable; similarly, none of |
1136 |
| - // the following statements in this block are reachable either. So, iff further statements |
1137 |
| - // exist, delete them all (including their comments). |
1138 |
| - if (indexInBlock < blockTree.getStatements().size() - 1) { |
1139 |
| - regionsToDelete.add( |
1140 |
| - Range.closed( |
1141 |
| - state.getEndPosition(blockTree.getStatements().get(indexInBlock)), |
1142 |
| - state.getEndPosition(blockTree))); |
| 1134 | + // If a next statement in this block exists, then it is not reachable. |
| 1135 | + if (indexInBlock < statements.size() - 1) { |
| 1136 | + String deletedRegion = |
| 1137 | + state |
| 1138 | + .getSourceCode() |
| 1139 | + .subSequence( |
| 1140 | + state.getEndPosition(statements.get(indexInBlock)), |
| 1141 | + state.getEndPosition(blockTree)) |
| 1142 | + .toString(); |
| 1143 | + // If the region we would delete looks interesting, bail out and just delete the orphaned |
| 1144 | + // statements. |
| 1145 | + if (deletedRegion.contains("LINT.")) { |
| 1146 | + statements |
| 1147 | + .subList(indexInBlock + 1, statements.size()) |
| 1148 | + .forEach(suggestedFixBuilder::delete); |
| 1149 | + } else { |
| 1150 | + // If the region doesn't seem to contain interesting comments, delete it along with |
| 1151 | + // comments: those comments are often just of the form "Unreachable code". |
| 1152 | + suggestedFixBuilder.replace( |
| 1153 | + state.getEndPosition(statements.get(indexInBlock)), |
| 1154 | + state.getEndPosition(blockTree), |
| 1155 | + "}"); |
| 1156 | + } |
1143 | 1157 | }
|
1144 | 1158 | }
|
1145 | 1159 | }
|
1146 | 1160 |
|
1147 |
| - SuggestedFix.Builder suggestedFixBuilder = SuggestedFix.builder(); |
1148 | 1161 | if (removeDefault) {
|
1149 | 1162 | suggestedFixBuilder.setShortDescription(REMOVE_DEFAULT_CASE_SHORT_DESCRIPTION);
|
1150 | 1163 | }
|
1151 | 1164 | suggestedFixBuilder.replace(switchTree, replacementCodeBuilder.toString());
|
1152 |
| - // Delete dead code and its comments |
1153 |
| - regionsToDelete.forEach( |
1154 |
| - r -> suggestedFixBuilder.replace(r.lowerEndpoint(), r.upperEndpoint(), "}")); |
1155 | 1165 | return suggestedFixBuilder.build();
|
1156 | 1166 | }
|
1157 | 1167 |
|
|
0 commit comments