From 5cb5ddeb3c8463ecb9472bea03b69a9521e87c5c Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 3 Apr 2020 03:17:40 +0200 Subject: [PATCH] .unwrap() less on .span_to_snippet() --- src/librustc_parse/parser/stmt.rs | 6 ++-- .../traits/error_reporting/suggestions.rs | 32 +++++++++---------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/librustc_parse/parser/stmt.rs b/src/librustc_parse/parser/stmt.rs index fddfe48bf8670..b3764d2d47be1 100644 --- a/src/librustc_parse/parser/stmt.rs +++ b/src/librustc_parse/parser/stmt.rs @@ -165,9 +165,9 @@ impl<'a> Parser<'a> { // Rewind to before attempting to parse the type and continue parsing. let parser_snapshot_after_type = self.clone(); mem::replace(self, parser_snapshot_before_type); - - let snippet = self.span_to_snippet(pat.span).unwrap(); - err.span_label(pat.span, format!("while parsing the type for `{}`", snippet)); + if let Ok(snip) = self.span_to_snippet(pat.span) { + err.span_label(pat.span, format!("while parsing the type for `{}`", snip)); + } (Some((parser_snapshot_after_type, colon_sp, err)), None) } } diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index 6a352602e266a..fcec29aaa8ecb 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -732,12 +732,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { true }; + let sm = self.tcx.sess.source_map(); let (snippet, last_ty) = if let (true, hir::TyKind::TraitObject(..), Ok(snippet), true, Some(last_ty)) = ( // Verify that we're dealing with a return `dyn Trait` ret_ty.span.overlaps(span), &ret_ty.kind, - self.tcx.sess.source_map().span_to_snippet(ret_ty.span), + sm.span_to_snippet(ret_ty.span), // If any of the return types does not conform to the trait, then we can't // suggest `impl Trait` nor trait objects, it is a type mismatch error. all_returns_conform_to_trait, @@ -775,26 +776,23 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { if is_object_safe { // Suggest `-> Box` and `Box::new(returned_value)`. // Get all the return values and collect their span and suggestion. - let mut suggestions = visitor + if let Some(mut suggestions) = visitor .returns .iter() .map(|expr| { - ( - expr.span, - format!( - "Box::new({})", - self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap() - ), - ) + let snip = sm.span_to_snippet(expr.span).ok()?; + Some((expr.span, format!("Box::new({})", snip))) }) - .collect::>(); - // Add the suggestion for the return type. - suggestions.push((ret_ty.span, format!("Box", trait_obj))); - err.multipart_suggestion( - "return a boxed trait object instead", - suggestions, - Applicability::MaybeIncorrect, - ); + .collect::>>() + { + // Add the suggestion for the return type. + suggestions.push((ret_ty.span, format!("Box", trait_obj))); + err.multipart_suggestion( + "return a boxed trait object instead", + suggestions, + Applicability::MaybeIncorrect, + ); + } } else { // This is currently not possible to trigger because E0038 takes precedence, but // leave it in for completeness in case anything changes in an earlier stage.