boolean (PsiExpression initializer, Branched branched) { if (ExpressionUtils.isSafelyRecomputableExpression(initializer) && !refersToNonFinalLocal(initializer)) return true; if (SideEffectChecker.mayHaveSideEffects(initializer)) return false; PsiIfStatement ifStatement = branched.ifStatement(); PsiDeclarationStatement declaration = PsiTreeUtil.getParentOfType(initializer, PsiDeclarationStatement.class); if (declaration == null || declaration.getParent() != ifStatement.getParent()) return false; PsiElement[] elements = declaration.getDeclaredElements(); if (elements.length > 1) { int i = ContainerUtil.indexOf(Arrays.asList(elements), e -> PsiTreeUtil.isAncestor(e, initializer, true)); for (int j = i + 1; j < elements.length; j++) { if (elements[j] instanceof PsiVariable var) { PsiExpression nextInitializer = var.getInitializer(); if (nextInitializer != null && SideEffectChecker.mayHaveSideEffects(nextInitializer)) return false; } } } PsiStatement nextStatement = declaration; while (true) { nextStatement = PsiTreeUtil.getNextSiblingOfType(nextStatement, PsiStatement.class); if (nextStatement == null) return false; if (nextStatement == ifStatement) break; if (nextStatement instanceof PsiDeclarationStatement decl) { if (StreamEx.of(decl.getDeclaredElements()).select(PsiLocalVariable.class).map(PsiLocalVariable::getInitializer).nonNull() .anyMatch(SideEffectChecker::mayHaveSideEffects)) { return false; } continue; } return false; } return branched.conditions().noneMatch(c -> SideEffectChecker.mayHaveSideEffects(c)); }