@@ -46,6 +46,7 @@ use rustc_span::hygiene::DesugaringKind;
4646use rustc_span:: lev_distance:: find_best_match_for_name;
4747use rustc_span:: source_map:: Span ;
4848use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
49+ use rustc_span:: { BytePos , Pos } ;
4950use rustc_trait_selection:: traits:: { self , ObligationCauseCode } ;
5051
5152impl < ' a , ' tcx > FnCtxt < ' a , ' tcx > {
@@ -2063,7 +2064,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20632064 Some ( span) ,
20642065 ) ;
20652066 } else {
2066- err. help ( "methods are immutable and cannot be assigned to" ) ;
2067+ let mut found = false ;
2068+
2069+ if let ty:: RawPtr ( ty_and_mut) = expr_t. kind ( ) {
2070+ if let ty:: Adt ( adt_def, _) = ty_and_mut. ty . kind ( ) {
2071+ if adt_def. variants . len ( ) == 1
2072+ && adt_def
2073+ . variants
2074+ . iter ( )
2075+ . next ( )
2076+ . unwrap ( )
2077+ . fields
2078+ . iter ( )
2079+ . any ( |f| f. ident == field)
2080+ {
2081+ if let Some ( dot_loc) = expr_snippet. rfind ( '.' ) {
2082+ found = true ;
2083+ err. span_suggestion (
2084+ expr. span . with_hi ( expr. span . lo ( ) + BytePos :: from_usize ( dot_loc) ) ,
2085+ "to access the field, dereference first" ,
2086+ format ! ( "(*{})" , & expr_snippet[ 0 ..dot_loc] ) ,
2087+ Applicability :: MaybeIncorrect ,
2088+ ) ;
2089+ }
2090+ }
2091+ }
2092+ }
2093+
2094+ if !found {
2095+ err. help ( "methods are immutable and cannot be assigned to" ) ;
2096+ }
20672097 }
20682098
20692099 err. emit ( ) ;
0 commit comments