InitializerUsageStatus (PsiVariable var, PsiStatement statement) { if(!(var instanceof PsiLocalVariable) || var.getInitializer() == null) return UNKNOWN; if(isDeclarationJustBefore(var, statement)) return DECLARED_JUST_BEFORE; // Check that variable is declared in the same method or the same lambda expression if(PsiTreeUtil.getParentOfType(var, PsiLambdaExpression.class, PsiMethod.class) != PsiTreeUtil.getParentOfType(statement, PsiLambdaExpression.class, PsiMethod.class)) return UNKNOWN; PsiElement block = PsiUtil.getVariableCodeBlock(var, null); if(block == null) return UNKNOWN; final ControlFlow controlFlow; try { controlFlow = ControlFlowFactory.getInstance(statement.getProject()) .getControlFlow(block, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance()); } catch (AnalysisCanceledException ignored) { return UNKNOWN; } int start = controlFlow.getEndOffset(var.getInitializer())+1; int stop = controlFlow.getStartOffset(statement); if (isVariableReferencedBeforeStatementEntry(controlFlow, start, statement, var, Collections.emptySet())) return UNKNOWN; if (!ControlFlowUtil.isValueUsedWithoutVisitingStop(controlFlow, start, stop, var)) return AT_WANTED_PLACE_ONLY; return var.hasModifierProperty(PsiModifier.FINAL) ? UNKNOWN : AT_WANTED_PLACE; }