@@ -2686,8 +2686,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2686
2686
2687
2687
// try to add a suggestion in case the field is a nested field of a field of the Adt
2688
2688
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
+ } ;
2689
2700
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)
2691
2702
{
2692
2703
let field_names = found_fields. iter ( ) . map ( |field| field. name ) . collect :: < Vec < _ > > ( ) ;
2693
2704
let candidate_fields: Vec < _ > = found_fields
@@ -2707,9 +2718,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2707
2718
field_path. pop ( ) ;
2708
2719
field_path
2709
2720
. 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 > ( )
2713
2723
} )
2714
2724
. collect :: < Vec < _ > > ( ) ;
2715
2725
@@ -2722,15 +2732,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2722
2732
if len > 1 { "some" } else { "one" } ,
2723
2733
if len > 1 { "have" } else { "has" } ,
2724
2734
) ,
2725
- candidate_fields. iter ( ) . map ( |path| format ! ( "{path}. " ) ) ,
2735
+ candidate_fields. iter ( ) . map ( |path| format ! ( "{unwrap}{ path}" ) ) ,
2726
2736
Applicability :: MaybeIncorrect ,
2727
2737
) ;
2728
2738
} else {
2729
2739
if let Some ( field_name) = find_best_match_for_name ( & field_names, field. name , None ) {
2730
- err. span_suggestion (
2740
+ err. span_suggestion_verbose (
2731
2741
field. span ,
2732
2742
"a field with a similar name exists" ,
2733
- field_name,
2743
+ format ! ( "{unwrap}{}" , field_name) ,
2734
2744
Applicability :: MaybeIncorrect ,
2735
2745
) ;
2736
2746
} else if !field_names. is_empty ( ) {
0 commit comments