@NotNull
  private static PsiElement correctAnchor(@NotNull PsiExpression expr,
                                          @NotNull PsiElement anchor,
                                          PsiExpression @NotNull [] occurrences) {
    if (!expr.isPhysical()) {
      expr = ObjectUtils.tryCast(expr.getUserData(ElementToWorkOn.PARENT), PsiExpression.class);
      if (expr == null) return anchor;
    }
    if (anchor instanceof PsiSwitchLabelStatementBase) {
      PsiSwitchBlock block = ((PsiSwitchLabelStatementBase)anchor).getEnclosingSwitchBlock();
      if (block == null) return anchor;
      anchor = block;
      if (anchor instanceof PsiExpression) {
        expr = (PsiExpression)anchor;
      }
    }
    if (anchor instanceof PsiStatement) {
      anchor = correctDueToSyntaxErrors(anchor);
    }
    Set<PsiExpression> allOccurrences = StreamEx.of(occurrences).filter(PsiElement::isPhysical).append(expr).toSet();
    PsiExpression firstOccurrence = Collections.min(allOccurrences, Comparator.comparing(e -> e.getTextRange().getStartOffset()));
    if (PsiUtil.isAvailable(JavaFeature.PATTERNS, anchor)) {
      PsiTypeCastExpression cast = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(firstOccurrence), PsiTypeCastExpression.class);
      if (cast != null) {
        PsiType castType = cast.getType();
        PsiExpression operand = cast.getOperand();
        if (castType != null && !(castType instanceof PsiPrimitiveType) && operand != null && operand.getType() != null && 
            !(castType.isAssignableFrom(operand.getType())) &&
            !(PsiUtil.skipParenthesizedExprUp(firstOccurrence.getParent()) instanceof PsiExpressionStatement)) {
          PsiInstanceOfExpression candidate = InstanceOfUtils.findPatternCandidate(cast);
          if (candidate != null && allOccurrences.stream()
            .map(occ -> ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(occ), PsiTypeCastExpression.class))
            .allMatch(occ -> occ != null && (occ == firstOccurrence || InstanceOfUtils.findPatternCandidate(occ) == candidate))) {
            return candidate;
          }
        }
      }
    }
    if (anchor instanceof PsiWhileStatement whileStatement) {
      PsiExpression condition = whileStatement.getCondition();
      if (condition != null && ContainerUtil.and(allOccurrences, occurrence -> PsiTreeUtil.isAncestor(whileStatement, occurrence, true))) {
        if (firstOccurrence != null && PsiTreeUtil.isAncestor(condition, firstOccurrence, false) &&
            !ExpressionUtils.isLoopInvariant(firstOccurrence, whileStatement)) {
          PsiPolyadicExpression polyadic = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(condition), PsiPolyadicExpression.class);
          if (polyadic != null && !PsiTreeUtil.isAncestor(firstOccurrence, polyadic, false) && 
              JavaTokenType.ANDAND.equals(polyadic.getOperationTokenType())) {
            PsiExpression operand = ContainerUtil.find(polyadic.getOperands(), op -> PsiTreeUtil.isAncestor(op, firstOccurrence, false));
            operand = PsiUtil.skipParenthesizedExprDown(operand);
            LOG.assertTrue(operand != null);
            return operand;
          }
          return condition;
        }
      }
    }
    if (firstOccurrence != null && CodeBlockSurrounder.canSurround(firstOccurrence) &&
        !PsiUtil.isAccessedForWriting(firstOccurrence)) {
      PsiExpression ancestorCandidate = ExpressionUtils.getTopLevelExpression(firstOccurrence);
      if (PsiTreeUtil.isAncestor(anchor, ancestorCandidate, false)) {
        PsiElement statement = CommonJavaRefactoringUtil.getParentStatement(ancestorCandidate, false);
        PsiElement extractable = statement == null ? PsiTreeUtil.getParentOfType(ancestorCandidate, PsiField.class) : statement;
        if (ContainerUtil.and(allOccurrences, occurrence ->
                                               PsiTreeUtil.isAncestor(extractable, occurrence, false) &&
                                               (!PsiTreeUtil.isAncestor(ancestorCandidate, occurrence, false) ||
                                                ReorderingUtils.canExtract(ancestorCandidate, occurrence) == ThreeState.NO))) {
          return firstOccurrence;
        }
      }
    }
    if (anchor instanceof PsiTryStatement && firstOccurrence != null) {
      PsiResourceList resourceList = ((PsiTryStatement)anchor).getResourceList();
      PsiElement parent = firstOccurrence.getParent();
      if (resourceList != null && parent instanceof PsiResourceExpression && parent.getParent() == resourceList
          && InheritanceUtil.isInheritor(firstOccurrence.getType(), CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE)) {
        return parent;
      }
    }
    if (anchor.getParent() instanceof PsiSwitchLabeledRuleStatement) {
      return ExpressionUtils.getTopLevelExpression(expr);
    }
    if (CommonJavaRefactoringUtil.isLoopOrIf(anchor.getParent())) return anchor;
    PsiElement child = locateAnchor(anchor);
    return child == null ? anchor : child;
  }
@NotNull
  private static PsiElement correctAnchor(@NotNull PsiExpression expr,
                                          @NotNull PsiElement anchor,
                                          PsiExpression @NotNull [] occurrences)