-
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
Warning about needless collect but suggestion does not compile #6420
Comments
@rustbot modify labels: +L-suggestion-causes-error |
Couldn't you use the |
Yeah, that would be better in most cases, but the actual code is more complicated unfortunately. In my case I want to loop over all keys of the map and for every key delete and add entries into the map until there is only one key left. |
This comment has been minimized.
This comment has been minimized.
@tigregalis The lint wasn't emitted at nightly |
@giraffate So it is. Thanks. |
I've found an even worse case: Code compiles… but the code logic changes: https://twitter.com/dmilith/status/1424370582179692550?s=20 |
Oh, so the |
Yes, I think so. Many false positives have been reported for |
I too have hit a few instances where this lint triggers and the suggestion is not valid because the original collection the iterator was collected from has been moved (e.g. with I've obfuscated the identifiers a bit but hopefully this will paint a picture: let a_b_indices: Vec<Option<usize>> = as
.iter()
.map(|a| {
bs
.iter()
.position(|b| b.id == a.id)
})
.collect(); // <-- clippy thinks this collect is needless
let b_a_indices: Vec<Option<usize>> = bs
.iter()
.map(|b| {
as
.iter()
.position(|a| b.id == a.id)
})
.collect(); // <-- clippy thinks this collect is needless
let as_with_contexts: Vec<_> =
as
.into_iter() // <-- this moves `as`
.zip(a_b_indices.into_iter()) // <-- clippy thinks the iterator that was collect()ed could be inlined here
.enumerate()
.map(|(a_index, (a, b_index))| {
let context = (a.id.clone(), a_index, b_index);
(a, context)
})
.collect();
let bs_with_contexts: Vec<_> =
bs
.into_iter() // <-- this moves `bs`
.zip(b_a_indices.into_iter()) // <-- clippy thinks the iterator that was collect()ed could be inlined here
.enumerate()
.map(|(b_index, (b, a_index))| {
let context = (b.id.clone(), a_index, b_index);
(b, context)
})
.collect(); I could probably do it in a nicer way I'm sure, but at least I can't see any way of avoiding the collect (at least not without |
Clippy warns about a needless collect, but the suggested code does not compile.
I have something similar to this code (condensed):
Suggestion from clippy:
This suggestion will not compile because
maps.keys().copied()
borrows the map.Full code example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=711425060fb8ac794003067454010a69
Expectation: the warning does not appear because removing the
.collect()
would break the code.Instead, this happened: the
needless_collect
warning triggered. Additionally, it's not possible to mute this warning for a specific line, so I resorted to adding#![allow(clippy::needless_collect)]
to the entire file.Meta
cargo +nightly clippy -V
: clippy 0.0.212 (1c389ff 2020-11-24) (I use the nightly clippy to get more warnings)rustc -Vv
:The text was updated successfully, but these errors were encountered: