Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Request: Static Operator #2395

Closed
2 tasks done
marcelofabri opened this issue Sep 10, 2018 · 0 comments
Closed
2 tasks done

Rule Request: Static Operator #2395

marcelofabri opened this issue Sep 10, 2018 · 0 comments
Labels
rule-request Requests for a new rules.

Comments

@marcelofabri
Copy link
Collaborator

New Issue Checklist

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.

  1. 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

  1. 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
    }
}
  1. Should the rule be configurable, if so what parameters should be configurable?

Only severity.

  1. 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.

@marcelofabri marcelofabri added the rule-request Requests for a new rules. label Sep 10, 2018
@realm-probot realm-probot bot added O:User rule-request Requests for a new rules. labels Sep 10, 2018
sjavora pushed a commit to sjavora/SwiftLint that referenced this issue Mar 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

1 participant