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

Merged
merged 2 commits into from
Jan 31, 2025

Conversation

Pzixel
Copy link
Contributor

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

@Pzixel
Copy link
Contributor Author

Pzixel commented Jan 31, 2025

Ping?

@phimuemue
Copy link
Member

phimuemue commented Jan 31, 2025

Hi there, thanks for this.

Rust itself does it for collect, so I'm fine with that. But it does not do it for try_collect, and I'd like to know why.

If you want, cut this PR down to only collect, so that we can merge to be in line with Rust. Once we find an answer to the try_collect thing, we can revisit.

@Pzixel
Copy link
Contributor Author

Pzixel commented Jan 31, 2025

@phimuemue Fixed. I suppose the problem is that with try_collect you don't know what is the output type, so for some 3rd party type it might have sense to collect it without using, maybe. So I removed the try_collect_vec attribute for now.

@phimuemue phimuemue enabled auto-merge January 31, 2025 16:30
@phimuemue
Copy link
Member

Thanks.

@phimuemue phimuemue added this pull request to the merge queue Jan 31, 2025
Merged via the queue into rust-itertools:master with commit 67b0d8e Jan 31, 2025
9 of 12 checks passed
@phimuemue phimuemue mentioned this pull request Jan 31, 2025
@scottmcm
Copy link
Contributor

try_collect I think trusts the return type to get you that -- if you try_collect into a Result, then it's must-use from the result. (But also it's unstable and has a couple of things blocking it from stabilizing any time soon, so it might also just be that people haven't thought about it much in core.)

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.

3 participants