Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8357d06

Browse files
committed
Factor out clippy_utils::get_async_closure_expr()
Also, be stricter in matching the right expansion of async closures.
1 parent a1e1960 commit 8357d06

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

clippy_utils/src/lib.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ use rustc_hir::hir_id::{HirIdMap, HirIdSet};
106106
use rustc_hir::intravisit::{FnKind, Visitor, walk_expr};
107107
use rustc_hir::{
108108
self as hir, Arm, BindingMode, Block, BlockCheckMode, Body, ByRef, Closure, ConstArgKind, ConstContext,
109-
Destination, Expr, ExprField, ExprKind, FnDecl, FnRetTy, GenericArg, GenericArgs, HirId, Impl, ImplItem,
110-
ImplItemKind, ImplItemRef, Item, ItemKind, LangItem, LetStmt, MatchSource, Mutability, Node, OwnerId, OwnerNode,
111-
Param, Pat, PatExpr, PatExprKind, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitFn, TraitItem,
112-
TraitItemKind, TraitItemRef, TraitRef, TyKind, UnOp, def,
109+
CoroutineDesugaring, CoroutineKind, Destination, Expr, ExprField, ExprKind, FnDecl, FnRetTy, GenericArg,
110+
GenericArgs, HirId, Impl, ImplItem, ImplItemKind, ImplItemRef, Item, ItemKind, LangItem, LetStmt, MatchSource,
111+
Mutability, Node, OwnerId, OwnerNode, Param, Pat, PatExpr, PatExprKind, PatKind, Path, PathSegment, PrimTy, QPath,
112+
Stmt, StmtKind, TraitFn, TraitItem, TraitItemKind, TraitItemRef, TraitRef, TyKind, UnOp, def,
113113
};
114114
use rustc_lexer::{TokenKind, tokenize};
115115
use rustc_lint::{LateContext, Level, Lint, LintContext};
@@ -2134,25 +2134,34 @@ pub fn is_async_fn(kind: FnKind<'_>) -> bool {
21342134
}
21352135
}
21362136

2137-
/// Peels away all the compiler generated code surrounding the body of an async function,
2138-
pub fn get_async_fn_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'_>) -> Option<&'tcx Expr<'tcx>> {
2139-
if let ExprKind::Closure(&Closure { body, .. }) = body.value.kind
2137+
/// Peels away all the compiler generated code surrounding the body of an async closure.
2138+
pub fn get_async_closure_expr<'tcx>(tcx: TyCtxt<'tcx>, expr: &Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
2139+
if let ExprKind::Closure(&Closure {
2140+
body,
2141+
kind: hir::ClosureKind::Coroutine(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)),
2142+
..
2143+
}) = expr.kind
21402144
&& let ExprKind::Block(
21412145
Block {
2142-
stmts: [],
21432146
expr:
21442147
Some(Expr {
2145-
kind: ExprKind::DropTemps(expr),
2148+
kind: ExprKind::DropTemps(inner_expr),
21462149
..
21472150
}),
21482151
..
21492152
},
21502153
_,
21512154
) = tcx.hir_body(body).value.kind
21522155
{
2153-
return Some(expr);
2156+
Some(inner_expr)
2157+
} else {
2158+
None
21542159
}
2155-
None
2160+
}
2161+
2162+
/// Peels away all the compiler generated code surrounding the body of an async function,
2163+
pub fn get_async_fn_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'_>) -> Option<&'tcx Expr<'tcx>> {
2164+
get_async_closure_expr(tcx, body.value)
21562165
}
21572166

21582167
// check if expr is calling method or function with #[must_use] attribute

0 commit comments

Comments
 (0)