@@ -366,6 +366,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
366366 false
367367 }
368368
369+ /// If the given `HirId` corresponds to a block with a trailing expression, return that expression
370+ crate fn maybe_get_block_expr ( & self , hir_id : hir:: HirId ) -> Option < & ' tcx hir:: Expr < ' tcx > > {
371+ match self . tcx . hir ( ) . find ( hir_id) ? {
372+ Node :: Expr ( hir:: Expr { kind : hir:: ExprKind :: Block ( block, ..) , .. } ) => block. expr ,
373+ _ => None ,
374+ }
375+ }
376+
377+ /// Returns whether the given expression is an `else if`.
378+ crate fn is_else_if_block ( & self , expr : & hir:: Expr < ' _ > ) -> bool {
379+ if let hir:: ExprKind :: If ( ..) = expr. kind {
380+ let parent_id = self . tcx . hir ( ) . get_parent_node ( expr. hir_id ) ;
381+ if let Some ( Node :: Expr ( hir:: Expr {
382+ kind : hir:: ExprKind :: If ( _, _, Some ( else_expr) ) ,
383+ ..
384+ } ) ) = self . tcx . hir ( ) . find ( parent_id)
385+ {
386+ return else_expr. hir_id == expr. hir_id ;
387+ }
388+ }
389+ false
390+ }
391+
369392 /// This function is used to determine potential "simple" improvements or users' errors and
370393 /// provide them useful help. For example:
371394 ///
@@ -652,6 +675,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
652675 } ;
653676 let suggestion = if is_struct_pat_shorthand_field {
654677 format ! ( "{}: *{}" , code, code)
678+ } else if self . is_else_if_block ( expr) {
679+ // Don't suggest nonsense like `else *if`
680+ return None ;
681+ } else if let Some ( expr) = self . maybe_get_block_expr ( expr. hir_id ) {
682+ format ! ( "*{}" , sm. span_to_snippet( expr. span) . unwrap_or( code) )
655683 } else {
656684 format ! ( "*{}" , code)
657685 } ;
0 commit comments