From 4dd45069feda95e06cf8287629caea9c64d9d481 Mon Sep 17 00:00:00 2001 From: Yukio Siraichi Date: Sun, 11 Mar 2018 02:21:38 -0300 Subject: [PATCH] Refactored with high-order functions. --- src/librustc/traits/error_reporting.rs | 42 +++++++++++++------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index c90a6d0709db..5bfeff89e355 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -888,28 +888,22 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { err: &mut DiagnosticBuilder<'tcx>, trait_ref: &ty::Binder>) { let ty::Binder(trait_ref) = trait_ref; - let span = obligation.cause.span; - let mut snippet = match self.tcx.sess.codemap().span_to_snippet(span) { - Ok(s) => s, - Err(_) => String::from(""), - }; - let mut refs_number = 0; + if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) { + let refs_number = snippet.chars() + .filter(|c| !c.is_whitespace()) + .take_while(|c| *c == '&') + .count(); - for c in snippet.chars() { - if c == '&' { - refs_number += 1; - } - } + let mut refs_remaining = refs_number; + let mut trait_type = trait_ref.self_ty(); + let mut selcx = SelectionContext::new(self); - let mut refs_remaining = refs_number; - let mut trait_type = trait_ref.self_ty(); - let mut selcx = SelectionContext::new(self); + while refs_remaining > 0 { + if let ty::TypeVariants::TyRef(_, ty::TypeAndMut{ ty: t_type, mutbl: _ }) = + trait_type.sty { - while refs_remaining > 0 { - if let ty::TypeVariants::TyRef(_, ty::TypeAndMut{ ty: t_type, mutbl: _ }) = - trait_type.sty { trait_type = t_type; refs_remaining -= 1; @@ -920,14 +914,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { new_trait_ref.to_predicate()); if selcx.evaluate_obligation(&new_obligation) { - for i in 0..refs_number { - snippet.remove(i); - } - err.span_suggestion(span, "consider removing `&`s like", format!("{}", snippet)); + let suggest_snippet = snippet.chars() + .skip(refs_number) + .collect::(); + + err.span_suggestion(span, + "consider removing `&`s like", + format!("{}", suggest_snippet)); + + break; } } else { break; } + } } }