PsiElementVisitor (@NotNull ProblemsHolder holder, boolean isOnTheFly) { return new JavaElementVisitor() { @Override public void visitInstanceOfExpression(@NotNull PsiInstanceOfExpression expression) { PsiPrimaryPattern pattern = expression.getPattern(); if (pattern == null) { return; } //without support deconstruction if (assignedVariablesInCondition(expression) || !(pattern instanceof PsiTypeTestPattern typeTestPattern) || typeTestPattern.getPatternVariable() == null) { return; } LocalQuickFix quickFix = new ConvertInstanceOfPatternToCastFix(pattern, tryToPreserveUnusedVariables); if (InspectionProjectProfileManager.isInformationLevel(getShortName(), pattern)) { holder.registerProblem(expression, TextRange.create(expression.getOperand().getStartOffsetInParent(), pattern.getStartOffsetInParent() + pattern.getTextLength()), JavaBundle.message("inspection.message.pattern.variables.can.be.replaced.with.cast"), quickFix); } else { holder.registerProblem(expression, TextRange.create(pattern.getStartOffsetInParent(), pattern.getStartOffsetInParent() + pattern.getTextLength()), JavaBundle.message("inspection.message.pattern.variables.can.be.replaced.with.cast"), quickFix); } } private static boolean assignedVariablesInCondition(PsiInstanceOfExpression psiInstanceOfExpression) { List<PsiPatternVariable> variables = JavaPsiPatternUtil.getExposedPatternVariables(psiInstanceOfExpression); PsiElement upperLevel = getUpperLevelOfCondition(psiInstanceOfExpression); for (PsiPatternVariable variable : variables) { if (VariableAccessUtils.variableIsAssigned(variable, upperLevel)) { return true; } } PsiConditionalLoopStatement conditionalLoopStatement = PsiTreeUtil.getParentOfType(psiInstanceOfExpression, PsiConditionalLoopStatement.class); if (conditionalLoopStatement instanceof PsiForStatement forStatement) { PsiStatement update = forStatement.getUpdate(); for (PsiPatternVariable variable : variables) { if (VariableAccessUtils.variableIsAssigned(variable, update)) { return true; } } } return false; } private static PsiElement getUpperLevelOfCondition(PsiExpression expression) { PsiElement current = expression; PsiElement parent = current.getParent(); while (parent instanceof PsiParenthesizedExpression || parent instanceof PsiPolyadicExpression || parent instanceof PsiPrefixExpression || parent instanceof PsiConditionalExpression) { current = parent; parent = current.getParent(); } return current; } }; }