@@ -415,20 +415,21 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
415415 "{:?}" ,
416416 move_place. ty( self . body, self . infcx. tcx) . ty,
417417 ) ;
418- let snippet = self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( span) . unwrap ( ) ;
419- let is_option = move_ty. starts_with ( "std::option::Option" ) ;
420- let is_result = move_ty. starts_with ( "std::result::Result" ) ;
421- if is_option || is_result {
422- err. span_suggestion (
423- span,
424- & format ! ( "consider borrowing the `{}`'s content" , if is_option {
425- "Option"
426- } else {
427- "Result"
428- } ) ,
429- format ! ( "{}.as_ref()" , snippet) ,
430- Applicability :: MaybeIncorrect ,
431- ) ;
418+ if let Ok ( snippet) = self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( span) {
419+ let is_option = move_ty. starts_with ( "std::option::Option" ) ;
420+ let is_result = move_ty. starts_with ( "std::result::Result" ) ;
421+ if is_option || is_result {
422+ err. span_suggestion (
423+ span,
424+ & format ! ( "consider borrowing the `{}`'s content" , if is_option {
425+ "Option"
426+ } else {
427+ "Result"
428+ } ) ,
429+ format ! ( "{}.as_ref()" , snippet) ,
430+ Applicability :: MaybeIncorrect ,
431+ ) ;
432+ }
432433 }
433434 err
434435 }
@@ -439,19 +440,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
439440 err : & mut DiagnosticBuilder < ' a > ,
440441 span : Span ,
441442 ) {
442- let snippet = self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( span) . unwrap ( ) ;
443443 match error {
444444 GroupedMoveError :: MovesFromPlace {
445445 mut binds_to,
446446 move_from,
447447 ..
448448 } => {
449- err. span_suggestion (
450- span,
451- "consider borrowing here" ,
452- format ! ( "&{}" , snippet) ,
453- Applicability :: Unspecified ,
454- ) ;
449+ if let Ok ( snippet) = self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( span) {
450+ err. span_suggestion (
451+ span,
452+ "consider borrowing here" ,
453+ format ! ( "&{}" , snippet) ,
454+ Applicability :: Unspecified ,
455+ ) ;
456+ }
455457
456458 if binds_to. is_empty ( ) {
457459 let place_ty = move_from. ty ( self . body , self . infcx . tcx ) . ty ;
@@ -517,27 +519,27 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
517519 ..
518520 } ) )
519521 ) = bind_to. is_user_variable {
520- let pat_snippet = self . infcx . tcx . sess . source_map ( )
521- . span_to_snippet ( pat_span)
522- . unwrap ( ) ;
523- if pat_snippet. starts_with ( '&' ) {
524- let pat_snippet = pat_snippet[ 1 ..] . trim_start ( ) ;
525- let suggestion;
526- let to_remove;
527- if pat_snippet. starts_with ( "mut" )
528- && pat_snippet[ "mut" . len ( ) ..] . starts_with ( Pattern_White_Space )
529- {
530- suggestion = pat_snippet[ "mut" . len ( ) ..] . trim_start ( ) ;
531- to_remove = "&mut" ;
532- } else {
533- suggestion = pat_snippet;
534- to_remove = "&" ;
522+ if let Ok ( pat_snippet) = self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( pat_span)
523+ {
524+ if pat_snippet. starts_with ( '&' ) {
525+ let pat_snippet = pat_snippet[ 1 ..] . trim_start ( ) ;
526+ let suggestion;
527+ let to_remove;
528+ if pat_snippet. starts_with ( "mut" )
529+ && pat_snippet[ "mut" . len ( ) ..] . starts_with ( Pattern_White_Space )
530+ {
531+ suggestion = pat_snippet[ "mut" . len ( ) ..] . trim_start ( ) ;
532+ to_remove = "&mut" ;
533+ } else {
534+ suggestion = pat_snippet;
535+ to_remove = "&" ;
536+ }
537+ suggestions. push ( (
538+ pat_span,
539+ to_remove,
540+ suggestion. to_owned ( ) ,
541+ ) ) ;
535542 }
536- suggestions. push ( (
537- pat_span,
538- to_remove,
539- suggestion. to_owned ( ) ,
540- ) ) ;
541543 }
542544 }
543545 }
0 commit comments