boolean (PsiLocalVariable variable, PsiReferenceExpression expression) { if (!(variable.getParent() instanceof PsiDeclarationStatement decl)) return false; PsiStatement statement = PsiTreeUtil.getNextSiblingOfType(decl, PsiStatement.class); if (statement == null) return false; PsiElement parent; PsiExpression cur = expression; while (true) { parent = cur.getParent(); if (parent instanceof PsiPolyadicExpression poly) { if (poly.getOperands()[0] != cur) return false; cur = poly; } else if (parent instanceof PsiParenthesizedExpression || parent instanceof PsiTypeCastExpression || parent instanceof PsiUnaryExpression || parent instanceof PsiInstanceOfExpression || parent instanceof PsiSwitchExpression) { cur = (PsiExpression)parent; } else if (parent instanceof PsiConditionalExpression cond) { if (cond.getCondition() != cur) return false; cur = cond; } else if (parent instanceof PsiExpressionList list) { for (PsiExpression expr : list.getExpressions()) { if (expr == cur) break; if (!ExpressionUtils.isSafelyRecomputableExpression(expr)) return false; } if (!(list.getParent() instanceof PsiCallExpression call)) return false; PsiExpression qualifier = call instanceof PsiMethodCallExpression methodCall ? methodCall.getMethodExpression().getQualifierExpression() : call instanceof PsiNewExpression newExpression ? newExpression.getQualifier() : null; if (qualifier != null && !ExpressionUtils.isSafelyRecomputableExpression(qualifier)) return false; cur = call; } else if (parent instanceof PsiReferenceExpression ref) { if (parent.getParent() instanceof PsiMethodCallExpression call) { cur = call; } else { cur = ref; } } else if (parent instanceof PsiArrayAccessExpression arr) { if (arr.getIndexExpression() == cur && !ExpressionUtils.isSafelyRecomputableExpression(arr)) return false; cur = arr; } else if (parent instanceof PsiArrayInitializerExpression init) { for (PsiExpression initializer : init.getInitializers()) { if (initializer == cur) break; if (!ExpressionUtils.isSafelyRecomputableExpression(initializer)) return false; } cur = init; } else if (parent instanceof PsiNewExpression newExpression) { cur = newExpression; } else if (parent instanceof PsiLocalVariable var) { return var.getParent() == statement; } else if (parent instanceof PsiStatement) { if (parent != statement) return false; if (parent instanceof PsiAssertStatement || parent instanceof PsiReturnStatement || parent instanceof PsiIfStatement || parent instanceof PsiExpressionStatement || parent instanceof PsiSynchronizedStatement || parent instanceof PsiSwitchStatement || parent instanceof PsiWhileStatement || parent instanceof PsiForeachStatement) { return true; } return false; } else { return false; } } }