PsiInstanceOfExpression (@NotNull PsiTypeCastExpression cast, @NotNull PsiElement context, @Nullable PsiElement parent) { if (parent instanceof PsiIfStatement ifStatement) { if (ifStatement.getThenBranch() == context) { return findInstanceOf(ifStatement.getCondition(), cast, true); } else if (ifStatement.getElseBranch() == context) { return findInstanceOf(ifStatement.getCondition(), cast, false); } } if (parent instanceof PsiForStatement || parent instanceof PsiWhileStatement) { return findInstanceOf(((PsiConditionalLoopStatement)parent).getCondition(), cast, true); } return null; }