@@ -2346,6 +2346,17 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {
23462346 provided_span,
23472347 format ! ( "unexpected argument{idx}{provided_ty_name}" ) ,
23482348 ) ) ;
2349+ if self . provided_arg_tys . len ( ) == 1
2350+ && let Some ( span) = self . maybe_suggest_expect_for_unwrap ( provided_ty)
2351+ {
2352+ err. span_suggestion_verbose (
2353+ span,
2354+ "did you mean to use `expect`?" ,
2355+ "expect" ,
2356+ Applicability :: MaybeIncorrect ,
2357+ ) ;
2358+ continue ;
2359+ }
23492360 let mut span = provided_span;
23502361 if span. can_be_used_for_suggestions ( )
23512362 && self . call_metadata . error_span . can_be_used_for_suggestions ( )
@@ -2776,6 +2787,22 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {
27762787
27772788 ( suggestion_span, suggestion)
27782789 }
2790+
2791+ fn maybe_suggest_expect_for_unwrap ( & self , provided_ty : Ty < ' tcx > ) -> Option < Span > {
2792+ let tcx = self . tcx ( ) ;
2793+ if let Some ( call_ident) = self . call_metadata . call_ident
2794+ && call_ident. name == sym:: unwrap
2795+ && let Some ( callee_ty) = self . callee_ty
2796+ && let ty:: Adt ( adt, _) = callee_ty. peel_refs ( ) . kind ( )
2797+ && ( tcx. is_diagnostic_item ( sym:: Option , adt. did ( ) )
2798+ || tcx. is_diagnostic_item ( sym:: Result , adt. did ( ) ) )
2799+ && self . may_coerce ( provided_ty, Ty :: new_static_str ( tcx) )
2800+ {
2801+ Some ( call_ident. span )
2802+ } else {
2803+ None
2804+ }
2805+ }
27792806}
27802807
27812808struct ArgMatchingCtxt < ' a , ' b , ' tcx > {
0 commit comments