SwiftFormat icon indicating copy to clipboard operation
SwiftFormat copied to clipboard

Rule idea: redundantSwiftVersionCheck

Open nicklockwood opened this issue 1 year ago • 4 comments

If SwiftFormat's --swiftversion is set to (say) 5.2 then it implies that all the code in the codebase being formatted must be Swift 5.2+ compliant. In which case any conditional compilation checks like this are redundant :

#if swift(>=5.2)
   ...
#endif

We could add a rule that removes these. Potentially we could also add a similar rule for minimum deployment targets, but that would require additional configuration.

nicklockwood avatar Aug 31 '24 09:08 nicklockwood

One complication for such a rule would be distinguishing between Swift 6 in Swift 6 mode and Swift 6 in Swift 5 mode, as there's no --swiftversion which refers to Swift 6 in Swift 5 mode (AFAIK).

jshier avatar Sep 25 '24 18:09 jshier

There is now 😄: https://github.com/nicklockwood/SwiftFormat/issues/1880

--swiftversion 6.0 --languagemode 5

calda avatar Sep 25 '24 18:09 calda

In what ways does the #if swift condition interact with the compiler version / language mode distinction?

calda avatar Sep 25 '24 18:09 calda

It allows you to detect enumerated versions directly, otherwise you need to have other checks.

#if swift(>=6)
  // Swift 6+ compiler in Swift 6 mode.
#elseif compiler(>=6)
  // Swift 6+ compiler in Swift 5 mode.
#else
  // Earlier Swift version.
#endif

jshier avatar Sep 25 '24 20:09 jshier