boolean (PsiVariable array) { PsiElement scope = PsiTreeUtil.getParentOfType(array, array instanceof PsiField ? PsiClass.class : PsiCodeBlock.class); if (scope == null) return false; return PsiTreeUtil.processElements(scope, e -> { if (!(e instanceof PsiReferenceExpression ref)) return true; if (!ref.isReferenceTo(array)) return true; PsiElement parent = PsiUtil.skipParenthesizedExprUp(ref.getParent()); if (parent instanceof PsiForeachStatement && PsiTreeUtil.isAncestor(((PsiForeachStatement)parent).getIteratedValue(), ref, false)) { return true; } if (parent instanceof PsiReferenceExpression) { if (isReferenceTo(getArrayFromLengthExpression((PsiExpression)parent), array)) return true; if (parent.getParent() instanceof PsiMethodCallExpression && MethodCallUtils.isCallToMethod((PsiMethodCallExpression)parent.getParent(), CommonClassNames.JAVA_LANG_OBJECT, null, "clone", PsiType.EMPTY_ARRAY)) { return true; } } return parent instanceof PsiArrayAccessExpression && !PsiUtil.isAccessedForWriting((PsiExpression)parent); }); }