Skip to content

Commit 9a1e7c5

Browse files
committed
Factor out clippy_utils::get_async_closure_expr()
Also, be stricter in matching the right expansion of async closures.
1 parent 2d13963 commit 9a1e7c5

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

clippy_utils/src/lib.rs

+20-13
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};
@@ -2137,28 +2137,35 @@ pub fn is_async_fn(kind: FnKind<'_>) -> bool {
21372137
}
21382138
}
21392139

2140-
/// Peels away all the compiler generated code surrounding the body of an async function,
2141-
pub fn get_async_fn_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'_>) -> Option<&'tcx Expr<'tcx>> {
2142-
if let ExprKind::Closure(&Closure { body, .. }) = body.value.kind {
2143-
if let ExprKind::Block(
2140+
/// Peels away all the compiler generated code surrounding the body of an async closure.
2141+
pub fn get_async_closure_expr<'tcx>(tcx: TyCtxt<'tcx>, expr: &Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
2142+
if let ExprKind::Closure(&Closure {
2143+
body,
2144+
kind: hir::ClosureKind::Coroutine(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)),
2145+
..
2146+
}) = expr.kind
2147+
&& let ExprKind::Block(
21442148
Block {
2145-
stmts: [],
21462149
expr:
21472150
Some(Expr {
2148-
kind: ExprKind::DropTemps(expr),
2151+
kind: ExprKind::DropTemps(inner_expr),
21492152
..
21502153
}),
21512154
..
21522155
},
21532156
_,
21542157
) = tcx.hir_body(body).value.kind
2155-
{
2156-
return Some(expr);
2157-
}
2158+
{
2159+
return Some(inner_expr);
21582160
}
21592161
None
21602162
}
21612163

2164+
/// Peels away all the compiler generated code surrounding the body of an async function.
2165+
pub fn get_async_fn_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'_>) -> Option<&'tcx Expr<'tcx>> {
2166+
get_async_closure_expr(tcx, body.value)
2167+
}
2168+
21622169
// check if expr is calling method or function with #[must_use] attribute
21632170
pub fn is_must_use_func_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
21642171
let did = match expr.kind {

0 commit comments

Comments
 (0)