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.21k stars 1.5k forks source link

lint `vec.clone().into_iter().stuff().collect()` to suggest `vec.iter().stuff().cloned().collect()` #3302

Open oli-obk opened 5 years ago

oli-obk commented 5 years ago

Found the following code in the wild

 let w: Vec<String> = vec_of_string
        .clone()
        .into_iter()
        .filter(|s| s.len() < 10)
        .collect();

Not sure what other things should be linted, but at least filter and filter_map after a clone+into_iter

scottmcm commented 5 years ago

I think this can be split into two parts:

hcpl commented 5 years ago

Throwing a wild idea into the mix: sort adaptors by their individual performance impact and then find a global optimum of the combination. I believe this will allow for a more generic lint. As for an implementation, a guided brute-force should do if the adaptor chain is short, say, < 10-15 elements; if not, it's too long anyway (is there a lint for that already?).

GnomedDev commented 2 months ago

I feel like that would be way too hard to implement and seems to have caused this idea to get lost for a couple years. A lint for .clone().into_iter() seems like quite an easy thing to implement.