Skip to content

Commit

Permalink
Cleanup switch guard check and add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
coehlrich committed Aug 18, 2024
1 parent 428896e commit 0a8c54c
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,23 +365,23 @@ private static boolean eliminateGuardRef(SwitchStatement stat, Map<List<Exprent>
// alternatively, it can be inverted as `if (guardCond) { /* regular case code... */ break; } idx = __thisIdx + 1;`
if (reference.b instanceof AssignmentExprent) {
Statement assignStat = reference.a;
int statementIndex = -1;

// If a record pattern contains an `instanceof` check then a guard is used so
// attempt to eliminate it
for (int i = 0; i < stat.getCaseStatements().size(); i++) {
if (stat.getCaseStatements().get(i).containsStatement(assignStat)) {
statementIndex = i;
}
}
Statement patternStat = assignStat;
while (patternStat.getParent() != null && !(patternStat.getParent() instanceof SwitchStatement)) {
patternStat = patternStat.getParent();
}
PatternExprent pattern = identifySwitchRecordPatternMatch(stat, patternStat, simulate);
if (pattern != null && statementIndex != -1) {
if (!simulate) {
guards.put(stat.getCaseValues().get(statementIndex), pattern);
Statement patternStat = stat.getCaseStatements().get(i);
PatternExprent pattern = identifySwitchRecordPatternMatch(stat, patternStat, simulate);
if (pattern != null) {
if (!simulate) {
guards.put(stat.getCaseValues().get(i), pattern);
}
return true;
}
break;
}
return true;
}

// Note: This can probably be checked earlier
if (assignStat.getAllPredecessorEdges().size() > 1) {
return false;
Expand Down

0 comments on commit 0a8c54c

Please sign in to comment.