@@ -888,28 +888,22 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
888
888
err : & mut DiagnosticBuilder < ' tcx > ,
889
889
trait_ref : & ty:: Binder < ty:: TraitRef < ' tcx > > ) {
890
890
let ty:: Binder ( trait_ref) = trait_ref;
891
-
892
891
let span = obligation. cause . span ;
893
- let mut snippet = match self . tcx . sess . codemap ( ) . span_to_snippet ( span) {
894
- Ok ( s) => s,
895
- Err ( _) => String :: from ( "" ) ,
896
- } ;
897
892
898
- let mut refs_number = 0 ;
893
+ if let Ok ( snippet) = self . tcx . sess . codemap ( ) . span_to_snippet ( span) {
894
+ let refs_number = snippet. chars ( )
895
+ . filter ( |c| !c. is_whitespace ( ) )
896
+ . take_while ( |c| * c == '&' )
897
+ . count ( ) ;
899
898
900
- for c in snippet. chars ( ) {
901
- if c == '&' {
902
- refs_number += 1 ;
903
- }
904
- }
899
+ let mut refs_remaining = refs_number;
900
+ let mut trait_type = trait_ref. self_ty ( ) ;
901
+ let mut selcx = SelectionContext :: new ( self ) ;
905
902
906
- let mut refs_remaining = refs_number ;
907
- let mut trait_type = trait_ref . self_ty ( ) ;
908
- let mut selcx = SelectionContext :: new ( self ) ;
903
+ while refs_remaining > 0 {
904
+ if let ty :: TypeVariants :: TyRef ( _ , ty :: TypeAndMut { ty : t_type , mutbl : _ } ) =
905
+ trait_type . sty {
909
906
910
- while refs_remaining > 0 {
911
- if let ty:: TypeVariants :: TyRef ( _, ty:: TypeAndMut { ty : t_type, mutbl : _ } ) =
912
- trait_type. sty {
913
907
trait_type = t_type;
914
908
refs_remaining -= 1 ;
915
909
@@ -920,14 +914,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
920
914
new_trait_ref. to_predicate ( ) ) ;
921
915
922
916
if selcx. evaluate_obligation ( & new_obligation) {
923
- for i in 0 ..refs_number {
924
- snippet. remove ( i) ;
925
- }
926
- err. span_suggestion ( span, "consider removing `&`s like" , format ! ( "{}" , snippet) ) ;
917
+ let suggest_snippet = snippet. chars ( )
918
+ . skip ( refs_number)
919
+ . collect :: < String > ( ) ;
920
+
921
+ err. span_suggestion ( span,
922
+ "consider removing `&`s like" ,
923
+ format ! ( "{}" , suggest_snippet) ) ;
924
+
925
+ break ;
927
926
}
928
927
} else {
929
928
break ;
930
929
}
930
+ }
931
931
}
932
932
}
933
933
0 commit comments