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

needless_collect has no understanding of lifetimes #9219

Open
awused opened this issue Jul 21, 2022 · 0 comments
Open

needless_collect has no understanding of lifetimes #9219

awused opened this issue Jul 21, 2022 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@awused
Copy link

awused commented Jul 21, 2022

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 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

@awused awused added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 21, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

2 participants