From 726aa1437ff639927758da17bfbdee85f9bceda4 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Fri, 12 Jul 2019 14:01:13 -0700 Subject: Use snippet instead of pprinting statement --- src/libsyntax/parse/parser.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 83dbff6b2d5..c79ccf21ba1 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4378,9 +4378,10 @@ impl<'a> Parser<'a> { Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec()))) } - fn parse_stmt_without_recovery(&mut self, - macro_legacy_warnings: bool) - -> PResult<'a, Option> { + fn parse_stmt_without_recovery( + &mut self, + macro_legacy_warnings: bool, + ) -> PResult<'a, Option> { maybe_whole!(self, NtStmt, |x| Some(x)); let attrs = self.parse_outer_attributes()?; @@ -4586,20 +4587,15 @@ impl<'a> Parser<'a> { if self.eat(&token::Semi) { stmt_span = stmt_span.with_hi(self.prev_span.hi()); } - let sugg = pprust::to_string(|s| { - use crate::print::pprust::INDENT_UNIT; - s.ibox(INDENT_UNIT); - s.bopen(); - s.print_stmt(&stmt); - s.bclose_maybe_open(stmt.span, false) - }); - e.span_suggestion( - stmt_span, - "try placing this code inside a block", - sugg, - // speculative, has been misleading in the past (closed Issue #46836) - Applicability::MaybeIncorrect - ); + if let Ok(snippet) = self.sess.source_map().span_to_snippet(stmt_span) { + e.span_suggestion( + stmt_span, + "try placing this code inside a block", + format!("{{ {} }}", snippet), + // speculative, has been misleading in the past (#46836) + Applicability::MaybeIncorrect, + ); + } } Err(mut e) => { self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore); -- cgit 1.4.1-3-g733a5