-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Prefer is_empty
to len()
#23682
Prefer is_empty
to len()
#23682
Conversation
Hm I think I may prefer to more officially establish a convention in this regard before migrating the codebase. Along those lines I'm curious if others have thoughts on this? I'm personally pretty ambivalent, I tend to reach for |
"is_empty()" is always going to run in constant time, whereas "len()" may take linear time for some containers. |
vecmap and bitvset still take linear time to answer is_empty if the answer is "true". If it's false they may do better depending on value locations. |
@Diggsey good call. @alexcrichton wdyt? |
I feel like the most commonly used collections for the compiler, Overall I'd still prefer a broader consensus for changes like this. Ideally this would be a rustc-specific lint as well, but that seems over the top for something so minor. |
☔ The latest upstream changes (presumably #23359) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #23796) made this pull request unmergeable. Please resolve the merge conflicts. |
7ee6aac
to
1d79eea
Compare
☔ The latest upstream changes (presumably #23936) made this pull request unmergeable. Please resolve the merge conflicts. |
I'm definitely inclined to prefer |
☔ The latest upstream changes (presumably #23857) made this pull request unmergeable. Please resolve the merge conflicts. |
@alexcrichton I think a lint is the way to go here, and probably not a rustc-specific one. I lack the necessary rustc-fu to make this happen though, could you mark this This lint should probably suggest
WDYT? |
☔ The latest upstream changes (presumably #23998) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #24177) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #24433) made this pull request unmergeable. Please resolve the merge conflicts. |
`s/(?<!\{ self)(?<=\.)len\(\) == 0/is_empty()/g`
`s/([^\(\s]+\.)len\(\) [(?:!=)>] 0/!$1is_empty()/g`
Ok I find the comments here pretty convincing, so I think this is fine to go in for now. We'll probably want to discuss a little more if we want a lint for something like this, but it seems beneficial regardless for rustc itself to be consistent. Thanks for being patient and doing so many rebases @tamird! |
@tamird That would make a great addition to https://github.com/Manishearth/rust-clippy EDIT: Filed as Manishearth/rust-clippy#32 |
+1 to |
r? @alexcrichton