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

Prefer is_empty to len() #23682

Merged
merged 3 commits into from
Apr 16, 2015
Merged

Prefer is_empty to len() #23682

merged 3 commits into from
Apr 16, 2015

Conversation

tamird
Copy link
Contributor

@tamird tamird commented Mar 25, 2015

@alexcrichton
Copy link
Member

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 len first just because it's what I'm using 90% of the time elsewhere.

@Diggsey
Copy link
Contributor

Diggsey commented Mar 25, 2015

"is_empty()" is always going to run in constant time, whereas "len()" may take linear time for some containers.

@Gankra
Copy link
Contributor

Gankra commented Mar 25, 2015

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.

@tamird
Copy link
Contributor Author

tamird commented Mar 26, 2015

@Diggsey good call. @alexcrichton wdyt?

@alexcrichton
Copy link
Member

I feel like the most commonly used collections for the compiler, Vec and HashMap, it doesn't matter perf-wise one way or another, but it is indeed a good point that collections may have a more optimized version of is_empty than they do of len.

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.

@bors
Copy link
Contributor

bors commented Mar 27, 2015

☔ The latest upstream changes (presumably #23359) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Mar 28, 2015

☔ The latest upstream changes (presumably #23796) made this pull request unmergeable. Please resolve the merge conflicts.

@tamird tamird force-pushed the DRY-is-empty branch 4 times, most recently from 7ee6aac to 1d79eea Compare March 28, 2015 22:04
@bors
Copy link
Contributor

bors commented Apr 1, 2015

☔ The latest upstream changes (presumably #23936) made this pull request unmergeable. Please resolve the merge conflicts.

@gereeter
Copy link
Contributor

gereeter commented Apr 4, 2015

I'm definitely inclined to prefer is_empty, both because of the efficiency argument and because if we use len, then is_empty serves no purpose.

@bors
Copy link
Contributor

bors commented Apr 7, 2015

☔ The latest upstream changes (presumably #23857) made this pull request unmergeable. Please resolve the merge conflicts.

@tamird
Copy link
Contributor Author

tamird commented Apr 7, 2015

@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 E-mentor?

This lint should probably suggest is_empty() if the following predicates hold:

  • fn len(&self) -> usize is called on a receiver that implements an in-scope fn is_empty(&self) -> bool and:
    • len()'s return is compared to zero or
    • len()'s return is checked for being less than 1)

WDYT?

@bors
Copy link
Contributor

bors commented Apr 8, 2015

☔ The latest upstream changes (presumably #23998) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Apr 10, 2015

☔ The latest upstream changes (presumably #24177) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Apr 15, 2015

☔ The latest upstream changes (presumably #24433) made this pull request unmergeable. Please resolve the merge conflicts.

tamird added 3 commits April 14, 2015 20:26
`s/(?<!\{ self)(?<=\.)len\(\) == 0/is_empty()/g`
`s/([^\(\s]+\.)len\(\) [(?:!=)>] 0/!$1is_empty()/g`
@alexcrichton
Copy link
Member

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!

@bors: r+ c55ae1d

@bors
Copy link
Contributor

bors commented Apr 16, 2015

⌛ Testing commit c55ae1d with merge 288809c...

@bors bors merged commit c55ae1d into rust-lang:master Apr 16, 2015
@tamird tamird deleted the DRY-is-empty branch April 16, 2015 06:21
@lambda-fairy
Copy link
Contributor

@tamird That would make a great addition to https://github.com/Manishearth/rust-clippy

EDIT: Filed as Manishearth/rust-clippy#32

@nikomatsakis
Copy link
Contributor

+1 to is_empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants