@@ -2632,8 +2632,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26322632
26332633 // try to add a suggestion in case the field is a nested field of a field of the Adt
26342634 let mod_id = self . tcx . parent_module ( id) . to_def_id ( ) ;
2635+ let ( ty, unwrap) = if let ty:: Adt ( def, args) = expr_t. kind ( )
2636+ && ( self . tcx . is_diagnostic_item ( sym:: Result , def. did ( ) )
2637+ || self . tcx . is_diagnostic_item ( sym:: Option , def. did ( ) )
2638+ )
2639+ && let Some ( arg) = args. get ( 0 )
2640+ && let Some ( ty) = arg. as_type ( )
2641+ {
2642+ ( ty, "unwrap()." )
2643+ } else {
2644+ ( expr_t, "" )
2645+ } ;
26352646 for ( found_fields, args) in
2636- self . get_field_candidates_considering_privacy ( span, expr_t , mod_id, id)
2647+ self . get_field_candidates_considering_privacy ( span, ty , mod_id, id)
26372648 {
26382649 let field_names = found_fields. iter ( ) . map ( |field| field. name ) . collect :: < Vec < _ > > ( ) ;
26392650 let candidate_fields: Vec < _ > = found_fields
@@ -2653,9 +2664,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26532664 field_path. pop ( ) ;
26542665 field_path
26552666 . iter ( )
2656- . map ( |id| id. name . to_ident_string ( ) )
2657- . collect :: < Vec < String > > ( )
2658- . join ( "." )
2667+ . map ( |id| format ! ( "{}." , id. name. to_ident_string( ) ) )
2668+ . collect :: < String > ( )
26592669 } )
26602670 . collect :: < Vec < _ > > ( ) ;
26612671
@@ -2668,15 +2678,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26682678 if len > 1 { "some" } else { "one" } ,
26692679 if len > 1 { "have" } else { "has" } ,
26702680 ) ,
2671- candidate_fields. iter ( ) . map ( |path| format ! ( "{path}. " ) ) ,
2681+ candidate_fields. iter ( ) . map ( |path| format ! ( "{unwrap}{ path}" ) ) ,
26722682 Applicability :: MaybeIncorrect ,
26732683 ) ;
26742684 } else {
26752685 if let Some ( field_name) = find_best_match_for_name ( & field_names, field. name , None ) {
2676- err. span_suggestion (
2686+ err. span_suggestion_verbose (
26772687 field. span ,
26782688 "a field with a similar name exists" ,
2679- field_name,
2689+ format ! ( "{unwrap}{}" , field_name) ,
26802690 Applicability :: MaybeIncorrect ,
26812691 ) ;
26822692 } else if !field_names. is_empty ( ) {
0 commit comments