From 0a8c54c2eba1b7844541a216de7832b0ebd7f6e8 Mon Sep 17 00:00:00 2001 From: coehlrich Date: Mon, 19 Aug 2024 09:27:32 +1200 Subject: [PATCH] Cleanup switch guard check and add comment --- .../SwitchPatternMatchProcessor.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/SwitchPatternMatchProcessor.java b/src/org/jetbrains/java/decompiler/modules/decompiler/SwitchPatternMatchProcessor.java index 394511de5..0692ff61d 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/SwitchPatternMatchProcessor.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/SwitchPatternMatchProcessor.java @@ -365,23 +365,23 @@ private static boolean eliminateGuardRef(SwitchStatement stat, Map // 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;