private static @Nullable SwitchReplacer inspectReturningSwitch(@NotNull PsiStatement statement,
                                                       @NotNull List<OldSwitchStatementBranch> branches,
                                                       boolean isExhaustive, int maxNumberStatementsForExpression) {
    PsiReturnStatement returnAfterSwitch =
      tryCast(PsiTreeUtil.getNextSiblingOfType(statement, PsiStatement.class), PsiReturnStatement.class);
    PsiThrowStatement throwAfterSwitch =
      tryCast(PsiTreeUtil.getNextSiblingOfType(statement, PsiStatement.class), PsiThrowStatement.class);
    if (returnAfterSwitch == null && throwAfterSwitch==null && !isExhaustive) return null;
    List<SwitchBranch> newBranches = new ArrayList<>();
    boolean hasReturningBranch = false;
    boolean isInfo = false;
    int maxLines = 0;
    for (int i = 0, size = branches.size(); i < size; i++) {
      OldSwitchStatementBranch branch = branches.get(i);
      if (!isConvertibleBranch(branch, i != size - 1)) return null;
      if (branch.isFallthrough()) continue;
      PsiStatement[] statements = branch.getStatements();
      if (statements.length == 1 && statements[0] instanceof PsiBlockStatement psiCodeBlock) {
        statements = psiCodeBlock.getCodeBlock().getStatements();
      }
      if (statements.length == 0) {
        if ((i == branches.size() - 1) || branch.isDefault()) {
          if (returnAfterSwitch != null) {
            statements = new PsiStatement[]{returnAfterSwitch};
          }
          else if (throwAfterSwitch != null) {
            statements = new PsiStatement[]{throwAfterSwitch};
          }
          else {
            return null;
          }
        }
        else {
          return null;
        }
      }
      if (maxLines < statements.length) {
        maxLines = statements.length;
      }
      if (statements.length > maxNumberStatementsForExpression) {
        isInfo = true;
      }
      int lastIndex = statements.length - 1;
      if (ContainerUtil.exists(statements,
                               st -> !PsiTreeUtil.findChildrenOfAnyType(st, PsiContinueStatement.class, PsiBreakStatement.class, PsiYieldStatement.class).isEmpty())) {
        return null;
      }
      PsiReturnStatement returnStmt = tryCast(statements[lastIndex], PsiReturnStatement.class);
      SwitchRuleResult result;
      if (returnStmt == null) {
        PsiThrowStatement throwStatement = tryCast(statements[lastIndex], PsiThrowStatement.class);
        if (throwStatement == null) return null;
        PsiStatement[] psiStatements = replaceAllReturnWithYield(statements);
        if (psiStatements == null) {
          return null;
        }
        result = new SwitchStatementBranch(psiStatements);
      }
      else {
        PsiExpression returnExpr = returnStmt.getReturnValue();
        if (returnExpr == null) return null;
        if (statements.length == 1) {
          result = new SwitchRuleExpressionResult(returnExpr);
        }
        else {
          PsiStatement[] psiStatements = replaceAllReturnWithYield(statements);
          if (psiStatements == null) {
            return null;
          }
          result = new SwitchStatementBranch(psiStatements);
        }
        hasReturningBranch = true;
      }
      newBranches.add(SwitchBranch.fromOldBranch(branch, result, branch.getUsedElements()));
    }
    if (!hasReturningBranch) return null;
    if (!isExhaustive && returnAfterSwitch != null) {
      PsiExpression returnExpr = returnAfterSwitch.getReturnValue();
      if (returnExpr == null) return null;
      newBranches.add(SwitchBranch.createDefault(new SwitchRuleExpressionResult(returnExpr)));
    }
    if (!isExhaustive && throwAfterSwitch != null) {
      newBranches.add(SwitchBranch.createDefault(new SwitchStatementBranch(new PsiStatement[]{throwAfterSwitch})));
    }
    List<PsiStatement> statementsToDelete = new ArrayList<>();
    if (isExhaustive && returnAfterSwitch == null && throwAfterSwitch == null) {
      PsiElement current = statement.getNextSibling();
      while (current != null) {
        if (current instanceof PsiStatement stmt) {
          if (current instanceof PsiSwitchLabelStatement) {
            break;
          }
          statementsToDelete.add(stmt);
          if (stmt instanceof PsiReturnStatement || stmt instanceof PsiThrowStatement) {
            break;
          }
        }
        current = current.getNextSibling();
      }
    }
    return new ReturningSwitchReplacer(statement, newBranches, returnAfterSwitch, throwAfterSwitch, statementsToDelete, isInfo, maxLines);
  }
private static @Nullable SwitchReplacer inspectReturningSwitch(@NotNull PsiStatement statement,
                                                       @NotNull List<OldSwitchStatementBranch> branches,
                                                       boolean isExhaustive, int maxNumberStatementsForExpression)