@@ -50,7 +50,6 @@ use rustc_span::hygiene::DesugaringKind;
5050use rustc_span:: lev_distance:: find_best_match_for_name;
5151use rustc_span:: source_map:: { Span , Spanned } ;
5252use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
53- use rustc_span:: { BytePos , Pos } ;
5453use rustc_target:: spec:: abi:: Abi :: RustIntrinsic ;
5554use rustc_trait_selection:: infer:: InferCtxtExt ;
5655use rustc_trait_selection:: traits:: { self , ObligationCauseCode } ;
@@ -2398,37 +2397,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23982397 expr,
23992398 Some ( span) ,
24002399 ) ;
2400+ } else if let ty:: RawPtr ( ty_and_mut) = expr_t. kind ( )
2401+ && let ty:: Adt ( adt_def, _) = ty_and_mut. ty . kind ( )
2402+ && let ExprKind :: Field ( base_expr, _) = expr. kind
2403+ && adt_def. variants ( ) . len ( ) == 1
2404+ && adt_def
2405+ . variants ( )
2406+ . iter ( )
2407+ . next ( )
2408+ . unwrap ( )
2409+ . fields
2410+ . iter ( )
2411+ . any ( |f| f. ident ( self . tcx ) == field)
2412+ {
2413+ err. multipart_suggestion (
2414+ "to access the field, dereference first" ,
2415+ vec ! [
2416+ ( base_expr. span. shrink_to_lo( ) , "(*" . to_string( ) ) ,
2417+ ( base_expr. span. shrink_to_hi( ) , ")" . to_string( ) ) ,
2418+ ] ,
2419+ Applicability :: MaybeIncorrect ,
2420+ ) ;
24012421 } else {
2402- let mut found = false ;
2403-
2404- if let ty:: RawPtr ( ty_and_mut) = expr_t. kind ( )
2405- && let ty:: Adt ( adt_def, _) = ty_and_mut. ty . kind ( )
2406- {
2407- if adt_def. variants ( ) . len ( ) == 1
2408- && adt_def
2409- . variants ( )
2410- . iter ( )
2411- . next ( )
2412- . unwrap ( )
2413- . fields
2414- . iter ( )
2415- . any ( |f| f. ident ( self . tcx ) == field)
2416- {
2417- if let Some ( dot_loc) = expr_snippet. rfind ( '.' ) {
2418- found = true ;
2419- err. span_suggestion (
2420- expr. span . with_hi ( expr. span . lo ( ) + BytePos :: from_usize ( dot_loc) ) ,
2421- "to access the field, dereference first" ,
2422- format ! ( "(*{})" , & expr_snippet[ 0 ..dot_loc] ) ,
2423- Applicability :: MaybeIncorrect ,
2424- ) ;
2425- }
2426- }
2427- }
2428-
2429- if !found {
2430- err. help ( "methods are immutable and cannot be assigned to" ) ;
2431- }
2422+ err. help ( "methods are immutable and cannot be assigned to" ) ;
24322423 }
24332424
24342425 err. emit ( ) ;
0 commit comments