SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

comma_inheritance breaks protocol composition in associated types

Open sjmadsen opened this issue 2 years ago • 3 comments

New Issue Checklist

Describe the bug

When two or more protocols are part of a protocol composition on an associated type, changing the & to , can cause a syntax error. For example:

protocol Transformable {
    associatedtype Unit: Equatable & CaseIterable & UnitConvertible

    ...
}

protocol UnitConvertible where Self: Equatable {
    ...
}

When using , instead of &, compilation results in Redundant conformance constraint 'Self.Unit' : 'Equatable'. In this example, it is important that the associated type Unit is declared as the composition of Equatable, CaseIterable, and UnitConvertible.

Complete output when running SwiftLint, including the stack trace and command used

n/a - SwiftLint runs fine. It's just too eager about suggesting that all & be changed to , in protocol compositions. Since it's in the default rule set, swiftlint lint --fix can introduce syntax errors.

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.47.1
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? Homebrew
  • Paste your configuration file:
opt_in_rules:
  - closure_end_indentation
  - contains_over_first_not_nil
  - empty_count
  - empty_string
  - first_where
  - implicit_return
  - modifier_order
  - nimble_operator
  - operator_usage_whitespace
  - overridden_super_call
  - prefer_nimble
  - prohibited_super_call
  - sorted_first_last
  - trailing_closure
  - unused_import
disabled_rules:
  - identifier_name
  - private_over_fileprivate
  - type_name
inclusive_language:
  override_terms:
    - blacklist
    - whitelist
    - slave
line_length:
  warning: 300
  error: 400
nesting:
  type_level:
    warning: 2
type_body_length:
  warning: 220
  • Are you using nested configurations? Yes, but not applicable to this problem. If so, paste their relative paths and respective contents.
  • Which Xcode version are you using (check xcodebuild -version)? 13.4.0
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules to quickly test if your example is really demonstrating the issue. If your example is more complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.

sjmadsen avatar Jul 16 '22 21:07 sjmadsen

It's unfortunate that Swift doesn't treat both in the same way - we probably should file an issue in their GitHub about it. However, I don't think there's much we can do from SwiftLint's side here without moving this to be an analyzer rule (which would make it require a full compilation, besides being much slower).

marcelofabri avatar Jul 26 '22 19:07 marcelofabri

At a minimum, the autocorrect should be turned off. A slightly more aggressive fix is removing it from the default rule set.

sjmadsen avatar Jul 26 '22 19:07 sjmadsen

Feel free to put up a PR disabling autocorrect!

marcelofabri avatar Jul 26 '22 19:07 marcelofabri