rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.32k stars 1.53k forks source link

`needless_collect` false positive #7512

Open SamRodri opened 3 years ago

SamRodri commented 3 years ago

Lint name: needless_collect

Reduced code:

pub struct Foo {
    foo: bool,
    bar: bool,
}

pub fn test(foo: Vec<Foo>) {
    let bar: Vec<_> = foo.iter().map(|f|f.bar).collect();
    drop(foo);

    for (_, _) in bar.into_iter().enumerate() {

    }    
}

// pub fn incorrect_suggestion(foo: Vec<Foo>) {
//     
//     drop(foo);
// 
//     for (_, _) in foo.iter().map(|f|f.bar).enumerate() {
// 
//     }    
// }

I expected no warning here because foo is dropped. The incorrect suggestion also appears if foo is shadowed, potentially generating incorrect code that compiles.

Meta

sophiajt commented 3 years ago

Just coming here to +1 this. We're having to disable this lint globally in Nushell because of the number of false positives.

Similar issue reported earlier: https://github.com/rust-lang/rust-clippy/issues/7336