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

Add "[must-use]" attribute into collect vec #1009

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Pzixel
Copy link

@Pzixel Pzixel commented Dec 24, 2024

This might lead to the problems as following:

    pub fn unique_id(&self) -> impl Hash + PartialEq + Eq {
        self.items.iter().map(|x| x.id_string.clone()).collect::<Vec<_>>();
    }

This code has a typo and rust warns against it:

unused return value of collect that must be used
if you really need to exhaust the iterator, consider .for_each(drop) instead
#[warn(unused_must_use)] on by defaultrustcClick for full compiler diagnostic
mod.rs(964, 9): use let _ = ... to ignore the resulting value: let _ =

If you replace it with following:

    pub fn unique_id(&self) -> impl Hash + PartialEq + Eq {
        self.items.iter().map(|x| x.id_string.clone()).collect_vec();
    }

No warning will be generated, as no must_use attribute exist on the function.

This PR adds it, while small, it can lead to huge problems, since () implements a lot of traits and impl Trait are used very often with iterators, so you can erroneously use one instead of another.

P.S. I also noticed that try_collect only works with #[cfg(feature = "use_alloc")], but try_collect doesn't. I'm not sure if this is intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant