@@ -1034,31 +1034,35 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
1034
1034
// For each method in the chain, see if this is `Result::map_err` or
1035
1035
// `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
1036
1036
// trailing `;`.
1037
- // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1038
- // accurate check, but we are in the wrong stage to do that and looking for
1039
- // `Result::map_err` by checking the Self type and the path segment is enough.
1040
- // sym::ok_or_else
1041
1037
if let Some ( ty) = get_e_type ( prev_ty)
1042
1038
&& let Some ( found_ty) = found_ty
1039
+ // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1040
+ // accurate check, but we are in the wrong stage to do that and looking for
1041
+ // `Result::map_err` by checking the Self type and the path segment is enough.
1042
+ // sym::ok_or_else
1043
1043
&& (
1044
- (
1044
+ ( // Result::map_err
1045
1045
path_segment. ident . name == sym:: map_err
1046
1046
&& is_diagnostic_item ( sym:: Result , next_ty)
1047
- ) || (
1047
+ ) || ( // Option::ok_or_else
1048
1048
path_segment. ident . name == sym:: ok_or_else
1049
1049
&& is_diagnostic_item ( sym:: Option , next_ty)
1050
1050
)
1051
1051
)
1052
- && [ sym :: map_err , sym :: ok_or_else ] . contains ( & path_segment . ident . name )
1052
+ // Found `Result<_, ()>?`
1053
1053
&& let ty:: Tuple ( tys) = found_ty. kind ( )
1054
1054
&& tys. is_empty ( )
1055
+ // The current method call returns `Result<_, ()>`
1055
1056
&& self . can_eq ( obligation. param_env , ty, found_ty)
1057
+ // There's a single argument in the method call and it is a closure
1056
1058
&& args. len ( ) == 1
1057
1059
&& let Some ( arg) = args. get ( 0 )
1058
1060
&& let hir:: ExprKind :: Closure ( closure) = arg. kind
1061
+ // The closure has a block for its body with no tail expression
1059
1062
&& let body = self . tcx . hir ( ) . body ( closure. body )
1060
1063
&& let hir:: ExprKind :: Block ( block, _) = body. value . kind
1061
1064
&& let None = block. expr
1065
+ // The last statement is of a type that can be converted to the return error type
1062
1066
&& let [ .., stmt] = block. stmts
1063
1067
&& let hir:: StmtKind :: Semi ( expr) = stmt. kind
1064
1068
&& let expr_ty = self . resolve_vars_if_possible (
0 commit comments