@@ -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;
@@ -1010,6 +1010,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10101010 return ;
10111011 }
10121012 let self_ty = trait_ref. self_ty ( ) ;
1013+ let found_ty = trait_ref. args . get ( 1 ) . and_then ( |a| a. as_type ( ) ) ;
10131014
10141015 let mut prev_ty = self . resolve_vars_if_possible (
10151016 typeck. expr_ty_adjusted_opt ( expr) . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
@@ -1035,17 +1036,76 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10351036
10361037 // The following logic is simlar to `point_at_chain`, but that's focused on associated types
10371038 let mut expr = expr;
1038- while let hir:: ExprKind :: MethodCall ( _path_segment , rcvr_expr, _args , span) = expr. kind {
1039+ while let hir:: ExprKind :: MethodCall ( path_segment , rcvr_expr, args , span) = expr. kind {
10391040 // Point at every method call in the chain with the `Result` type.
10401041 // let foo = bar.iter().map(mapper)?;
10411042 // ------ -----------
10421043 expr = rcvr_expr;
10431044 chain. push ( ( span, prev_ty) ) ;
10441045
1045- prev_ty = self . resolve_vars_if_possible (
1046+ let next_ty = self . resolve_vars_if_possible (
10461047 typeck. expr_ty_adjusted_opt ( expr) . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
10471048 ) ;
10481049
1050+ let is_diagnostic_item = |symbol : Symbol , ty : Ty < ' tcx > | {
1051+ let ty:: Adt ( def, _) = ty. kind ( ) else {
1052+ return false ;
1053+ } ;
1054+ self . tcx . is_diagnostic_item ( symbol, def. did ( ) )
1055+ } ;
1056+ // For each method in the chain, see if this is `Result::map_err` or
1057+ // `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
1058+ // trailing `;`.
1059+ // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1060+ // accurate check, but we are in the wrong stage to do that and looking for
1061+ // `Result::map_err` by checking the Self type and the path segment is enough.
1062+ // sym::ok_or_else
1063+ if let Some ( ty) = get_e_type ( prev_ty)
1064+ && let Some ( found_ty) = found_ty
1065+ && (
1066+ (
1067+ path_segment. ident . name == sym:: map_err
1068+ && is_diagnostic_item ( sym:: Result , next_ty)
1069+ ) || (
1070+ path_segment. ident . name == sym:: ok_or_else
1071+ && is_diagnostic_item ( sym:: Option , next_ty)
1072+ )
1073+ )
1074+ && [ sym:: map_err, sym:: ok_or_else] . contains ( & path_segment. ident . name )
1075+ && let ty:: Tuple ( tys) = found_ty. kind ( )
1076+ && tys. is_empty ( )
1077+ && self . can_eq ( obligation. param_env , ty, found_ty)
1078+ && args. len ( ) == 1
1079+ && let Some ( arg) = args. get ( 0 )
1080+ && let hir:: ExprKind :: Closure ( closure) = arg. kind
1081+ && let body = self . tcx . hir ( ) . body ( closure. body )
1082+ && let hir:: ExprKind :: Block ( block, _) = body. value . kind
1083+ && let None = block. expr
1084+ && let [ .., stmt] = block. stmts
1085+ && let hir:: StmtKind :: Semi ( expr) = stmt. kind
1086+ && let expr_ty = self . resolve_vars_if_possible (
1087+ typeck. expr_ty_adjusted_opt ( expr)
1088+ . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
1089+ )
1090+ && self
1091+ . infcx
1092+ . type_implements_trait (
1093+ self . tcx . get_diagnostic_item ( sym:: From ) . unwrap ( ) ,
1094+ [ self_ty, expr_ty] ,
1095+ obligation. param_env ,
1096+ )
1097+ . must_apply_modulo_regions ( )
1098+ {
1099+ err. span_suggestion_short (
1100+ stmt. span . with_lo ( expr. span . hi ( ) ) ,
1101+ "remove this semicolon" ,
1102+ String :: new ( ) ,
1103+ Applicability :: MachineApplicable ,
1104+ ) ;
1105+ }
1106+
1107+ prev_ty = next_ty;
1108+
10491109 if let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( None , path) ) = expr. kind
10501110 && let hir:: Path { res : hir:: def:: Res :: Local ( hir_id) , .. } = path
10511111 && let Some ( hir:: Node :: Pat ( binding) ) = self . tcx . hir ( ) . find ( * hir_id)
0 commit comments