@@ -962,38 +962,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
962962 expected : Ty < ' tcx > ,
963963 found : Ty < ' tcx > ,
964964 ) -> bool {
965- let ty:: Adt ( e, args_e) = expected. kind ( ) else {
966- return false ;
967- } ;
968- let ty:: Adt ( f, args_f) = found. kind ( ) else {
969- return false ;
970- } ;
971- if e. did ( ) != f. did ( ) {
972- return false ;
973- }
974- if Some ( e. did ( ) ) != self . tcx . get_diagnostic_item ( sym:: Result ) {
975- return false ;
976- }
977965 let map = self . tcx . hir ( ) ;
978- if let Some ( hir:: Node :: Expr ( expr) ) = map. find_parent ( expr. hir_id )
979- && let hir:: ExprKind :: Ret ( _) = expr. kind
980- {
981- // `return foo;`
982- } else if map. get_return_block ( expr. hir_id ) . is_some ( ) {
983- // Function's tail expression.
984- } else {
985- return false ;
986- }
987- let e = args_e. type_at ( 1 ) ;
988- let f = args_f. type_at ( 1 ) ;
989- if self
990- . infcx
991- . type_implements_trait (
992- self . tcx . get_diagnostic_item ( sym:: Into ) . unwrap ( ) ,
993- [ f, e] ,
994- self . param_env ,
995- )
996- . must_apply_modulo_regions ( )
966+ let returned = matches ! (
967+ map. find_parent( expr. hir_id) ,
968+ Some ( hir:: Node :: Expr ( hir:: Expr { kind: hir:: ExprKind :: Ret ( _) , .. } ) )
969+ ) || map. get_return_block ( expr. hir_id ) . is_some ( ) ;
970+ if returned
971+ && let ty:: Adt ( e, args_e) = expected. kind ( )
972+ && let ty:: Adt ( f, args_f) = found. kind ( )
973+ && e. did ( ) == f. did ( )
974+ && Some ( e. did ( ) ) == self . tcx . get_diagnostic_item ( sym:: Result )
975+ && let e_ok = args_e. type_at ( 0 )
976+ && let f_ok = args_f. type_at ( 0 )
977+ && self . infcx . can_eq ( self . param_env , f_ok, e_ok)
978+ && let e_err = args_e. type_at ( 1 )
979+ && let f_err = args_f. type_at ( 1 )
980+ && self
981+ . infcx
982+ . type_implements_trait (
983+ self . tcx . get_diagnostic_item ( sym:: Into ) . unwrap ( ) ,
984+ [ f_err, e_err] ,
985+ self . param_env ,
986+ )
987+ . must_apply_modulo_regions ( )
997988 {
998989 err. multipart_suggestion (
999990 "use `?` to coerce and return an appropriate `Err`, and wrap the resulting value \
0 commit comments