void (@NotNull PsiConditionalLoopStatement statement, @NotNull PsiPatternVariable variable, @NotNull PsiInstanceOfExpression psiInstanceOfExpression, @NotNull List<PsiReferenceExpression> references) { //trivial cases ConditionState conditionState = getConditionIfInstanceOfTrue(psiInstanceOfExpression, statement.getCondition()); List<PsiReferenceExpression> unusedReferences = new ArrayList<>(references); List<PsiReferenceExpression> referencesForInsideBlock = ContainerUtil.filter(unusedReferences, t -> PsiTreeUtil.isAncestor(statement.getBody(), t, false)); unusedReferences.removeAll(referencesForInsideBlock); if (statement.getBody() != null && !(statement.getBody() instanceof PsiEmptyStatement) && ((this.tryToPreserveUnusedVariables && conditionState == ConditionState.TRUE && !(statement instanceof PsiDoWhileStatement)) || !referencesForInsideBlock.isEmpty())) { addDeclarationInsideBlock(statement.getBody(), variable); } List<PsiReferenceExpression> referencesOutsideLoop = ContainerUtil.filter(unusedReferences, t -> !PsiTreeUtil.isAncestor(statement, t, false)); unusedReferences.removeAll(referencesOutsideLoop); boolean noBreak = PsiTreeUtil.processElements(statement, e -> !(e instanceof PsiBreakStatement) || ((PsiBreakStatement)e).findExitedStatement() != statement); if ((this.tryToPreserveUnusedVariables && conditionState == ConditionState.FALSE && noBreak) || !referencesOutsideLoop.isEmpty()) { addDeclarationOutsideBlock(statement, variable); } //other cases replaceWithCast(variable, unusedReferences); }