Skip to content

Commit 4fafcdf

Browse files
markhbradyError Prone Team
authored andcommitted
[IfChainToSwitch] refactor InstanceOfIr to use Optional instead of @Nullable, like in the rest of the rest of this class
PiperOrigin-RevId: 862765305
1 parent d0d061e commit 4fafcdf

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

core/src/main/java/com/google/errorprone/bugpatterns/IfChainToSwitch.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,14 @@ private static String prettyPrint(
273273
} else if (caseIr.instanceOfOptional().isPresent()) {
274274
InstanceOfIr instanceOfIr = caseIr.instanceOfOptional().get();
275275
sb.append("case ");
276-
if (instanceOfIr.patternVariable() != null) {
276+
if (instanceOfIr.patternVariable().isPresent()) {
277277
sb.append(
278278
printRawTypesAsWildcards(
279-
getType(instanceOfIr.patternVariable()), state, suggestedFixBuilder));
279+
getType(instanceOfIr.patternVariable().get()), state, suggestedFixBuilder));
280280

281-
Symbol sym = ASTHelpers.getSymbol(instanceOfIr.patternVariable());
281+
Symbol sym = ASTHelpers.getSymbol(instanceOfIr.patternVariable().get());
282282
sb.append(" ").append(sym.getSimpleName()).append(" ");
283-
} else if (instanceOfIr.type() != null && instanceOfIr.expression() != null) {
283+
} else if (instanceOfIr.expression().isPresent()) {
284284
sb.append(
285285
printRawTypesAsWildcards(getType(instanceOfIr.type()), state, suggestedFixBuilder));
286286
// It's possible that "unused" could conflict with an existing local variable name;
@@ -441,6 +441,7 @@ private Optional<List<CaseIr>> maybeFixDefaultNullAndUnconditional(
441441

442442
boolean hasDefault = cases.stream().anyMatch(CaseIr::hasDefault);
443443
boolean hasCaseNull = cases.stream().anyMatch(CaseIr::hasCaseNull);
444+
// NOMUTANTS -- this is a performance optimization
444445
boolean recheckDominanceNeeded = false;
445446

446447
boolean switchOnNullWouldImplicitlyThrow = switchOnNullWouldImplicitlyThrow(subject, cases);
@@ -1062,7 +1063,9 @@ private static Optional<ExpressionTree> validateInstanceofForSubject(
10621063
/* hasDefault= */ false,
10631064
/* instanceOfOptional= */ Optional.of(
10641065
new InstanceOfIr(
1065-
instanceOfTree.getExpression(), bpt.getVariable(), instanceOfTree.getType())),
1066+
Optional.ofNullable(instanceOfTree.getExpression()),
1067+
Optional.ofNullable(bpt.getVariable()),
1068+
instanceOfTree.getType())),
10661069
/* guardOptional= */ Optional.empty(),
10671070
/* expressionsOptional= */ Optional.empty(),
10681071
/* arrowRhsOptional= */ arrowRhsOptional,
@@ -1099,7 +1102,10 @@ private static Optional<ExpressionTree> validateInstanceofForSubject(
10991102
/* hasCaseNull= */ false,
11001103
/* hasDefault= */ false,
11011104
/* instanceOfOptional= */ Optional.of(
1102-
new InstanceOfIr(instanceOfTree.getExpression(), null, instanceOfTree.getType())),
1105+
new InstanceOfIr(
1106+
Optional.ofNullable(instanceOfTree.getExpression()),
1107+
Optional.empty(),
1108+
instanceOfTree.getType())),
11031109
/* guardOptional= */ Optional.empty(),
11041110
/* expressionsOptional= */ Optional.empty(),
11051111
/* arrowRhsOptional= */ arrowRhsOptional,
@@ -1526,8 +1532,8 @@ public static boolean isDominatedBy(
15261532
// Cannot unbox LHS pattern, so RHS primitive constant should come before it
15271533
return true;
15281534
}
1529-
} else if (instanceOfIr.patternVariable() != null) {
1530-
VariableTree patternVariable = instanceOfIr.patternVariable();
1535+
} else if (instanceOfIr.patternVariable().isPresent()) {
1536+
VariableTree patternVariable = instanceOfIr.patternVariable().get();
15311537
Type patternType = getType(patternVariable);
15321538
if (isSubtype(getType(constantExpression), patternType, state)) {
15331539
// RHS constant can be assigned to LHS pattern
@@ -1604,12 +1610,12 @@ public static boolean isDominatedBy(
16041610
Type lhsType =
16051611
lhs.instanceOfOptional().get().type() != null
16061612
? getType(lhs.instanceOfOptional().get().type())
1607-
: getType(lhs.instanceOfOptional().get().patternVariable().getType());
1613+
: getType(lhs.instanceOfOptional().get().patternVariable().get().getType());
16081614
var rhsInstanceOf = rhs.instanceOfOptional().get();
16091615
Type rhsType =
16101616
rhsInstanceOf.type() != null
16111617
? getType(rhsInstanceOf.type())
1612-
: getType(rhsInstanceOf.patternVariable().getType());
1618+
: getType(rhsInstanceOf.patternVariable().get().getType());
16131619
if (isSubtype(rhsType, lhsType, state)) {
16141620
// The LHS type is a subtype of the RHS type, so the LHS dominates the RHS
16151621
return true;
@@ -1626,9 +1632,9 @@ public static boolean isDominatedBy(
16261632
*/
16271633
record InstanceOfIr(
16281634
// In the example above, the expression would be `y`.
1629-
@Nullable ExpressionTree expression,
1635+
Optional<ExpressionTree> expression,
16301636
// In the example above, the variable tree would be `Y y`.
1631-
@Nullable VariableTree patternVariable,
1637+
Optional<VariableTree> patternVariable,
16321638
// In the example above, the type would be `Y`.
16331639
Tree type) {
16341640

0 commit comments

Comments
 (0)