@@ -41,7 +41,7 @@ use rustc_session::config::{DumpSolverProofTree, TraitSolver};
4141use rustc_session:: Limit ;
4242use rustc_span:: def_id:: LOCAL_CRATE ;
4343use rustc_span:: symbol:: sym;
44- use rustc_span:: { BytePos , ExpnKind , Span , DUMMY_SP } ;
44+ use rustc_span:: { BytePos , ExpnKind , Span , Symbol , DUMMY_SP } ;
4545use std:: borrow:: Cow ;
4646use std:: fmt;
4747use std:: iter;
@@ -1045,6 +1045,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10451045 return ;
10461046 }
10471047 let self_ty = trait_ref. self_ty ( ) ;
1048+ let found_ty = trait_ref. args . get ( 1 ) . and_then ( |a| a. as_type ( ) ) ;
10481049
10491050 let mut prev_ty = self . resolve_vars_if_possible (
10501051 typeck. expr_ty_adjusted_opt ( expr) . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
@@ -1070,17 +1071,76 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10701071
10711072 // The following logic is simlar to `point_at_chain`, but that's focused on associated types
10721073 let mut expr = expr;
1073- while let hir:: ExprKind :: MethodCall ( _path_segment , rcvr_expr, _args , span) = expr. kind {
1074+ while let hir:: ExprKind :: MethodCall ( path_segment , rcvr_expr, args , span) = expr. kind {
10741075 // Point at every method call in the chain with the `Result` type.
10751076 // let foo = bar.iter().map(mapper)?;
10761077 // ------ -----------
10771078 expr = rcvr_expr;
10781079 chain. push ( ( span, prev_ty) ) ;
10791080
1080- prev_ty = self . resolve_vars_if_possible (
1081+ let next_ty = self . resolve_vars_if_possible (
10811082 typeck. expr_ty_adjusted_opt ( expr) . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
10821083 ) ;
10831084
1085+ let is_diagnostic_item = |symbol : Symbol , ty : Ty < ' tcx > | {
1086+ let ty:: Adt ( def, _) = ty. kind ( ) else {
1087+ return false ;
1088+ } ;
1089+ self . tcx . is_diagnostic_item ( symbol, def. did ( ) )
1090+ } ;
1091+ // For each method in the chain, see if this is `Result::map_err` or
1092+ // `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
1093+ // trailing `;`.
1094+ // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1095+ // accurate check, but we are in the wrong stage to do that and looking for
1096+ // `Result::map_err` by checking the Self type and the path segment is enough.
1097+ // sym::ok_or_else
1098+ if let Some ( ty) = get_e_type ( prev_ty)
1099+ && let Some ( found_ty) = found_ty
1100+ && (
1101+ (
1102+ path_segment. ident . name == sym:: map_err
1103+ && is_diagnostic_item ( sym:: Result , next_ty)
1104+ ) || (
1105+ path_segment. ident . name == sym:: ok_or_else
1106+ && is_diagnostic_item ( sym:: Option , next_ty)
1107+ )
1108+ )
1109+ && [ sym:: map_err, sym:: ok_or_else] . contains ( & path_segment. ident . name )
1110+ && let ty:: Tuple ( tys) = found_ty. kind ( )
1111+ && tys. is_empty ( )
1112+ && self . can_eq ( obligation. param_env , ty, found_ty)
1113+ && args. len ( ) == 1
1114+ && let Some ( arg) = args. get ( 0 )
1115+ && let hir:: ExprKind :: Closure ( closure) = arg. kind
1116+ && let body = self . tcx . hir ( ) . body ( closure. body )
1117+ && let hir:: ExprKind :: Block ( block, _) = body. value . kind
1118+ && let None = block. expr
1119+ && let [ .., stmt] = block. stmts
1120+ && let hir:: StmtKind :: Semi ( expr) = stmt. kind
1121+ && let expr_ty = self . resolve_vars_if_possible (
1122+ typeck. expr_ty_adjusted_opt ( expr)
1123+ . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
1124+ )
1125+ && self
1126+ . infcx
1127+ . type_implements_trait (
1128+ self . tcx . get_diagnostic_item ( sym:: From ) . unwrap ( ) ,
1129+ [ self_ty, expr_ty] ,
1130+ obligation. param_env ,
1131+ )
1132+ . must_apply_modulo_regions ( )
1133+ {
1134+ err. span_suggestion_short (
1135+ stmt. span . with_lo ( expr. span . hi ( ) ) ,
1136+ "remove this semicolon" ,
1137+ String :: new ( ) ,
1138+ Applicability :: MachineApplicable ,
1139+ ) ;
1140+ }
1141+
1142+ prev_ty = next_ty;
1143+
10841144 if let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( None , path) ) = expr. kind
10851145 && let hir:: Path { res : hir:: def:: Res :: Local ( hir_id) , .. } = path
10861146 && let Some ( hir:: Node :: Pat ( binding) ) = self . tcx . hir ( ) . find ( * hir_id)
0 commit comments