Skip to content

Commit 99c7838

Browse files
committed
Auto merge of rust-lang#11967 - samueltardieu:issue-11959, r=llogiq
Do not consider `async { (impl IntoFuture).await }` as redundant changelog: [`redundant_async_block`]: do not trigger on `IntoFuture` instances Fix rust-lang#11959
2 parents 5ab2319 + e52405a commit 99c7838

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

clippy_lints/src/redundant_async_block.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ops::ControlFlow;
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::peel_blocks;
55
use clippy_utils::source::{snippet, walk_span_to_context};
6+
use clippy_utils::ty::implements_trait;
67
use clippy_utils::visitors::for_each_expr;
78
use rustc_errors::Applicability;
89
use rustc_hir::{Closure, CoroutineKind, CoroutineSource, Expr, ExprKind, MatchSource};
@@ -49,6 +50,9 @@ impl<'tcx> LateLintPass<'tcx> for RedundantAsyncBlock {
4950
let Some(expr) = desugar_await(peel_blocks(body_expr)) &&
5051
// The await prefix must not come from a macro as its content could change in the future.
5152
expr.span.eq_ctxt(body_expr.span) &&
53+
// The await prefix must implement Future, as implementing IntoFuture is not enough.
54+
let Some(future_trait) = cx.tcx.lang_items().future_trait() &&
55+
implements_trait(cx, cx.typeck_results().expr_ty(expr), future_trait, &[]) &&
5256
// An async block does not have immediate side-effects from a `.await` point-of-view.
5357
(!expr.can_have_side_effects() || desugar_async_block(cx, expr).is_some()) &&
5458
let Some(shortened_span) = walk_span_to_context(expr.span, span.ctxt())

tests/ui/redundant_async_block.fixed

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(unused, clippy::manual_async_fn)]
22
#![warn(clippy::redundant_async_block)]
33

4-
use std::future::Future;
4+
use std::future::{Future, IntoFuture};
55

66
async fn func1(n: usize) -> usize {
77
n + 1
@@ -189,3 +189,9 @@ fn await_from_macro_deep() -> impl Future<Output = u32> {
189189
// or return different things depending on its argument
190190
async { mac!(async { 42 }) }
191191
}
192+
193+
// Issue 11959
194+
fn from_into_future(a: impl IntoFuture<Output = u32>) -> impl Future<Output = u32> {
195+
// Do not lint: `a` is not equivalent to this expression
196+
async { a.await }
197+
}

tests/ui/redundant_async_block.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(unused, clippy::manual_async_fn)]
22
#![warn(clippy::redundant_async_block)]
33

4-
use std::future::Future;
4+
use std::future::{Future, IntoFuture};
55

66
async fn func1(n: usize) -> usize {
77
n + 1
@@ -189,3 +189,9 @@ fn await_from_macro_deep() -> impl Future<Output = u32> {
189189
// or return different things depending on its argument
190190
async { mac!(async { 42 }) }
191191
}
192+
193+
// Issue 11959
194+
fn from_into_future(a: impl IntoFuture<Output = u32>) -> impl Future<Output = u32> {
195+
// Do not lint: `a` is not equivalent to this expression
196+
async { a.await }
197+
}

0 commit comments

Comments
 (0)