SpecialFirstIterationLoop (TerminalBlock terminalBlock) { List<PsiStatement> statements = List.of(terminalBlock.getStatements()); int index = getSingleStatementIndex(statements, PsiIfStatement.class::isInstance); if (index == -1) return null; PsiStatement statement = statements.get(index); PsiIfStatement ifStatement = tryCast(statement, PsiIfStatement.class); if (ifStatement == null) return null; ThreeState firstIterationThen = isFirstIterationThen(statement); if (firstIterationThen.equals(ThreeState.UNSURE)) return null; final ConditionData conditionData = extract(ifStatement, firstIterationThen.toBoolean()); if(conditionData == null) return null; PsiAssignmentExpression assignment = conditionData.assignment(); PsiExpression expression = assignment.getLExpression(); PsiLocalVariable boolFlag = resolveLocalVariable(expression); if(boolFlag == null) return null; PsiExpression rExpression = assignment.getRExpression(); if (rExpression == null) return null; if (!assignmentNegatesInitializer(boolFlag, rExpression)) return null; PsiExpression condition = ifStatement.getCondition(); boolean referencesAllowed = ReferencesSearch.search(boolFlag).forEach(reference -> PsiTreeUtil.isAncestor(condition, reference.getElement(), false) || PsiTreeUtil.isAncestor(assignment, reference.getElement(), false) || PsiTreeUtil.isAncestor(boolFlag, reference.getElement(), false)); if (!referencesAllowed) return null; return SpecialFirstIterationLoop .extract(firstIterationThen.toBoolean(), index, conditionData.thenStatements(), conditionData.elseStatements(), statements, boolFlag); }