-
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 no ownership of the vector #8538
Comments
rust-lang/rust-clippy#8538 warning: `drain(..)` used on a `Vec` --> src/variance.rs:32:10 | 32 | .drain(..) | ^^^^^^^^^ help: try this: `into_iter()` | = note: `#[warn(clippy::iter_with_drain)]` on by default = 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. |
This also applies even if you have ownership of the fn example(mut vec: Vec<u32>) -> Vec<u32> {
vec.drain(..).for_each(|x| println!("{}", x);
vec
} |
Can confirm, we experienced in godot-rust with a |
This issue should be reopened because clippy is still giving false positives. Reproducible example: fn main() {
let mut my_vec = Vec::new();
for i in 0..10 {
my_vec.push(i);
for x in my_vec.drain(..) {
println!("{x}");
}
}
} False positive:
Clippy version: |
That's a distinctly different issue due to conflicting borrows. A new issue should be opened if one already isn't. |
Summary
The
iter_with_drain
lint says to replacevec.drain(..)
withvec.into_iter()
. It sounds like what this has in mind is theimpl<T> IntoIterator for Vec<T>
, but only the owner of the vec could call that, which does not work in general for all uses ofdrain(..)
.Replacing
vec.drain(..)
withvec.into_iter()
when vec is a mutable reference ends up callingimpl<T> IntoIterator for &mut Vec<T>
which has a different element type thandrain(..)
, so that usually wouldn't work. Also, clippy would immediately triggerinto_iter_on_ref
in that case, telling you to remove the.into_iter()
in favor of.iter_mut()
.Lint Name
iter_with_drain
Reproducer
Following the suggestion causes an error.
Version
Additional Labels
@rustbot label +I-suggestion-causes-error
The text was updated successfully, but these errors were encountered: