Skip to content

Commit d849c4f

Browse files
graememorganError Prone Team
authored andcommitted
Scan the entire SwitchTree rather than individual cases, and reduce to a boolean rather than adding to a set.
PiperOrigin-RevId: 798233843
1 parent 49b3262 commit d849c4f

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ private static AnalysisResult analyzeSwitchTree(SwitchTree switchTree, VisitorSt
385385
// In principle, this search could be terminated after checking up through `caseTree`
386386
// (inclusive) because naming conflicts after that would have caused compile-time
387387
// errors. For simplicity, the search is not restricted.
388-
.filter(symbol -> declaresAnotherVariableNamed(symbol, cases))
388+
.filter(symbol -> declaresAnotherVariableNamed(symbol, switchTree))
389389
.findAny()
390390
.isPresent();
391391
if (hasNamingConflict) {
@@ -568,27 +568,26 @@ private void handle(Tree tree) {
568568
* Determines whether the switch statement has a case that declares a local variable with the same
569569
* name as the supplied {@code symbol}.
570570
*/
571-
private static boolean declaresAnotherVariableNamed(
572-
VarSymbol symbol, List<? extends CaseTree> cases) {
573-
574-
Set<VarSymbol> nameConflicts = new HashSet<>();
575-
for (CaseTree caseTree : cases) {
576-
new TreeScanner<Void, Void>() {
577-
@Override
578-
public Void visitVariable(VariableTree variableTree, Void unused) {
579-
// If the variable is named the same as the symbol, but it's not the original declaration
580-
// of the symbol, then there's a name conflict.
581-
if (variableTree.getName().contentEquals(symbol.name.toString())) {
582-
VarSymbol thisVarSymbol = ASTHelpers.getSymbol(variableTree);
583-
if (!thisVarSymbol.equals(symbol)) {
584-
nameConflicts.add(thisVarSymbol);
585-
}
571+
private static boolean declaresAnotherVariableNamed(VarSymbol symbol, SwitchTree switchTree) {
572+
return new TreeScanner<Boolean, Void>() {
573+
@Override
574+
public Boolean visitVariable(VariableTree variableTree, Void unused) {
575+
// If the variable is named the same as the symbol, but it's not the original declaration
576+
// of the symbol, then there's a name conflict.
577+
if (variableTree.getName().contentEquals(symbol.name.toString())) {
578+
VarSymbol thisVarSymbol = ASTHelpers.getSymbol(variableTree);
579+
if (!thisVarSymbol.equals(symbol)) {
580+
return true;
586581
}
587-
return super.visitVariable(variableTree, null);
588582
}
589-
}.scan(caseTree, null);
590-
}
591-
return !nameConflicts.isEmpty();
583+
return super.visitVariable(variableTree, null);
584+
}
585+
586+
@Override
587+
public Boolean reduce(@Nullable Boolean left, @Nullable Boolean right) {
588+
return Objects.equals(left, true) || Objects.equals(right, true);
589+
}
590+
}.scan(switchTree, null);
592591
}
593592

594593
/**

0 commit comments

Comments
 (0)