@@ -1092,31 +1092,35 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
1092
1092
// For each method in the chain, see if this is `Result::map_err` or
1093
1093
// `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
1094
1094
// trailing `;`.
1095
- // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1096
- // accurate check, but we are in the wrong stage to do that and looking for
1097
- // `Result::map_err` by checking the Self type and the path segment is enough.
1098
- // sym::ok_or_else
1099
1095
if let Some ( ty) = get_e_type ( prev_ty)
1100
1096
&& let Some ( found_ty) = found_ty
1097
+ // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1098
+ // accurate check, but we are in the wrong stage to do that and looking for
1099
+ // `Result::map_err` by checking the Self type and the path segment is enough.
1100
+ // sym::ok_or_else
1101
1101
&& (
1102
- (
1102
+ ( // Result::map_err
1103
1103
path_segment. ident . name == sym:: map_err
1104
1104
&& is_diagnostic_item ( sym:: Result , next_ty)
1105
- ) || (
1105
+ ) || ( // Option::ok_or_else
1106
1106
path_segment. ident . name == sym:: ok_or_else
1107
1107
&& is_diagnostic_item ( sym:: Option , next_ty)
1108
1108
)
1109
1109
)
1110
- && [ sym :: map_err , sym :: ok_or_else ] . contains ( & path_segment . ident . name )
1110
+ // Found `Result<_, ()>?`
1111
1111
&& let ty:: Tuple ( tys) = found_ty. kind ( )
1112
1112
&& tys. is_empty ( )
1113
+ // The current method call returns `Result<_, ()>`
1113
1114
&& self . can_eq ( obligation. param_env , ty, found_ty)
1115
+ // There's a single argument in the method call and it is a closure
1114
1116
&& args. len ( ) == 1
1115
1117
&& let Some ( arg) = args. get ( 0 )
1116
1118
&& let hir:: ExprKind :: Closure ( closure) = arg. kind
1119
+ // The closure has a block for its body with no tail expression
1117
1120
&& let body = self . tcx . hir ( ) . body ( closure. body )
1118
1121
&& let hir:: ExprKind :: Block ( block, _) = body. value . kind
1119
1122
&& let None = block. expr
1123
+ // The last statement is of a type that can be converted to the return error type
1120
1124
&& let [ .., stmt] = block. stmts
1121
1125
&& let hir:: StmtKind :: Semi ( expr) = stmt. kind
1122
1126
&& let expr_ty = self . resolve_vars_if_possible (
0 commit comments