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: Legacy Multiple #2612

Closed
2 tasks done
marcelofabri opened this issue Jan 30, 2019 · 0 comments · Fixed by #2771
Closed
2 tasks done

Rule Request: Legacy Multiple #2612

marcelofabri opened this issue Jan 30, 2019 · 0 comments · Fixed by #2771
Labels
rule-request Requests for a new rules.

Comments

@marcelofabri
Copy link
Collaborator

marcelofabri commented Jan 30, 2019

New Issue Checklist

New rule request

Prefer using x.isMultiple(of: y) than x % y == 0.

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.

https://github.com/apple/swift-evolution/blob/master/proposals/0225-binaryinteger-iseven-isodd-ismultiple.md

  1. Provide several examples of what would and wouldn't trigger violations.

Would trigger

// UITableView alternating row colour
cell.contentView.backgroundColor = indexPath.row % 2 == 0 ? .gray : .white

// Codable.swift.gyb in apple/swift
guard count % 2 == 0 else { throw DecodingError.dataCorrupted(...) }

// Bool.swift in apple/swift
public static func random<T: RandomNumberGenerator>(using generator: inout T) -> Bool {
    return (generator.next() >> 17) & 1 == 0
}

// KeyPath.swift in apple/swift
_sanityCheck(bytes > 0 && bytes % 4 == 0, "capacity must be multiple of 4 bytes")

// ReversedCollection Index.base documentation https://developer.apple.com/documentation/swift/reversedcollection/index/2965437-base
guard let i = reversedNumbers.firstIndex(where: { $0 % 2 == 0 })

Wouldn't trigger

// UITableView alternating row colour
cell.contentView.backgroundColor = indexPath.row.isMultiple(of: 2) ? .gray : .white

// Codable.swift.gyb in apple/swift
guard count.isMultiple(of: 2) else { throw DecodingError.dataCorrupted(...) }

// Bool.swift in apple/swift
public static func random<T: RandomNumberGenerator>(using generator: inout T) -> Bool {
    return (generator.next() >> 17).isMultiple(of: 2)
}

// KeyPath.swift in apple/swift
_sanityCheck(bytes > 0 && bytes.isMultiple(of: 4), "capacity must be multiple of 4 bytes")

// not used in a boolean context
let secretValue = (value % 3) + 2
  1. Should the rule be configurable, if so what parameters should be configurable?

Just 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 isMultiple(of:) is only available on Swift 5+.

@marcelofabri marcelofabri added the rule-request Requests for a new rules. label Jan 30, 2019
marcelofabri added a commit that referenced this issue Jun 2, 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

Successfully merging a pull request may close this issue.

1 participant