rust-clippy
rust-clippy copied to clipboard
`needless_collect` has no understanding of lifetimes
Summary
needless_collect
doesn't consider if the iterator is borrowing values that are moved between the collect
call and where the collected values are used.
Lint Name
needless_collect
Reproducer
I tried this code:
fn main() {
let original = vec![1, 2];
let copied: Vec<_> = (&original).iter().copied().collect();
drop(original);
copied.into_iter().for_each(|e| println!("{e:?}"));
}
I saw this happen:
needless_collect
suggests this code, which references original
after I explicitly move it and the suggestion does not compile.
fn main() {
let original = vec![1, 2];
drop(original);
(&original).iter().copied().for_each(|e| println!("{e:?}"));
}
I expected to see this happen:
needless_collect
should not fire here since originals
does is moved after the collect
call but before the into_iter
call that it wants to eliminate.
Version
rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: x86_64-unknown-linux-gnu
release: 1.62.0
LLVM version: 14.0.5
Additional Labels
@rustbot label I-suggestion-causes-error