SwiftLint
SwiftLint copied to clipboard
comma_inheritance breaks protocol composition in associated types
New Issue Checklist
- [x] Updated SwiftLint to the latest version
- [x] I searched for existing GitHub issues
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 useswiftlint lint --path [file here] --no-cache --enable-all-rules
.
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).
At a minimum, the autocorrect should be turned off. A slightly more aggressive fix is removing it from the default rule set.
Feel free to put up a PR disabling autocorrect!