Skip to content

Commit f7a1cf9

Browse files
committedAug 22, 2023
fix: avoid off-by-one error on AST matching for desugared async closure
1 parent 8a655d0 commit f7a1cf9

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed
 

‎clippy_lints/src/redundant_closure_call.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ impl<'tcx> Visitor<'tcx> for ReturnVisitor {
6262

6363
/// Checks if the body is owned by an async closure
6464
fn is_async_closure(cx: &LateContext<'_>, body: &hir::Body<'_>) -> bool {
65-
if let hir::ExprKind::Closure(desugared_outer_closure) = body.value.kind
66-
&& let desugared_outer_closure_body = cx.tcx.hir().body(desugared_outer_closure.body)
67-
&& let hir::ExprKind::Closure(desugared_inner_closure) = desugared_outer_closure_body.value.kind
68-
// NB: `async || 42` is desugared into `|| |mut __context: #[lang = "ResumeTy"] | 42`; so
69-
// we need to check if the only input was that lang item.
70-
&& let [compiler_generated_resume_ty] = desugared_inner_closure.fn_decl.inputs
71-
&& let hir::TyKind::Path(hir::QPath::LangItem(hir::LangItem::ResumeTy, ..)) =
72-
compiler_generated_resume_ty.kind
65+
// NB: `async || 42` is desugared into `|| |mut __context: #[lang = "ResumeTy"] | 42`; so
66+
// we need to check if there is inner closure and check GeneratorKind of the inner one.
67+
if let hir::ExprKind::Closure(desugared_inner_closure) = body.value.kind
68+
&& let desugared_inner_closure_body = cx.tcx.hir().body(desugared_inner_closure.body)
69+
&& let Some(GeneratorKind::Async(AsyncGeneratorKind::Closure)) = desugared_inner_closure_body.generator_kind
7370
{
7471
true
7572
} else {

0 commit comments

Comments
 (0)