-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New lint: iter_overeager_cloned
#8202
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
Comments
I created a new PR for that lint: #8203 |
Iterators are lazy, so |
By themselves With |
Ok, I see your point. The benefit would be only in number of warnings from clippy for a not-strictly-necessary case. |
I think the examples for this are bad and should be updated: // Bad
vec.iter().cloned().take(10);
// Good
vec.iter().take(10).cloned(); These are equivalent, not a single clone happens. Even if we add // Bad
vec.iter().cloned().skip(10).next(); // 11 clones
// Good
vec.iter().skip(10).cloned().next(); // 0-1 clones // Bad
vec.iter().cloned().last();
// Good
vec.iter().last().cloned(); This has a typo, the good line should be |
No. |
What about the first part of my comment? |
What it does
Calling
.iter().cloned.last()
involved doing unnecessary work.It's better to write it as
.iter().last().cloned()
Same thing can rule can be applied to:
.iter().cloned().next()
.iter().cloned().count()
.iter().cloned().flatten()
.iter().cloned().take(...)
.iter().cloned().skip(...)
.iter().cloned().nth(...)
Lint Name
cloned_last
Category
style, perf
Advantage
Drawbacks
Example
Could be written as:
The text was updated successfully, but these errors were encountered: