-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
iter_with_drain false positive when vec capacity should be retained #8539
Comments
rust-lang/rust-clippy#8539 error: `drain(..)` used on a `Vec` --> src/constfn.rs:51:36 | 51 | out.extend(pending.drain(..)); | ^^^^^^^^^ help: try this: `into_iter()` | = note: `-D clippy::iter-with-drain` implied by `-D clippy::all` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_with_drain
Mentioning @ldm0 @flip1995 @giraffate since the lint is from #8483. |
@dtolnay Could you try |
|
#8538 This one can also be replaced with |
That also destroys the capacity because it replaces the old vector with an empty one that doesn't have any capacity. |
I also had concerns about this lint triggering on owned vs referenced vecs, but couldn't come up with a concrete example at the time (I probably should have thought about this harder). Together with #8538, I think this lint is not ready to be warn-by-default. But I also think this lint still needs some work, so I'll put it in nursery. (next sync is in a little over a week from now, so I won't do a out-of-cycle sync for this) |
I just ran into the same issue: let mut results = results.into_vec();
results.sort_unstable();
let entries = results
.drain(..)
.map_stuff...
.collect();
*search_result_buf = results; I want to reuse the |
I wrote another example of the false positive in an older issue: #8538 |
Summary
The
iter_with_drain
lint is aperformance
lint that says to replacevec.drain(..)
withvec.into_iter()
. This is unfortunate becauseinto_iter
destroys the capacity of a vector that you might have been planning to reuse, which is the opposite of performance.Lint Name
iter_with_drain
Reproducer
Following clippy's suggestion makes the code fail to build.
It's possible to get around that by recreating a new vector after
into_iter
has dropped the old one, but the resulting code is slower than we started with because the old capacity is being destroyed and reallocated in each loop.Version
Additional Labels
@rustbot label +I-suggestion-causes-error
The text was updated successfully, but these errors were encountered: