Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not consider async { (impl IntoFuture).await } as redundant #11967

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions clippy_lints/src/redundant_async_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ops::ControlFlow;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::peel_blocks;
use clippy_utils::source::{snippet, walk_span_to_context};
use clippy_utils::ty::implements_trait;
use clippy_utils::visitors::for_each_expr;
use rustc_errors::Applicability;
use rustc_hir::{Closure, CoroutineKind, CoroutineSource, Expr, ExprKind, MatchSource};
Expand Down Expand Up @@ -49,6 +50,9 @@ impl<'tcx> LateLintPass<'tcx> for RedundantAsyncBlock {
let Some(expr) = desugar_await(peel_blocks(body_expr)) &&
// The await prefix must not come from a macro as its content could change in the future.
expr.span.eq_ctxt(body_expr.span) &&
// The await prefix must implement Future, as implementing IntoFuture is not enough.
let Some(future_trait) = cx.tcx.lang_items().future_trait() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

This check means that the whole lint will be deactivated if lang items have no future.

That's totally OK, I just wanted to make that pun.

implements_trait(cx, cx.typeck_results().expr_ty(expr), future_trait, &[]) &&
// An async block does not have immediate side-effects from a `.await` point-of-view.
(!expr.can_have_side_effects() || desugar_async_block(cx, expr).is_some()) &&
let Some(shortened_span) = walk_span_to_context(expr.span, span.ctxt())
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/redundant_async_block.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused, clippy::manual_async_fn)]
#![warn(clippy::redundant_async_block)]

use std::future::Future;
use std::future::{Future, IntoFuture};

async fn func1(n: usize) -> usize {
n + 1
Expand Down Expand Up @@ -189,3 +189,9 @@ fn await_from_macro_deep() -> impl Future<Output = u32> {
// or return different things depending on its argument
async { mac!(async { 42 }) }
}

// Issue 11959
fn from_into_future(a: impl IntoFuture<Output = u32>) -> impl Future<Output = u32> {
// Do not lint: `a` is not equivalent to this expression
async { a.await }
}
8 changes: 7 additions & 1 deletion tests/ui/redundant_async_block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused, clippy::manual_async_fn)]
#![warn(clippy::redundant_async_block)]

use std::future::Future;
use std::future::{Future, IntoFuture};

async fn func1(n: usize) -> usize {
n + 1
Expand Down Expand Up @@ -189,3 +189,9 @@ fn await_from_macro_deep() -> impl Future<Output = u32> {
// or return different things depending on its argument
async { mac!(async { 42 }) }
}

// Issue 11959
fn from_into_future(a: impl IntoFuture<Output = u32>) -> impl Future<Output = u32> {
// Do not lint: `a` is not equivalent to this expression
async { a.await }
}