rust-lang / rust-lang/rust-clippy
Don't pass `FuturesUnordered` to `join_all`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
The FuturesUnordered collection is a way to run code concurrently, however it is also possible to use FuturesUnordered as a linked-list stack and ignoring its concurrency features. The linked list can be created via collect on an iterator or with its push method. Then, its IntoIterator impl can be used to get the items back out in FILO order like a stack, destroying the FuturesUnordered in the progress.
This is what happens when you pass a FuturesUnordered to the join_all method. The futures are added to the collection, then immediately removed again when join_all collects the iterator its given into a Vec.
Lint Name
futures-unordered-with-join-all
Category
complexity
Advantage
Any code that passes a FuturesUnordered to join_all is semantically incorrect and should use a Vec instead.
Drawbacks
None.
Example
let tasks = tasks.
.into_iter()
.map(|task| my_async_fn(task))
.collect::<FuturesUnordered<_>>();
let outputs = join_all(tasks).await;
Could be written as:
let tasks = tasks.
.into_iter()
.map(|task| my_async_fn(task))
.collect::<Vec<_>>();
let outputs = join_all(tasks).await;
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
No implementation files or tests are named. Start by searching the repository for existing lint implementations and tests, then use the lint name futures-unordered-with-join-all and the supplied example to trace the relevant patterns. Done means the lint detects passing FuturesUnordered to join_all and recommends collecting into a Vec instead.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100