mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-dfa] Do not report unreachable switch branches if they are throwing
GitOrigin-RevId: 5164e1b5f86abb797f7aa1a33b61fd28d85ca1d5
This commit is contained in:
committed by
intellij-monorepo-bot
parent
7452a08273
commit
42f43778e2
+22
@@ -369,6 +369,7 @@ public abstract class DataFlowInspectionBase extends AbstractBaseJavaLocalInspec
|
||||
unreachableLabels.put(label, switchBlock);
|
||||
}
|
||||
unreachableLabels.forEach((label, switchBlock) -> {
|
||||
if (isThrowing(label)) return;
|
||||
// duplicate case label is a compilation error so no need to highlight by the inspection
|
||||
Set<PsiElement> suspiciousElements = SwitchBlockHighlightingModel.findSuspiciousLabelElements(switchBlock);
|
||||
if (!suspiciousElements.contains(label)) {
|
||||
@@ -378,6 +379,27 @@ public abstract class DataFlowInspectionBase extends AbstractBaseJavaLocalInspec
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean isThrowing(PsiCaseLabelElement label) {
|
||||
PsiCaseLabelElementList caseLabelList = tryCast(label.getParent(), PsiCaseLabelElementList.class);
|
||||
if (caseLabelList == null) return false;
|
||||
PsiSwitchLabelStatementBase labelStatement = tryCast(caseLabelList.getParent(), PsiSwitchLabelStatementBase.class);
|
||||
if (labelStatement == null) return false;
|
||||
if (labelStatement instanceof PsiSwitchLabeledRuleStatement) {
|
||||
return ControlFlowUtils.stripBraces(((PsiSwitchLabeledRuleStatement)labelStatement).getBody()) instanceof PsiThrowStatement;
|
||||
}
|
||||
if (labelStatement instanceof PsiSwitchLabelStatement) {
|
||||
PsiElement cur = labelStatement;
|
||||
while(true) {
|
||||
PsiElement next = cur.getNextSibling();
|
||||
if (!(next instanceof PsiComment) && !(next instanceof PsiWhiteSpace) && !(next instanceof PsiSwitchLabelStatement)) {
|
||||
return next instanceof PsiThrowStatement;
|
||||
}
|
||||
cur = next;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean canRemoveUnreachableBranches(PsiSwitchLabelStatementBase labelStatement, PsiSwitchBlock statement) {
|
||||
if (Objects.requireNonNull(labelStatement.getCaseLabelElementList()).getElementCount() != 1) return true;
|
||||
List<PsiSwitchLabelStatementBase> allBranches =
|
||||
|
||||
@@ -54,4 +54,20 @@ public class SwitchStatements {
|
||||
case 2 -> System.out.println("two");
|
||||
}
|
||||
}
|
||||
|
||||
enum X {A, B, C}
|
||||
|
||||
void testThrowingBranch(X x) {
|
||||
if (x == X.A) return;
|
||||
switch(x) {
|
||||
case A -> throw new AssertionError();
|
||||
case B -> System.out.println(1);
|
||||
case C -> System.out.println(2);
|
||||
}
|
||||
switch(x) {
|
||||
case B: System.out.println(1);break;
|
||||
case C: System.out.println(2);break;
|
||||
case A: default: throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user