11use super :: FnCtxt ;
22
3- use crate :: coercion:: CollectRetsVisitor ;
43use crate :: errors;
54use crate :: fluent_generated as fluent;
65use crate :: fn_ctxt:: rustc_span:: BytePos ;
@@ -17,7 +16,6 @@ use rustc_errors::{Applicability, Diagnostic, MultiSpan};
1716use rustc_hir as hir;
1817use rustc_hir:: def:: Res ;
1918use rustc_hir:: def:: { CtorKind , CtorOf , DefKind } ;
20- use rustc_hir:: intravisit:: { Map , Visitor } ;
2119use rustc_hir:: lang_items:: LangItem ;
2220use rustc_hir:: {
2321 CoroutineDesugaring , CoroutineKind , CoroutineSource , Expr , ExprKind , GenericBound , HirId , Node ,
@@ -1041,22 +1039,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10411039 return ;
10421040 }
10431041
1044- let in_closure = matches ! (
1045- self . tcx
1046- . hir( )
1047- . parent_iter( id)
1048- . filter( |( _, node) | {
1049- matches!(
1050- node,
1051- Node :: Expr ( Expr { kind: ExprKind :: Closure ( ..) , .. } )
1052- | Node :: Item ( _)
1053- | Node :: TraitItem ( _)
1054- | Node :: ImplItem ( _)
1055- )
1056- } )
1057- . next( ) ,
1058- Some ( ( _ , Node :: Expr ( Expr { kind : ExprKind :: Closure ( .. ) , .. } ) ) )
1059- ) ;
1042+ let scope = self
1043+ . tcx
1044+ . hir ( )
1045+ . parent_iter ( id)
1046+ . filter ( |( _, node) | {
1047+ matches ! (
1048+ node,
1049+ Node :: Expr ( Expr { kind: ExprKind :: Closure ( ..) , .. } )
1050+ | Node :: Item ( _)
1051+ | Node :: TraitItem ( _)
1052+ | Node :: ImplItem ( _)
1053+ )
1054+ } )
1055+ . next ( ) ;
1056+ let in_closure =
1057+ matches ! ( scope , Some ( ( _ , Node :: Expr ( Expr { kind : ExprKind :: Closure ( .. ) , .. } ) ) ) ) ;
10601058
10611059 let can_return = match fn_decl. output {
10621060 hir:: FnRetTy :: Return ( ty) => {
@@ -1078,35 +1076,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10781076 self . can_coerce ( found, ty)
10791077 }
10801078 hir:: FnRetTy :: DefaultReturn ( _) if in_closure => {
1081- let mut rets = vec ! [ ] ;
1082- if let Some ( ret_coercion) = self . ret_coercion . as_ref ( ) {
1083- let ret_ty = ret_coercion. borrow ( ) . expected_ty ( ) ;
1084- rets. push ( ret_ty) ;
1085- }
1086- let mut visitor = CollectRetsVisitor { ret_exprs : vec ! [ ] } ;
1087- if let Some ( item) = self . tcx . hir ( ) . find ( id)
1088- && let Node :: Expr ( expr) = item
1089- {
1090- visitor. visit_expr ( expr) ;
1091- for expr in visitor. ret_exprs {
1092- if let Some ( ty) = self . typeck_results . borrow ( ) . node_type_opt ( expr. hir_id ) {
1093- rets. push ( ty) ;
1094- }
1095- }
1096- if let hir:: ExprKind :: Block ( hir:: Block { expr : Some ( expr) , .. } , _) = expr. kind
1097- {
1098- if let Some ( ty) = self . typeck_results . borrow ( ) . node_type_opt ( expr. hir_id ) {
1099- rets. push ( ty) ;
1100- }
1101- }
1102- }
1103- rets. into_iter ( ) . all ( |ty| self . can_coerce ( found, ty) )
1079+ self . ret_coercion . as_ref ( ) . map_or ( false , |ret| {
1080+ let ret_ty = ret. borrow ( ) . expected_ty ( ) ;
1081+ self . can_coerce ( found, ret_ty)
1082+ } )
11041083 }
11051084 _ => false ,
11061085 } ;
11071086 if can_return
11081087 && let Some ( owner_node) = self . tcx . hir_node ( fn_id) . as_owner ( )
1109- && let Some ( span) = expr. span . find_ancestor_inside ( owner_node. span ( ) )
1088+ && let Some ( span) = expr. span . find_ancestor_inside ( * owner_node. span ( ) )
11101089 {
11111090 err. multipart_suggestion (
11121091 "you might have meant to return this value" ,
0 commit comments