Closed
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
New rule request
Operators should be declared as static functions, not free functions. Operators used to be declared as free functions but at some point (Swift 3?) they can also be declared as static
methods on the type that they operate.
- Why should this rule be added? Share links to existing discussion about what
the community thinks about this.
https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID42
- Provide several examples of what would and wouldn't trigger violations.
Non Triggering Examples
class A: Equatable {
static func == (lhs: A, rhs: A) -> Bool {
return false
}
class A<T>: Equatable {
static func == <T>(lhs: A<T>, rhs: A<T>) -> Bool {
return false
}
public extension Array where Element == Rule {
static func == (lhs: Array, rhs: Array) -> Bool {
if lhs.count != rhs.count { return false }
return !zip(lhs, rhs).contains { !$0.0.isEqualTo($0.1) }
}
}
private extension Optional where Wrapped: Comparable {
static func < (lhs: Optional, rhs: Optional) -> Bool {
switch (lhs, rhs) {
case let (lhs?, rhs?):
return lhs < rhs
case (nil, _?):
return true
default:
return false
}
}
}
Triggering Examples
↓func == (lhs: A, rhs: A) -> Bool {
return false
}
↓func == <T>(lhs: A<T>, rhs: A<T>) -> Bool {
return false
}
↓func == (lhs: [Rule], rhs: [Rule]) -> Bool {
if lhs.count != rhs.count { return false }
return !zip(lhs, rhs).contains { !$0.0.isEqualTo($0.1) }
}
private ↓func < <T: Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (lhs?, rhs?):
return lhs < rhs
case (nil, _?):
return true
default:
return false
}
}
- Should the rule be configurable, if so what parameters should be configurable?
Only severity.
- Should the rule be opt-in or enabled by default? Why?
See README.md for guidelines on when to mark a rule as opt-in.
Opt-in as a lot of code still uses free functions. Potentially there're also cases where declaring in a type is not possible.