@@ -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 } ;
@@ -2391,37 +2390,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23912390 expr,
23922391 Some ( span) ,
23932392 ) ;
2393+ } else if let ty:: RawPtr ( ty_and_mut) = expr_t. kind ( )
2394+ && let ty:: Adt ( adt_def, _) = ty_and_mut. ty . kind ( )
2395+ && let ExprKind :: Field ( base_expr, _) = expr. kind
2396+ && adt_def. variants ( ) . len ( ) == 1
2397+ && adt_def
2398+ . variants ( )
2399+ . iter ( )
2400+ . next ( )
2401+ . unwrap ( )
2402+ . fields
2403+ . iter ( )
2404+ . any ( |f| f. ident ( self . tcx ) == field)
2405+ {
2406+ err. multipart_suggestion (
2407+ "to access the field, dereference first" ,
2408+ vec ! [
2409+ ( base_expr. span. shrink_to_lo( ) , "(*" . to_string( ) ) ,
2410+ ( base_expr. span. shrink_to_hi( ) , ")" . to_string( ) ) ,
2411+ ] ,
2412+ Applicability :: MaybeIncorrect ,
2413+ ) ;
23942414 } else {
2395- let mut found = false ;
2396-
2397- if let ty:: RawPtr ( ty_and_mut) = expr_t. kind ( )
2398- && let ty:: Adt ( adt_def, _) = ty_and_mut. ty . kind ( )
2399- {
2400- if adt_def. variants ( ) . len ( ) == 1
2401- && adt_def
2402- . variants ( )
2403- . iter ( )
2404- . next ( )
2405- . unwrap ( )
2406- . fields
2407- . iter ( )
2408- . any ( |f| f. ident ( self . tcx ) == field)
2409- {
2410- if let Some ( dot_loc) = expr_snippet. rfind ( '.' ) {
2411- found = true ;
2412- err. span_suggestion (
2413- expr. span . with_hi ( expr. span . lo ( ) + BytePos :: from_usize ( dot_loc) ) ,
2414- "to access the field, dereference first" ,
2415- format ! ( "(*{})" , & expr_snippet[ 0 ..dot_loc] ) ,
2416- Applicability :: MaybeIncorrect ,
2417- ) ;
2418- }
2419- }
2420- }
2421-
2422- if !found {
2423- err. help ( "methods are immutable and cannot be assigned to" ) ;
2424- }
2415+ err. help ( "methods are immutable and cannot be assigned to" ) ;
24252416 }
24262417
24272418 err. emit ( ) ;
0 commit comments