@@ -293,49 +293,59 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
293
293
callee_node : & hir:: ExprKind < ' _ > ,
294
294
callee_span : Span ,
295
295
) {
296
+ let hir:: ExprKind :: Block ( ..) = callee_node else {
297
+ // Only calls on blocks suggested here.
298
+ return ;
299
+ } ;
300
+
296
301
let hir = self . tcx . hir ( ) ;
297
- let parent_hir_id = hir. parent_id ( hir_id) ;
298
- let parent_node = self . tcx . hir_node ( parent_hir_id) ;
299
- if let (
300
- hir:: Node :: Expr ( hir:: Expr {
301
- kind : hir:: ExprKind :: Closure ( & hir:: Closure { fn_decl_span, kind, .. } ) ,
302
+ let fn_decl_span = if let hir:: Node :: Expr ( hir:: Expr {
303
+ kind : hir:: ExprKind :: Closure ( & hir:: Closure { fn_decl_span, .. } ) ,
304
+ ..
305
+ } ) = hir. get_parent ( hir_id)
306
+ {
307
+ fn_decl_span
308
+ } else if let Some ( (
309
+ _,
310
+ hir:: Node :: Expr ( & hir:: Expr {
311
+ hir_id : parent_hir_id,
312
+ kind :
313
+ hir:: ExprKind :: Closure ( & hir:: Closure {
314
+ kind :
315
+ hir:: ClosureKind :: Coroutine ( hir:: CoroutineKind :: Desugared (
316
+ hir:: CoroutineDesugaring :: Async ,
317
+ hir:: CoroutineSource :: Closure ,
318
+ ) ) ,
319
+ ..
320
+ } ) ,
302
321
..
303
322
} ) ,
304
- hir:: ExprKind :: Block ( ..) ,
305
- ) = ( parent_node, callee_node)
323
+ ) ) = hir. parent_iter ( hir_id) . nth ( 3 )
306
324
{
307
- let fn_decl_span = if matches ! (
308
- kind,
309
- hir:: ClosureKind :: Coroutine ( hir:: CoroutineKind :: Desugared (
310
- hir:: CoroutineDesugaring :: Async ,
311
- hir:: CoroutineSource :: Closure
312
- ) , )
313
- ) {
314
- // Actually need to unwrap one more layer of HIR to get to
315
- // the _real_ closure...
316
- let async_closure = hir. parent_id ( parent_hir_id) ;
317
- if let hir:: Node :: Expr ( hir:: Expr {
318
- kind : hir:: ExprKind :: Closure ( & hir:: Closure { fn_decl_span, .. } ) ,
319
- ..
320
- } ) = self . tcx . hir_node ( async_closure)
321
- {
322
- fn_decl_span
323
- } else {
324
- return ;
325
- }
326
- } else {
325
+ // Actually need to unwrap one more layer of HIR to get to
326
+ // the _real_ closure...
327
+ let async_closure = hir. parent_id ( parent_hir_id) ;
328
+ if let hir:: Node :: Expr ( hir:: Expr {
329
+ kind : hir:: ExprKind :: Closure ( & hir:: Closure { fn_decl_span, .. } ) ,
330
+ ..
331
+ } ) = self . tcx . hir_node ( async_closure)
332
+ {
327
333
fn_decl_span
328
- } ;
334
+ } else {
335
+ return ;
336
+ }
337
+ } else {
338
+ return ;
339
+ } ;
329
340
330
- let start = fn_decl_span. shrink_to_lo ( ) ;
331
- let end = callee_span. shrink_to_hi ( ) ;
332
- err. multipart_suggestion (
333
- "if you meant to create this closure and immediately call it, surround the \
341
+ let start = fn_decl_span. shrink_to_lo ( ) ;
342
+ let end = callee_span. shrink_to_hi ( ) ;
343
+ err. multipart_suggestion (
344
+ "if you meant to create this closure and immediately call it, surround the \
334
345
closure with parentheses",
335
- vec ! [ ( start, "(" . to_string( ) ) , ( end, ")" . to_string( ) ) ] ,
336
- Applicability :: MaybeIncorrect ,
337
- ) ;
338
- }
346
+ vec ! [ ( start, "(" . to_string( ) ) , ( end, ")" . to_string( ) ) ] ,
347
+ Applicability :: MaybeIncorrect ,
348
+ ) ;
339
349
}
340
350
341
351
/// Give appropriate suggestion when encountering `[("a", 0) ("b", 1)]`, where the
0 commit comments