rust-lang / rust-lang/rust-clippy

If future returns Pending, it must store waker

Open
#6,314 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-hard T-MIR
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

If future returns Pending, it should use the provided waker to notify executor that it can be polled again.
If the waker is not stored, the future will be unable to notify it and therefore it will be never polled again. Such a future is probably incorrect.

What is verified

(Probably imprecise, so just a proposal.)
For each MIR block B0 whose terminator is return clippy should check that there is least one maybe-predecessor statement (i.e. defined in a block B1, and B0 is reachable from B1 in CFG) which somehow uses cx.

Categories (optional)
  • Kind: correctness
Drawbacks

False-positive here is a future that sometimes diverges and sometimes terminates (because if a future always diverges that programmer should use futures_util::future::pending() instead). I think such features are rare but don't have any estimations or experiments.

Example
impl Future for MyFut {
    type Output = ...;
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match self.state() {
            // ...
            State::Foo => {
                  // oops, we've forgotten to store waker, future will be never polled again
                  Poll::Pending
             }
        }
   }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the proposed MIR check for return terminators, maybe-predecessor statements, CFG reachability, and uses of cx in Future::poll. Determine how Clippy represents these blocks and how a lint could distinguish stored wakers from other uses. Done means the lint reliably identifies the shown missing-waker case without unacceptable false positives.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.