|
20 | 20 | import static com.google.common.collect.ImmutableList.toImmutableList; |
21 | 21 | import static com.google.common.collect.ImmutableSet.toImmutableSet; |
22 | 22 | import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
| 23 | +import static com.google.errorprone.bugpatterns.SwitchUtils.COMPILE_TIME_CONSTANT_MATCHER; |
| 24 | +import static com.google.errorprone.bugpatterns.SwitchUtils.isEnumValue; |
| 25 | +import static com.google.errorprone.bugpatterns.SwitchUtils.renderComments; |
23 | 26 | import static com.google.errorprone.matchers.Description.NO_MATCH; |
24 | 27 | import static com.google.errorprone.util.ASTHelpers.constValue; |
25 | 28 | import static com.google.errorprone.util.ASTHelpers.getStartPosition; |
|
40 | 43 | import com.google.errorprone.ErrorProneFlags; |
41 | 44 | import com.google.errorprone.VisitorState; |
42 | 45 | import com.google.errorprone.bugpatterns.BugChecker.IfTreeMatcher; |
| 46 | +import com.google.errorprone.bugpatterns.SwitchUtils.Validity; |
43 | 47 | import com.google.errorprone.bugpatterns.threadsafety.ConstantExpressions; |
44 | 48 | import com.google.errorprone.fixes.SuggestedFix; |
45 | 49 | import com.google.errorprone.fixes.SuggestedFixes; |
46 | | -import com.google.errorprone.matchers.CompileTimeConstantExpressionMatcher; |
47 | 50 | import com.google.errorprone.matchers.Description; |
48 | | -import com.google.errorprone.matchers.Matcher; |
49 | 51 | import com.google.errorprone.suppliers.Suppliers; |
50 | 52 | import com.google.errorprone.util.ASTHelpers; |
51 | 53 | import com.google.errorprone.util.ErrorProneComment; |
@@ -88,18 +90,6 @@ public final class IfChainToSwitch extends BugChecker implements IfTreeMatcher { |
88 | 90 | // it's either an ExpressionStatement or a Throw. Refer to JLS 14 §14.11.1 |
89 | 91 | private static final ImmutableSet<Kind> KINDS_CONVERTIBLE_WITHOUT_BRACES = |
90 | 92 | ImmutableSet.of(THROW, EXPRESSION_STATEMENT); |
91 | | - private static final Matcher<ExpressionTree> COMPILE_TIME_CONSTANT_MATCHER = |
92 | | - CompileTimeConstantExpressionMatcher.instance(); |
93 | | - |
94 | | - /** |
95 | | - * Tri-state of whether the if-chain is valid, invalid, or possibly valid for conversion to a |
96 | | - * switch. |
97 | | - */ |
98 | | - enum Validity { |
99 | | - MAYBE_VALID, |
100 | | - INVALID, |
101 | | - VALID |
102 | | - } |
103 | 93 |
|
104 | 94 | private final boolean enableMain; |
105 | 95 | private final boolean enableSafe; |
@@ -349,14 +339,6 @@ private static Range<Integer> buildCommentRange(ErrorProneComment comment, int i |
349 | 339 | return Range.closedOpen(comment.getPos() + ifTreeStart, comment.getEndPos() + ifTreeStart); |
350 | 340 | } |
351 | 341 |
|
352 | | - /** Render the supplied comments, separated by newlines. */ |
353 | | - private static String renderComments(ImmutableList<ErrorProneComment> comments) { |
354 | | - return comments.stream() |
355 | | - .map(ErrorProneComment::getText) |
356 | | - .filter(commentText -> !commentText.isEmpty()) |
357 | | - .collect(joining("\n")); |
358 | | - } |
359 | | - |
360 | 342 | /** |
361 | 343 | * Renders Java source code representation of the supplied {@code Type} that is suitable for use |
362 | 344 | * in fixes, where any raw types are replaced with wildcard types. For example, `List` becomes |
@@ -468,7 +450,7 @@ && isSubtype( |
468 | 450 | boolean hasPattern = cases.stream().anyMatch(x -> x.instanceOfOptional().isPresent()); |
469 | 451 |
|
470 | 452 | boolean allEnumValuesPresent = |
471 | | - isEnum(subject, state) |
| 453 | + isEnumValue(subject, state) |
472 | 454 | && handledEnumValues.containsAll(ASTHelpers.enumValues(switchType.asElement())); |
473 | 455 |
|
474 | 456 | if (hasDefault && hasUnconditional) { |
@@ -869,10 +851,6 @@ private IfChainAnalysisState analyzeIfStatement( |
869 | 851 | ImmutableSet.copyOf(handledEnumValues)); |
870 | 852 | } |
871 | 853 |
|
872 | | - private static boolean isEnum(ExpressionTree tree, VisitorState state) { |
873 | | - return isSubtype(getType(tree), state.getSymtab().enumSym.type, state); |
874 | | - } |
875 | | - |
876 | 854 | /** Determines whether any yield or break statements are present in the tree. */ |
877 | 855 | private static boolean hasBreakOrYieldInTree(Tree tree) { |
878 | 856 | Boolean result = |
@@ -958,7 +936,7 @@ private Optional<ExpressionTree> validatePredicateForSubject( |
958 | 936 | hasElseIf); |
959 | 937 | } else { |
960 | 938 | // Predicate is a binary tree, but neither side is a constant. |
961 | | - if (isEnum(lhs, state) || isEnum(rhs, state)) { |
| 939 | + if (isEnumValue(lhs, state) || isEnumValue(rhs, state)) { |
962 | 940 | return validateEnumPredicateForSubject( |
963 | 941 | lhs, |
964 | 942 | rhs, |
@@ -1236,8 +1214,8 @@ private Optional<ExpressionTree> validateEnumPredicateForSubject( |
1236 | 1214 | int caseEndPosition, |
1237 | 1215 | boolean hasElse, |
1238 | 1216 | boolean hasElseIf) { |
1239 | | - boolean lhsIsEnumConstant = isEnum(lhs, state) && ASTHelpers.isEnumConstant(lhs); |
1240 | | - boolean rhsIsEnumConstant = isEnum(rhs, state) && ASTHelpers.isEnumConstant(rhs); |
| 1217 | + boolean lhsIsEnumConstant = isEnumValue(lhs, state) && ASTHelpers.isEnumConstant(lhs); |
| 1218 | + boolean rhsIsEnumConstant = isEnumValue(rhs, state) && ASTHelpers.isEnumConstant(rhs); |
1241 | 1219 |
|
1242 | 1220 | if (lhsIsEnumConstant && rhsIsEnumConstant) { |
1243 | 1221 | // Comparing enum const to enum const, cannot convert |
@@ -1549,7 +1527,7 @@ public static boolean isDominatedBy( |
1549 | 1527 | continue; |
1550 | 1528 | } |
1551 | 1529 | } |
1552 | | - boolean isEnum = isEnum(constantExpression, state); |
| 1530 | + boolean isEnum = isEnumValue(constantExpression, state); |
1553 | 1531 | if (isEnum) { |
1554 | 1532 | if (lhs.guardOptional().isPresent()) { |
1555 | 1533 | // Guarded patterns cannot dominate enum values |
|
0 commit comments