rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`redundant_async_block` not catching uses with nontrivial futures

Open jwodder opened this issue 2 years ago • 1 comments

Summary

The redundant_async_block lint fails to trigger on uses of async { EXPRESSION.await } for seemingly any EXPRESSION other than a single variable.

Lint Name

redundant_async_block

Reproducer

I tried this code:

use std::future::ready;

pub fn foo() {
    tokio::spawn(async { ready(42).await });
}

I expected to see this happen: clippy should have warned about the redundant async block and recommended just writing tokio::spawn(ready(42)) instead.

Instead, this happened: no warnings

Version

rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-apple-darwin
release: 1.77.2
LLVM version: 17.0.6

jwodder avatar Apr 29 '24 02:04 jwodder

They are semantically different if the expression has side effects.

async { some_function().await } only executes the function call on the first poll, whereas some_function() eagerly calls the function. ready happens to not have any side effects where this would be noticeable, but the lint doesn't know that and assumes that it does because it's a function call. https://github.com/rust-lang/rust-clippy/blob/0fc9a65b8f161314dfc5016472d949b6b3986e22/clippy_lints/src/redundant_async_block.rs#L59

See also #10509.

We could probably make it emit a warning when an async fn is called, because for those there is no difference, but that wouldn't fix this specific reproducer with the ready function, because that is a normal function returning a specific Ready type.

y21 avatar Apr 29 '24 16:04 y21