@@ -5,8 +5,8 @@ use clippy_utils::sugg::Sugg;
5
5
use if_chain:: if_chain;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir as hir;
8
- use rustc_hir:: intravisit as hir_visit;
9
8
use rustc_hir:: intravisit:: { Visitor as HirVisitor , Visitor } ;
9
+ use rustc_hir:: { intravisit as hir_visit, AsyncGeneratorKind , GeneratorKind } ;
10
10
use rustc_lint:: { LateContext , LateLintPass } ;
11
11
use rustc_middle:: hir:: nested_filter;
12
12
use rustc_middle:: lint:: in_external_macro;
@@ -62,9 +62,9 @@ impl<'tcx> Visitor<'tcx> for ReturnVisitor {
62
62
63
63
/// Checks if the body is owned by an async closure
64
64
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 )
68
68
{
69
69
true
70
70
} else {
@@ -136,7 +136,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
136
136
}
137
137
138
138
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.
140
140
// we do this in order to prevent linting multiple times; consider:
141
141
// `(|| || 1)()()`
142
142
// ^^ 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 {
155
155
let mut applicability = Applicability :: MachineApplicable ;
156
156
let mut hint = Sugg :: hir_with_context ( cx, body, full_expr. span . ctxt ( ) , ".." , & mut applicability) ;
157
157
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 ) ;
162
162
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
+ }
166
167
}
167
168
168
169
hint = hint. asyncify ( ) ;
0 commit comments