@@ -2632,8 +2632,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2632
2632
2633
2633
// try to add a suggestion in case the field is a nested field of a field of the Adt
2634
2634
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
+ } ;
2635
2646
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)
2637
2648
{
2638
2649
let field_names = found_fields. iter ( ) . map ( |field| field. name ) . collect :: < Vec < _ > > ( ) ;
2639
2650
let candidate_fields: Vec < _ > = found_fields
@@ -2653,9 +2664,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2653
2664
field_path. pop ( ) ;
2654
2665
field_path
2655
2666
. 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 > ( )
2659
2669
} )
2660
2670
. collect :: < Vec < _ > > ( ) ;
2661
2671
@@ -2668,15 +2678,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2668
2678
if len > 1 { "some" } else { "one" } ,
2669
2679
if len > 1 { "have" } else { "has" } ,
2670
2680
) ,
2671
- candidate_fields. iter ( ) . map ( |path| format ! ( "{path}. " ) ) ,
2681
+ candidate_fields. iter ( ) . map ( |path| format ! ( "{unwrap}{ path}" ) ) ,
2672
2682
Applicability :: MaybeIncorrect ,
2673
2683
) ;
2674
2684
} else {
2675
2685
if let Some ( field_name) = find_best_match_for_name ( & field_names, field. name , None ) {
2676
- err. span_suggestion (
2686
+ err. span_suggestion_verbose (
2677
2687
field. span ,
2678
2688
"a field with a similar name exists" ,
2679
- field_name,
2689
+ format ! ( "{unwrap}{}" , field_name) ,
2680
2690
Applicability :: MaybeIncorrect ,
2681
2691
) ;
2682
2692
} else if !field_names. is_empty ( ) {
0 commit comments