@@ -883,6 +883,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
883
883
}
884
884
}
885
885
886
+ /// Whenever references are used by mistake, like `for (i, e) in &vec.iter().enumerate()`,
887
+ /// suggest removing these references until we reach a type that implements the trait.
886
888
fn suggest_remove_reference ( & self ,
887
889
obligation : & PredicateObligation < ' tcx > ,
888
890
err : & mut DiagnosticBuilder < ' tcx > ,
@@ -896,16 +898,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
896
898
. take_while ( |c| * c == '&' )
897
899
. count ( ) ;
898
900
899
- let mut refs_remaining = refs_number;
900
901
let mut trait_type = trait_ref. self_ty ( ) ;
901
902
let mut selcx = SelectionContext :: new ( self ) ;
902
903
903
- while refs_remaining > 0 {
904
+ for refs_remaining in 0 ..refs_number {
904
905
if let ty:: TypeVariants :: TyRef ( _, ty:: TypeAndMut { ty : t_type, mutbl : _ } ) =
905
906
trait_type. sty {
906
907
907
908
trait_type = t_type;
908
- refs_remaining -= 1 ;
909
909
910
910
let substs = self . tcx . mk_substs_trait ( trait_type, & [ ] ) ;
911
911
let new_trait_ref = ty:: TraitRef :: new ( trait_ref. def_id , substs) ;
@@ -914,12 +914,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
914
914
new_trait_ref. to_predicate ( ) ) ;
915
915
916
916
if selcx. evaluate_obligation ( & new_obligation) {
917
+ let remove_refs = refs_remaining + 1 ;
918
+
917
919
let suggest_snippet = snippet. chars ( )
918
- . skip ( refs_number)
920
+ . filter ( |c| !c. is_whitespace ( ) )
921
+ . skip ( remove_refs)
919
922
. collect :: < String > ( ) ;
920
923
921
924
err. span_suggestion ( span,
922
- "consider removing `&`s like" ,
925
+ & format ! ( "consider removing {} references `&`" ,
926
+ remove_refs) ,
923
927
format ! ( "{}" , suggest_snippet) ) ;
924
928
925
929
break ;
0 commit comments