nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.92k stars 639 forks source link

"Failed to terminate" error thrown when applying `redundantReturn` to switch statement with a large number of cases #1499

Closed gcox closed 1 year ago

gcox commented 1 year ago

Given a switch statement with enough cases, applying the redundantReturn rule will throw a "Failed to terminate" error. Only guessing here, but I'd imagine other rules that process each case of a switch statement may have this limitation as well.

The limit currently seems to be 18 cases before you get a "Failed to terminate" error. Processing the switch statement below will throw the error, but removing any one of those cases will avoid it.

The problem also goes away if the default value for maxIterations is increased to 11. The more cases, the more you maxIterations needs to be increased.

func test(_ value: SomeEnum) -> String {
  switch value {
  case .one:
    return ""
  case .two:
    return ""
  case .three:
    return ""
  case .four:
    return ""
  case .five:
    return ""
  case .six:
    return ""
  case .seven:
    return ""
  case .eight:
    return ""
  case .nine:
    return ""
  case .ten:
    return ""
  case .eleven:
    return ""
  case .twelve:
    return ""
  case .thirteen:
    return ""
  case .fourteen:
    return ""
  case .fifteen:
    return ""
  case .sixteen:
    return ""
  case .seventeen:
    return ""
  case .eighteen:
    return ""
  case .nineteen:
    return ""
  }
}
nicklockwood commented 1 year ago

@gcox fixed in 0.52.0