PsiExpression (PsiExpression expression) { PsiElement parent = expression.getParent(); expression = PsiUtil.skipParenthesizedExprDown(expression); if (!(expression instanceof PsiReferenceExpression reference)) return expression; PsiLocalVariable variable = tryCast(reference.resolve(), PsiLocalVariable.class); if (variable == null) return expression; PsiExpression initializer = variable.getInitializer(); if (initializer == null) return expression; if (parent instanceof PsiBinaryExpression) { // Check if variable is reused in the same expression if (VariableAccessUtils.getVariableReferences(variable, parent).size() != 1) return expression; } PsiElement block = ControlFlowUtil.findCodeFragment(variable); PsiElement expressionContext = PsiTreeUtil.getParentOfType(expression, PsiMember.class, PsiLambdaExpression.class); if (expressionContext == null || PsiTreeUtil.isAncestor(block, expressionContext, true)) return expression; if (!HighlightControlFlowUtil.isEffectivelyFinal(variable, block, null)) return expression; ControlFlow flow; try { flow = ControlFlowFactory.getInstance(block.getProject()).getControlFlow(block, new LocalsControlFlowPolicy(block), false); } catch (AnalysisCanceledException e) { return expression; } int initializerEnd = flow.getEndOffset(initializer); int start = initializerEnd + 1; if (ControlFlowUtils.isVariableReferencedBeforeStatementEntry(flow, start, expression, variable, Set.of(initializerEnd))) { return expression; } return initializer; }