@@ -1055,31 +1055,35 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10551055 // For each method in the chain, see if this is `Result::map_err` or
10561056 // `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
10571057 // trailing `;`.
1058- // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1059- // accurate check, but we are in the wrong stage to do that and looking for
1060- // `Result::map_err` by checking the Self type and the path segment is enough.
1061- // sym::ok_or_else
10621058 if let Some ( ty) = get_e_type ( prev_ty)
10631059 && let Some ( found_ty) = found_ty
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
10641064 && (
1065- (
1065+ ( // Result::map_err
10661066 path_segment. ident . name == sym:: map_err
10671067 && is_diagnostic_item ( sym:: Result , next_ty)
1068- ) || (
1068+ ) || ( // Option::ok_or_else
10691069 path_segment. ident . name == sym:: ok_or_else
10701070 && is_diagnostic_item ( sym:: Option , next_ty)
10711071 )
10721072 )
1073- && [ sym :: map_err , sym :: ok_or_else ] . contains ( & path_segment . ident . name )
1073+ // Found `Result<_, ()>?`
10741074 && let ty:: Tuple ( tys) = found_ty. kind ( )
10751075 && tys. is_empty ( )
1076+ // The current method call returns `Result<_, ()>`
10761077 && self . can_eq ( obligation. param_env , ty, found_ty)
1078+ // There's a single argument in the method call and it is a closure
10771079 && args. len ( ) == 1
10781080 && let Some ( arg) = args. get ( 0 )
10791081 && let hir:: ExprKind :: Closure ( closure) = arg. kind
1082+ // The closure has a block for its body with no tail expression
10801083 && let body = self . tcx . hir ( ) . body ( closure. body )
10811084 && let hir:: ExprKind :: Block ( block, _) = body. value . kind
10821085 && let None = block. expr
1086+ // The last statement is of a type that can be converted to the return error type
10831087 && let [ .., stmt] = block. stmts
10841088 && let hir:: StmtKind :: Semi ( expr) = stmt. kind
10851089 && let expr_ty = self . resolve_vars_if_possible (
0 commit comments