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: use contains() instead of range(of:) != nil #2776

Closed
2 tasks done
ZevEisenberg opened this issue May 31, 2019 · 1 comment · Fixed by #2811
Closed
2 tasks done

Rule request: use contains() instead of range(of:) != nil #2776

ZevEisenberg opened this issue May 31, 2019 · 1 comment · Fixed by #2811
Labels
rule-request Requests for a new rules.

Comments

@ZevEisenberg
Copy link
Contributor

ZevEisenberg commented May 31, 2019

New Issue Checklist

New rule request

Similar to contains_over_first_not_nil (#1846) but for strings

  1. Why should this rule be added? It’s more idiomatic to ask whether the collection contains the element. Checking whether the range of the substring is nil is a more Obj-C way of doing it (e.g. with range.location == NSNotFound)
  2. Provide several examples of what would and wouldn't trigger violations.

Triggering:

if items.range(of: item) != nil {
    // items contains item
}

if items.range(of: item) == nil { // notice the negation of the previous example
    // items does not contain item
}

Not triggering:

if item.contains(item) {
    // items contains item
}

if !items.contains(item) { // notice the negation of the previous example
    // items does not contain item
}
  1. Should the rule be configurable, if so what parameters should be configurable? I don’t think it needs to be configurable.
  2. Should the rule be opt-in or enabled by default? Why? I think it should be on by default. It’s always clearer, in my opinion, to express your intent in checking containment, rather than grabbing the index and checking if it’s nil. Plus, it saves on the calculation and storage allocation for the range, which isn’t much, but is probably more than a Bool.
@marcelofabri marcelofabri added the rule-request Requests for a new rules. label May 31, 2019
@ZevEisenberg ZevEisenberg changed the title Rule request: use contains() instead of index(of:) != nil Rule request: use contains() instead of range(of:) != nil Jun 4, 2019
@ZevEisenberg
Copy link
Contributor Author

Fixed title. Makes more sense now.

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.

2 participants