11use rustc:: mir:: * ;
22use rustc:: ty;
33use rustc_errors:: { Applicability , DiagnosticBuilder } ;
4- use rustc_span:: Span ;
4+ use rustc_span:: source_map:: DesugaringKind ;
5+ use rustc_span:: { Span , Symbol } ;
56
67use crate :: borrow_check:: diagnostics:: UseSpans ;
78use crate :: borrow_check:: prefixes:: PrefixSet ;
@@ -383,10 +384,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
383384 }
384385 }
385386 } ;
386- let move_ty = format ! ( "{:?}" , move_place. ty( * self . body, self . infcx. tcx) . ty, ) ;
387387 if let Ok ( snippet) = self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( span) {
388- let is_option = move_ty. starts_with ( "std::option::Option" ) ;
389- let is_result = move_ty. starts_with ( "std::result::Result" ) ;
388+ let def_id = match move_place. ty ( * self . body , self . infcx . tcx ) . ty . kind {
389+ ty:: Adt ( self_def, _) => self_def. did ,
390+ ty:: Foreign ( def_id)
391+ | ty:: FnDef ( def_id, _)
392+ | ty:: Closure ( def_id, _)
393+ | ty:: Generator ( def_id, ..)
394+ | ty:: Opaque ( def_id, _) => def_id,
395+ _ => return err,
396+ } ;
397+ let is_option =
398+ self . infcx . tcx . is_diagnostic_item ( Symbol :: intern ( "option_type" ) , def_id) ;
399+ let is_result =
400+ self . infcx . tcx . is_diagnostic_item ( Symbol :: intern ( "result_type" ) , def_id) ;
390401 if ( is_option || is_result) && use_spans. map_or ( true , |v| !v. for_closure ( ) ) {
391402 err. span_suggestion (
392403 span,
@@ -397,6 +408,16 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
397408 format ! ( "{}.as_ref()" , snippet) ,
398409 Applicability :: MaybeIncorrect ,
399410 ) ;
411+ } else if span. is_desugaring ( DesugaringKind :: ForLoop )
412+ && self . infcx . tcx . is_diagnostic_item ( Symbol :: intern ( "vec_type" ) , def_id)
413+ {
414+ // FIXME: suggest for anything that implements `IntoIterator`.
415+ err. span_suggestion (
416+ span,
417+ "consider iterating over a slice of the `Vec<_>`'s content" ,
418+ format ! ( "&{}" , snippet) ,
419+ Applicability :: MaybeIncorrect ,
420+ ) ;
400421 }
401422 }
402423 err
0 commit comments