rust-lang / rust-lang/rust-clippy
Clippy makes a broken correction to bad collect()
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Playground to the sample working code:
Clippy suggests changing the main function from this:
let size = 3;
let ids: HashSet<&str> = HashSet::new();
let chunked_ids = ids
.iter()
.map(|s| s.to_string())
.chunks(size)
.into_iter()
.map(|chunk| chunk.collect())
.collect::<Vec<Vec<String>>>();
let tasks = chunked_ids
.into_iter()
.map(|c| tokio::spawn(async move { fetch_chunk(&c, size).await }));
let _set = futures::future::join_all(tasks).await;
To this:
let size = 3;
let ids: HashSet<&str> = HashSet::new();
let tasks = ids
.iter()
.map(|s| s.to_string())
.chunks(size)
.into_iter()
.map(|chunk| chunk.collect())
.map(|c| tokio::spawn(async move { fetch_chunk(&c, size).await }));
let _set = futures::future::join_all(tasks).await;
(Note it also inserts an extra unneeded whitespace, not sure what for, but that isn't the main issue)
This won't compile though:

I think it meant to do this:
let size = 3;
let ids: HashSet<&str> = HashSet::new();
let chunks = ids
.iter()
.map(|s| s.to_string())
.chunks(size);
let tasks = chunks
.into_iter()
.map(|chunk| chunk.collect::<Vec<String>>())
.map(|c| tokio::spawn(async move { fetch_chunk(&c, size).await }));
let _set = futures::future::join_all(tasks).await;
Reproducer
See summary
Version
rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-pc-windows-msvc
release: 1.61.0
LLVM version: 14.0.0
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Rust Playground reproducer and identify which Clippy lint produces the broken collect() suggestion. Compare the suggested correction with the compiling example in the issue; done means the diagnostic produces a correction that compiles and preserves the intended chunk collection behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100