private static boolean isUsedInTopLevelFlowOnly(PsiElement element) {
    GrVariable var = null;
    if (element instanceof GrVariable) {
      var = (GrVariable)element;
    }
    else if (element instanceof GrReferenceExpression) {
      final PsiElement resolved = ((GrReferenceExpression)element).resolve();
      if (resolved instanceof GrVariable) var = (GrVariable)resolved;
    }

    if (var != null) {
      final GroovyPsiElement scope = ControlFlowUtils.findControlFlowOwner(var);
      if (scope == null) {
        PsiFile file = var.getContainingFile();
        if (file == null) {
          LOG.error("no file??? var of type" + var.getClass().getCanonicalName());
        }
        else {
          TextRange range = var.getTextRange();
          LOG.error("var: " + var.getName() + ", offset:" + (range != null ? range.getStartOffset() : -1) + ", owner: " + PsiTreeUtil.getParentOfType(var, GrControlFlowOwner.class));
        }
        return false;
      }

      return ReferencesSearch.search(var, var.getUseScope()).forEach(
        ref -> ControlFlowUtils.findControlFlowOwner(ref.getElement()) == scope);
    }

    return true;
  }
private static boolean isUsedInTopLevelFlowOnly(PsiElement element)