Skip to content

Commit 0fc2542

Browse files
authored
Fix primitives in pattern with an if statement with false (#531)
* Fix primitives in patterns when the if statement has `false` * Add null check * Add test
1 parent 2fea772 commit 0fc2542

File tree

3 files changed

+403
-306
lines changed

3 files changed

+403
-306
lines changed

src/org/jetbrains/java/decompiler/modules/decompiler/IfPatternMatchProcessor.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,17 +474,27 @@ record PatternStore(StructRecordComponent component, StructClass cl, VarType typ
474474
ok = true;
475475
}
476476
}
477-
} else if (ifSt.getHeadexprent().getCondition() instanceof ConstExprent constExp && constExp.hasBooleanValue() && (int) constExp.getValue() == 1) {
478-
BasicBlockStatement headStat = ifSt.getIfstat().getBasichead();
479-
List<Exprent> headExprents = headStat.getExprents();
480-
if (headExprents.size() >= 1
481-
&& headExprents.get(0) instanceof AssignmentExprent assignment
477+
} else if (ifSt.getHeadexprent().getCondition() instanceof ConstExprent constExp && constExp.hasBooleanValue()) {
478+
BasicBlockStatement stat = null;
479+
List<Exprent> exprents = null;
480+
if ((int) constExp.getValue() == 1) {
481+
stat = ifSt.getIfstat().getBasichead();
482+
exprents = stat.getExprents();
483+
} else if (stIdx + 1 < branch.getStats().size()) {
484+
stat = branch.getStats().get(stIdx + 1).getBasichead();
485+
exprents = stat.getExprents();
486+
inverted = true;
487+
}
488+
489+
if (stat != null
490+
&& exprents.size() >= 1
491+
&& exprents.get(0) instanceof AssignmentExprent assignment
482492
&& assignment.getLeft() instanceof VarExprent store
483493
&& assignment.getRight() instanceof VarExprent) {
484494
found = true;
485495
vars.put(c, store);
486496
ok = true;
487-
List<Exprent> toRemove = remove.computeIfAbsent(headStat, block -> new ArrayList<>());
497+
List<Exprent> toRemove = remove.computeIfAbsent(stat, block -> new ArrayList<>());
488498
toRemove.add(assignment);
489499
}
490500
}

0 commit comments

Comments
 (0)