Skip to content

Commit 7adf0a0

Browse files
fix: close #11357 (wip)
1 parent 7408f1d commit 7adf0a0

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

clippy_lints/src/redundant_closure_call.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use clippy_utils::sugg::Sugg;
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
8-
use rustc_hir::intravisit as hir_visit;
98
use rustc_hir::intravisit::{Visitor as HirVisitor, Visitor};
9+
use rustc_hir::{intravisit as hir_visit, AsyncGeneratorKind, GeneratorKind};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_middle::hir::nested_filter;
1212
use rustc_middle::lint::in_external_macro;
@@ -62,9 +62,9 @@ impl<'tcx> Visitor<'tcx> for ReturnVisitor {
6262

6363
/// Checks if the body is owned by an async closure
6464
fn is_async_closure(body: &hir::Body<'_>) -> bool {
65-
if let hir::ExprKind::Closure(closure) = body.value.kind
66-
&& let [resume_ty] = closure.fn_decl.inputs
67-
&& let hir::TyKind::Path(hir::QPath::LangItem(hir::LangItem::ResumeTy, ..)) = resume_ty.kind
65+
if let hir::ExprKind::Closure(_) = body.value.kind
66+
&& let Some(kind) = body.generator_kind
67+
&& kind == GeneratorKind::Async(AsyncGeneratorKind::Closure)
6868
{
6969
true
7070
} else {
@@ -136,7 +136,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
136136
}
137137

138138
if let hir::ExprKind::Call(recv, _) = expr.kind
139-
// don't lint if the receiver is a call, too.
139+
// don't lint if the receiver is a call, too.
140140
// we do this in order to prevent linting multiple times; consider:
141141
// `(|| || 1)()()`
142142
// ^^ we only want to lint for this call (but we walk up the calls to consider both calls).
@@ -155,14 +155,15 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
155155
let mut applicability = Applicability::MachineApplicable;
156156
let mut hint = Sugg::hir_with_context(cx, body, full_expr.span.ctxt(), "..", &mut applicability);
157157

158-
if generator_kind.is_async()
159-
&& let hir::ExprKind::Closure(closure) = body.kind
160-
{
161-
let async_closure_body = cx.tcx.hir().body(closure.body);
158+
if generator_kind.is_async() {
159+
// does the innermost closure returns another closure?
160+
if let hir::ExprKind::Closure(closure) = body.kind {
161+
let async_closure_body = cx.tcx.hir().body(closure.body);
162162

163-
// `async x` is a syntax error, so it becomes `async { x }`
164-
if !matches!(async_closure_body.value.kind, hir::ExprKind::Block(_, _)) {
165-
hint = hint.blockify();
163+
// `async x` is a syntax error, so it becomes `async { x }`
164+
if !matches!(async_closure_body.value.kind, hir::ExprKind::Block(_, _)) {
165+
hint = hint.blockify();
166+
}
166167
}
167168

168169
hint = hint.asyncify();

tests/ui/redundant_closure_call_early.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ fn main() {
1818
#[allow(clippy::try_err)]
1919
(|| -> Result<i32, i32> { Err(2)? })();
2020
}
21+
22+
// don't lint
23+
async fn issue_11357() {
24+
let x = (|| async { () })().await;
25+
}

0 commit comments

Comments
 (0)