PsiInstanceOfExpression (@Nullable PsiType castType, @NotNull PsiReferenceExpression operand, @NotNull PsiElement context) { if (!(castType instanceof PsiClassType classType)) { return null; } if (((PsiClassType)castType).resolve() instanceof PsiTypeParameter) { return null; } final PsiClassType rawType = classType.rawType(); final InstanceofChecker checker = new InstanceofChecker(operand, rawType, false); PsiStatement sibling = PsiTreeUtil.getParentOfType(context, PsiStatement.class); sibling = PsiTreeUtil.getPrevSiblingOfType(sibling, PsiStatement.class); while (sibling != null) { if (sibling instanceof PsiIfStatement ifStatement) { final PsiExpression condition = ifStatement.getCondition(); if (condition != null) { if (!ControlFlowUtils.statementMayCompleteNormally(ifStatement.getThenBranch())) { checker.negate = true; checker.checkExpression(condition); if (checker.hasAgreeingInstanceof()) { return null; } } else if (!ControlFlowUtils.statementMayCompleteNormally(ifStatement.getElseBranch())) { checker.negate = false; checker.checkExpression(condition); if (checker.hasAgreeingInstanceof()) { return null; } } } } else if (sibling instanceof PsiAssertStatement assertStatement) { final PsiExpression condition = assertStatement.getAssertCondition(); checker.negate = false; checker.checkExpression(condition); if (checker.hasAgreeingInstanceof()) { return null; } } else if (sibling instanceof PsiExpressionStatement) { PsiMethodCallExpression call = ObjectUtils.tryCast(((PsiExpressionStatement)sibling).getExpression(), PsiMethodCallExpression.class); if (isInstanceOfAssertionCall(checker, call)) return null; } sibling = PsiTreeUtil.getPrevSiblingOfType(sibling, PsiStatement.class); } checker.negate = false; PsiElement parent = findInterestingParent(context); while (parent != null) { IElementType tokenType = parent instanceof PsiPolyadicExpression ? ((PsiPolyadicExpression)parent).getOperationTokenType() : null; if (JavaTokenType.ANDAND.equals(tokenType) || JavaTokenType.OROR.equals(tokenType)) { checker.negate = tokenType.equals(JavaTokenType.OROR); for (PsiExpression expression : ((PsiPolyadicExpression)parent).getOperands()) { if (PsiTreeUtil.isAncestor(expression, context, false)) break; expression.accept(checker); if (checker.hasAgreeingInstanceof()) { return null; } } checker.negate = false; } else { parent.accept(checker); if (checker.hasAgreeingInstanceof()) { return null; } } parent = findInterestingParent(parent); } if (checker.hasAgreeingInstanceof()) { return null; } return checker.getConflictingInstanceof(); }