nicklockwood / SwiftFormat

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

Fixes `redundantReturn` for switch with associated value matching #1497

Closed gcox closed 1 year ago

gcox commented 1 year ago

Prior to this, redundant returns in the following switch statements would not be removed using Swift 5.9:

func test(_ value: SomeEnum) -> String {
  switch value {
  case let .first(str):
      return "first \\(str)"
  case .second("str"):
      return "second"
  default:
      return "default"
  }
}
func test(_ value: SomeEnum) -> String {
  switch value {
  case .first(let str):
      return "first \\(str)"
  case .second("str"):
      return "second"
  default:
      return "default"
  }
}

The fix was a minor change to Formatter.switchStatementBranches(at:) to explicitly find the first .startOfScope(":") after the current case branch's start.

codecov[bot] commented 1 year ago

Codecov Report

Merging #1497 (f6cbe5d) into develop (4042960) will increase coverage by 0.27%. The diff coverage is 95.08%.

:exclamation: Current head f6cbe5d differs from pull request most recent head e12b71d. Consider uploading reports for the commit e12b71d to get more accurate results

@@             Coverage Diff             @@
##           develop    #1497      +/-   ##
===========================================
+ Coverage    94.91%   95.18%   +0.27%     
===========================================
  Files           20       18       -2     
  Lines        21064    20969      -95     
===========================================
- Hits         19993    19960      -33     
+ Misses        1071     1009      -62     
Files Changed Coverage Δ
Sources/JSONReporter.swift 94.44% <ø> (ø)
Sources/Reporter.swift 100.00% <ø> (ø)
Sources/GrammaticalNumber.swift 41.97% <41.97%> (ø)
Sources/Options.swift 95.71% <77.77%> (-0.29%) :arrow_down:
Sources/FormattingHelpers.swift 97.75% <97.52%> (-0.02%) :arrow_down:
Sources/ParsingHelpers.swift 96.83% <98.82%> (+0.01%) :arrow_up:
Sources/Rules.swift 98.41% <99.82%> (+<0.01%) :arrow_up:
Sources/Arguments.swift 94.88% <100.00%> (ø)
Sources/Examples.swift 99.93% <100.00%> (ø)
Sources/OptionDescriptor.swift 99.12% <100.00%> (+0.54%) :arrow_up:

... and 4 files with indirect coverage changes