-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
add rule to encourage checking isEmpty
over comparing count
to zero
#206
Conversation
402560f
to
80f51f3
Compare
Checking if the type is a collection? |
How? |
Don't you have the type of the item from the sourcekit request? |
Not quite. You can get that if you have all the required compiler arguments for the given Swift file, which jazzy has but SwiftLint doesn't currently use. To get those, the project has to be fully compiled & indexed and we need to run |
Ah gotcha, I thought SwiftLint operated on the same source transformation |
80f51f3
to
f7342aa
Compare
874f9e7
to
0adf4fb
Compare
10d0f5c
to
4c52163
Compare
I've revived this now that we have opt-in rules. I'm making this an opt-in rule because it can easily lead to false positives as mentioned in the first comment in this PR. However, this is still tremendously useful as I constantly find myself comparing |
add rule to encourage checking `isEmpty` over comparing `count` to zero
also, with regard to the rule itself... if I have an optional array, |
This rule isn't so much about style as it is performance. Many data structures allow for more efficient checking if it's empty or not ( |
This already helped catch some inefficient calls in SwiftLint 🎉, but I don't think we can merge this as is currently implemented.
I can picture two false positives happening in practice with the way this rule is currently implemented:
count
. In this case, it'd be fine to checkcount
against zero.count
on types that don't incur a performance hit e.g.struct A { let count: Int }
.I'd appreciate any ideas to help address these two false positives.