@@ -1583,55 +1583,68 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
1583
1583
}
1584
1584
1585
1585
fn suggest_remove_await ( & self , obligation : & PredicateObligation < ' tcx > , err : & mut Diagnostic ) {
1586
- let span = obligation. cause . span ;
1587
-
1588
- if let ObligationCauseCode :: AwaitableExpr ( hir_id) = obligation. cause . code ( ) . peel_derives ( ) {
1589
- let hir = self . tcx . hir ( ) ;
1590
- if let Some ( hir:: Node :: Expr ( expr) ) = hir_id. and_then ( |hir_id| hir. find ( hir_id) ) {
1591
- // FIXME: use `obligation.predicate.kind()...trait_ref.self_ty()` to see if we have `()`
1592
- // and if not maybe suggest doing something else? If we kept the expression around we
1593
- // could also check if it is an fn call (very likely) and suggest changing *that*, if
1594
- // it is from the local crate.
1586
+ let hir = self . tcx . hir ( ) ;
1587
+ if let ObligationCauseCode :: AwaitableExpr ( Some ( hir_id) ) = obligation. cause . code ( ) . peel_derives ( )
1588
+ && let hir:: Node :: Expr ( expr) = hir. get ( * hir_id)
1589
+ {
1590
+ // FIXME: use `obligation.predicate.kind()...trait_ref.self_ty()` to see if we have `()`
1591
+ // and if not maybe suggest doing something else? If we kept the expression around we
1592
+ // could also check if it is an fn call (very likely) and suggest changing *that*, if
1593
+ // it is from the local crate.
1594
+
1595
+ // use nth(1) to skip one layer of desugaring from `IntoIter::into_iter`
1596
+ if let Some ( ( _, hir:: Node :: Expr ( await_expr) ) ) = hir. parent_iter ( * hir_id) . nth ( 1 )
1597
+ && let Some ( expr_span) = expr. span . find_ancestor_inside ( await_expr. span )
1598
+ {
1599
+ let removal_span = self . tcx
1600
+ . sess
1601
+ . source_map ( )
1602
+ . span_extend_while ( expr_span, char:: is_whitespace)
1603
+ . unwrap_or ( expr_span)
1604
+ . shrink_to_hi ( )
1605
+ . to ( await_expr. span . shrink_to_hi ( ) ) ;
1595
1606
err. span_suggestion (
1596
- span ,
1607
+ removal_span ,
1597
1608
"remove the `.await`" ,
1598
1609
"" ,
1599
1610
Applicability :: MachineApplicable ,
1600
1611
) ;
1601
- // FIXME: account for associated `async fn`s.
1602
- if let hir:: Expr { span, kind : hir:: ExprKind :: Call ( base, _) , .. } = expr {
1603
- if let ty:: PredicateKind :: Clause ( ty:: Clause :: Trait ( pred) ) =
1604
- obligation. predicate . kind ( ) . skip_binder ( )
1612
+ } else {
1613
+ err. span_label ( obligation. cause . span , "remove the `.await`" ) ;
1614
+ }
1615
+ // FIXME: account for associated `async fn`s.
1616
+ if let hir:: Expr { span, kind : hir:: ExprKind :: Call ( base, _) , .. } = expr {
1617
+ if let ty:: PredicateKind :: Clause ( ty:: Clause :: Trait ( pred) ) =
1618
+ obligation. predicate . kind ( ) . skip_binder ( )
1619
+ {
1620
+ err. span_label ( * span, & format ! ( "this call returns `{}`" , pred. self_ty( ) ) ) ;
1621
+ }
1622
+ if let Some ( typeck_results) = & self . typeck_results
1623
+ && let ty = typeck_results. expr_ty_adjusted ( base)
1624
+ && let ty:: FnDef ( def_id, _substs) = ty. kind ( )
1625
+ && let Some ( hir:: Node :: Item ( hir:: Item { ident, span, vis_span, .. } ) ) =
1626
+ hir. get_if_local ( * def_id)
1605
1627
{
1606
- err. span_label ( * span, & format ! ( "this call returns `{}`" , pred. self_ty( ) ) ) ;
1607
- }
1608
- if let Some ( typeck_results) = & self . typeck_results
1609
- && let ty = typeck_results. expr_ty_adjusted ( base)
1610
- && let ty:: FnDef ( def_id, _substs) = ty. kind ( )
1611
- && let Some ( hir:: Node :: Item ( hir:: Item { ident, span, vis_span, .. } ) ) =
1612
- hir. get_if_local ( * def_id)
1613
- {
1614
- let msg = format ! (
1615
- "alternatively, consider making `fn {}` asynchronous" ,
1616
- ident
1628
+ let msg = format ! (
1629
+ "alternatively, consider making `fn {}` asynchronous" ,
1630
+ ident
1631
+ ) ;
1632
+ if vis_span. is_empty ( ) {
1633
+ err. span_suggestion_verbose (
1634
+ span. shrink_to_lo ( ) ,
1635
+ & msg,
1636
+ "async " ,
1637
+ Applicability :: MaybeIncorrect ,
1638
+ ) ;
1639
+ } else {
1640
+ err. span_suggestion_verbose (
1641
+ vis_span. shrink_to_hi ( ) ,
1642
+ & msg,
1643
+ " async" ,
1644
+ Applicability :: MaybeIncorrect ,
1617
1645
) ;
1618
- if vis_span. is_empty ( ) {
1619
- err. span_suggestion_verbose (
1620
- span. shrink_to_lo ( ) ,
1621
- & msg,
1622
- "async " ,
1623
- Applicability :: MaybeIncorrect ,
1624
- ) ;
1625
- } else {
1626
- err. span_suggestion_verbose (
1627
- vis_span. shrink_to_hi ( ) ,
1628
- & msg,
1629
- " async" ,
1630
- Applicability :: MaybeIncorrect ,
1631
- ) ;
1632
- }
1633
1646
}
1634
- }
1647
+ }
1635
1648
}
1636
1649
}
1637
1650
}
0 commit comments