@@ -2686,8 +2686,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26862686
26872687 // try to add a suggestion in case the field is a nested field of a field of the Adt
26882688 let mod_id = self . tcx . parent_module ( id) . to_def_id ( ) ;
2689+ let ( ty, unwrap) = if let ty:: Adt ( def, args) = expr_t. kind ( )
2690+ && ( self . tcx . is_diagnostic_item ( sym:: Result , def. did ( ) )
2691+ || self . tcx . is_diagnostic_item ( sym:: Option , def. did ( ) )
2692+ )
2693+ && let Some ( arg) = args. get ( 0 )
2694+ && let Some ( ty) = arg. as_type ( )
2695+ {
2696+ ( ty, "unwrap()." )
2697+ } else {
2698+ ( expr_t, "" )
2699+ } ;
26892700 for ( found_fields, args) in
2690- self . get_field_candidates_considering_privacy ( span, expr_t , mod_id, id)
2701+ self . get_field_candidates_considering_privacy ( span, ty , mod_id, id)
26912702 {
26922703 let field_names = found_fields. iter ( ) . map ( |field| field. name ) . collect :: < Vec < _ > > ( ) ;
26932704 let candidate_fields: Vec < _ > = found_fields
@@ -2707,9 +2718,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27072718 field_path. pop ( ) ;
27082719 field_path
27092720 . iter ( )
2710- . map ( |id| id. name . to_ident_string ( ) )
2711- . collect :: < Vec < String > > ( )
2712- . join ( "." )
2721+ . map ( |id| format ! ( "{}." , id. name. to_ident_string( ) ) )
2722+ . collect :: < String > ( )
27132723 } )
27142724 . collect :: < Vec < _ > > ( ) ;
27152725
@@ -2722,15 +2732,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27222732 if len > 1 { "some" } else { "one" } ,
27232733 if len > 1 { "have" } else { "has" } ,
27242734 ) ,
2725- candidate_fields. iter ( ) . map ( |path| format ! ( "{path}. " ) ) ,
2735+ candidate_fields. iter ( ) . map ( |path| format ! ( "{unwrap}{ path}" ) ) ,
27262736 Applicability :: MaybeIncorrect ,
27272737 ) ;
27282738 } else {
27292739 if let Some ( field_name) = find_best_match_for_name ( & field_names, field. name , None ) {
2730- err. span_suggestion (
2740+ err. span_suggestion_verbose (
27312741 field. span ,
27322742 "a field with a similar name exists" ,
2733- field_name,
2743+ format ! ( "{unwrap}{}" , field_name) ,
27342744 Applicability :: MaybeIncorrect ,
27352745 ) ;
27362746 } else if !field_names. is_empty ( ) {
0 commit comments