@@ -1057,31 +1057,35 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
1057
1057
// For each method in the chain, see if this is `Result::map_err` or
1058
1058
// `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
1059
1059
// trailing `;`.
1060
- // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1061
- // accurate check, but we are in the wrong stage to do that and looking for
1062
- // `Result::map_err` by checking the Self type and the path segment is enough.
1063
- // sym::ok_or_else
1064
1060
if let Some ( ty) = get_e_type ( prev_ty)
1065
1061
&& let Some ( found_ty) = found_ty
1062
+ // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1063
+ // accurate check, but we are in the wrong stage to do that and looking for
1064
+ // `Result::map_err` by checking the Self type and the path segment is enough.
1065
+ // sym::ok_or_else
1066
1066
&& (
1067
- (
1067
+ ( // Result::map_err
1068
1068
path_segment. ident . name == sym:: map_err
1069
1069
&& is_diagnostic_item ( sym:: Result , next_ty)
1070
- ) || (
1070
+ ) || ( // Option::ok_or_else
1071
1071
path_segment. ident . name == sym:: ok_or_else
1072
1072
&& is_diagnostic_item ( sym:: Option , next_ty)
1073
1073
)
1074
1074
)
1075
- && [ sym :: map_err , sym :: ok_or_else ] . contains ( & path_segment . ident . name )
1075
+ // Found `Result<_, ()>?`
1076
1076
&& let ty:: Tuple ( tys) = found_ty. kind ( )
1077
1077
&& tys. is_empty ( )
1078
+ // The current method call returns `Result<_, ()>`
1078
1079
&& self . can_eq ( obligation. param_env , ty, found_ty)
1080
+ // There's a single argument in the method call and it is a closure
1079
1081
&& args. len ( ) == 1
1080
1082
&& let Some ( arg) = args. get ( 0 )
1081
1083
&& let hir:: ExprKind :: Closure ( closure) = arg. kind
1084
+ // The closure has a block for its body with no tail expression
1082
1085
&& let body = self . tcx . hir ( ) . body ( closure. body )
1083
1086
&& let hir:: ExprKind :: Block ( block, _) = body. value . kind
1084
1087
&& let None = block. expr
1088
+ // The last statement is of a type that can be converted to the return error type
1085
1089
&& let [ .., stmt] = block. stmts
1086
1090
&& let hir:: StmtKind :: Semi ( expr) = stmt. kind
1087
1091
&& let expr_ty = self . resolve_vars_if_possible (
0 commit comments