Skip to content

Add a lint for an async block/closure that yields a type that is itself awaitable.#5909

Merged
bors merged 3 commits into
rust-lang:masterfrom
khuey:async_yields_async
Aug 31, 2020
Merged

Add a lint for an async block/closure that yields a type that is itself awaitable.#5909
bors merged 3 commits into
rust-lang:masterfrom
khuey:async_yields_async

Conversation

@khuey

@khuey khuey commented Aug 15, 2020

Copy link
Copy Markdown
Contributor

This catches bugs of the form

tokio::spawn(async move {
let f = some_async_thing();
f // Oh no I forgot to await f so that work will never complete.
});

See the two XXXkhuey comments and the unfixed _l structure for things that need more thought.

Please keep the line below
changelog: none

@rust-highfive

Copy link
Copy Markdown

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @flip1995 (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 15, 2020
@Manishearth Manishearth requested a review from yaahc August 15, 2020 00:59
@Manishearth

Copy link
Copy Markdown
Member

r? @yaahc bc you like async stuff iirc

@rust-highfive rust-highfive assigned yaahc and unassigned flip1995 Aug 15, 2020
@bors

bors commented Aug 16, 2020

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #5894) made this pull request unmergeable. Please resolve the merge conflicts.

@khuey khuey force-pushed the async_yields_async branch 2 times, most recently from 9d687ee to 3f1d5ee Compare August 16, 2020 22:54

@yaahc yaahc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, just have one test case that I think might be good to add, assuming I understood the comment in check_body correctly.

Comment thread clippy_lints/src/async_yields_async.rs
Comment thread tests/ui/async_yields_async.stderr
Comment thread clippy_lints/src/async_yields_async.rs
Comment thread clippy_lints/src/async_yields_async.rs
Comment thread clippy_lints/src/async_yields_async.rs Outdated
Comment thread clippy_lints/src/async_yields_async.rs Outdated
Comment thread clippy_lints/src/async_yields_async.rs
@khuey khuey force-pushed the async_yields_async branch from 1a27ec1 to b060058 Compare August 23, 2020 04:54
Comment on lines +66 to +67
let _n = async || custom_future_type_ctor();
let _o = async || f();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why aren't these two triggering the lint here? I can understand that their definitions shouldn't trigger the lint but these usages seem like they should.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of the limitations of having to match a zillion AST types to deduce return_expr_span :(

There's no case for these so they don't warn.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, do you mean the XXXhuey bit, as in, this doesn't resolve to either a Block or a Path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea it feels like there has to be a way to handle this...

@bors

bors commented Aug 25, 2020

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #5952) made this pull request unmergeable. Please resolve the merge conflicts.

khuey added 3 commits August 29, 2020 15:33
…lf awaitable.

This catches bugs of the form

tokio::spawn(async move {
    let f = some_async_thing();
    f // Oh no I forgot to await f so that work will never complete.
});
@khuey khuey force-pushed the async_yields_async branch from b060058 to 04912ca Compare August 29, 2020 22:34
@khuey

khuey commented Aug 29, 2020

Copy link
Copy Markdown
Contributor Author

Given that nobody knows a better way forward with return_expr_span, I'd like to land this as is and more cases can be added later.

@Manishearth

Copy link
Copy Markdown
Member

Seems fair. @yaahc ?

@yaahc

yaahc commented Aug 31, 2020

Copy link
Copy Markdown
Member

sounds good to me

@bors r+

@bors

bors commented Aug 31, 2020

Copy link
Copy Markdown
Contributor

📌 Commit 04912ca has been approved by yaahc

@bors

bors commented Aug 31, 2020

Copy link
Copy Markdown
Contributor

⌛ Testing commit 04912ca with merge 8334a58...

@bors

bors commented Aug 31, 2020

Copy link
Copy Markdown
Contributor

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: yaahc
Pushing 8334a58 to master...

@bors bors merged commit 8334a58 into rust-lang:master Aug 31, 2020
@SichangHe

Copy link
Copy Markdown

more improvement

this code should still be avoid:

tokio::spawn(async move {
    some_async_thing().await
});

if some_async_thing().await is the only expression in the closure && no statement is in the closure
it should be written as

tokio::spawn(some_async_thing());

correct me if I'm wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants