boolean (@Nullable PsiExpression expression, @NotNull PsiVariable variable, boolean builderPattern) { if (expression == null || variable.isUnnamed()) { return false; } if (expression instanceof PsiParenthesizedExpression parenthesizedExpression) { final PsiExpression containedExpression = parenthesizedExpression.getExpression(); return mayEvaluateToVariable(containedExpression, variable, builderPattern); } if (expression instanceof PsiTypeCastExpression typeCastExpression) { final PsiExpression containedExpression = typeCastExpression.getOperand(); return mayEvaluateToVariable(containedExpression, variable, builderPattern); } if (expression instanceof PsiConditionalExpression conditional) { final PsiExpression thenExpression = conditional.getThenExpression(); final PsiExpression elseExpression = conditional.getElseExpression(); return mayEvaluateToVariable(thenExpression, variable, builderPattern) || mayEvaluateToVariable(elseExpression, variable, builderPattern); } if (expression instanceof PsiArrayAccessExpression arrayAccessExpression) { final PsiElement parent = expression.getParent(); if (parent instanceof PsiArrayAccessExpression) { return false; } final PsiType type = variable.getType(); if (!(type instanceof PsiArrayType arrayType)) { return false; } final int dimensions = arrayType.getArrayDimensions(); if (dimensions <= 1) { return false; } PsiExpression arrayExpression = arrayAccessExpression.getArrayExpression(); int count = 1; while (arrayExpression instanceof PsiArrayAccessExpression) { arrayAccessExpression = (PsiArrayAccessExpression)arrayExpression; arrayExpression = arrayAccessExpression.getArrayExpression(); count++; } return count != dimensions && mayEvaluateToVariable(arrayExpression, variable, builderPattern); } if (builderPattern && expression instanceof PsiMethodCallExpression methodCallExpression) { final PsiMethod method = methodCallExpression.resolveMethod(); if (method == null) { return false; } final PsiType returnType = method.getReturnType(); final PsiType variableType = variable.getType(); if (!variableType.equals(returnType)) { return false; } final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifier = methodExpression.getQualifierExpression(); return mayEvaluateToVariable(qualifier, variable, true); } return ExpressionUtils.isReferenceTo(expression, variable); }