]> git.lizzy.rs Git - rust.git/commitdiff
Use snippet instead of pprinting statement
authorEsteban Küber <esteban@kuber.com.ar>
Fri, 12 Jul 2019 21:01:13 +0000 (14:01 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 12 Jul 2019 21:01:13 +0000 (14:01 -0700)
src/libsyntax/parse/parser.rs
src/libsyntax/print/pprust.rs
src/test/ui/issues/issue-39848.stderr
src/test/ui/label/label_break_value_illegal_uses.stderr

index 83dbff6b2d5746dda8bb5757a0032904a352fd8d..c79ccf21ba1c80817a2dca577cfae30420ae9785 100644 (file)
@@ -4378,9 +4378,10 @@ fn eat_macro_def(&mut self, attrs: &[Attribute], vis: &Visibility, lo: Span)
         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<Stmt>> {
+    fn parse_stmt_without_recovery(
+        &mut self,
+        macro_legacy_warnings: bool,
+    ) -> PResult<'a, Option<Stmt>> {
         maybe_whole!(self, NtStmt, |x| Some(x));
 
         let attrs = self.parse_outer_attributes()?;
@@ -4586,20 +4587,15 @@ pub fn parse_block(&mut self) -> PResult<'a, P<Block>> {
                     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);
index 3b6064fcdf7bf2c7e24fc86f000f9a6356fa21b8..0e3ce2787f3ed72399f4ec816cf617e5e9e120ae 100644 (file)
@@ -745,8 +745,7 @@ impl<'a> State<'a> {
         self.bclose_maybe_open(span, true)
     }
 
-    crate fn break_offset_if_not_bol(&mut self, n: usize,
-                                   off: isize) {
+    crate fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
         if !self.s.is_beginning_of_line() {
             self.s.break_offset(n, off)
         } else {
index 6eda03c1a08ede9e518280784989102aa5409879..fa87967432df3483452124e17da8ff209f89797e 100644 (file)
@@ -2,7 +2,7 @@ error: expected `{`, found `foo`
   --> $DIR/issue-39848.rs:8:19
    |
 LL |         if $tgt.has_$field() {}
-   |         --                 - help: try placing this code inside a block: `{ foo(); }`
+   |         --                 - help: try placing this code inside a block: `{ ) }`
    |         |
    |         this `if` statement has a condition, but no block
 ...
index 0b9754c3c70e35e294199380cfbaee0e7b13873d..80b4329ad40119edf3751f8bf0e827578a9797ff 100644 (file)
@@ -11,7 +11,7 @@ LL |     if true 'b: {}
    |     --      ^^----
    |     |       |
    |     |       expected `{`
-   |     |       help: try placing this code inside a block: `{ 'b: { } }`
+   |     |       help: try placing this code inside a block: `{ 'b: {} }`
    |     this `if` statement has a condition, but no block
 
 error: expected `{`, found `'b`
@@ -21,7 +21,7 @@ LL |     if true {} else 'b: {}
    |                     ^^----
    |                     |
    |                     expected `{`
-   |                     help: try placing this code inside a block: `{ 'b: { } }`
+   |                     help: try placing this code inside a block: `{ 'b: {} }`
 
 error: expected one of `.`, `?`, `{`, or an operator, found `'b`
   --> $DIR/label_break_value_illegal_uses.rs:18:17