Skip to content

Commit 726aa14

Browse files
committed
Use snippet instead of pprinting statement
1 parent 4bb6b4a commit 726aa14

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

src/libsyntax/parse/parser.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -4378,9 +4378,10 @@ impl<'a> Parser<'a> {
43784378
Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
43794379
}
43804380

4381-
fn parse_stmt_without_recovery(&mut self,
4382-
macro_legacy_warnings: bool)
4383-
-> PResult<'a, Option<Stmt>> {
4381+
fn parse_stmt_without_recovery(
4382+
&mut self,
4383+
macro_legacy_warnings: bool,
4384+
) -> PResult<'a, Option<Stmt>> {
43844385
maybe_whole!(self, NtStmt, |x| Some(x));
43854386

43864387
let attrs = self.parse_outer_attributes()?;
@@ -4586,20 +4587,15 @@ impl<'a> Parser<'a> {
45864587
if self.eat(&token::Semi) {
45874588
stmt_span = stmt_span.with_hi(self.prev_span.hi());
45884589
}
4589-
let sugg = pprust::to_string(|s| {
4590-
use crate::print::pprust::INDENT_UNIT;
4591-
s.ibox(INDENT_UNIT);
4592-
s.bopen();
4593-
s.print_stmt(&stmt);
4594-
s.bclose_maybe_open(stmt.span, false)
4595-
});
4596-
e.span_suggestion(
4597-
stmt_span,
4598-
"try placing this code inside a block",
4599-
sugg,
4600-
// speculative, has been misleading in the past (closed Issue #46836)
4601-
Applicability::MaybeIncorrect
4602-
);
4590+
if let Ok(snippet) = self.sess.source_map().span_to_snippet(stmt_span) {
4591+
e.span_suggestion(
4592+
stmt_span,
4593+
"try placing this code inside a block",
4594+
format!("{{ {} }}", snippet),
4595+
// speculative, has been misleading in the past (#46836)
4596+
Applicability::MaybeIncorrect,
4597+
);
4598+
}
46034599
}
46044600
Err(mut e) => {
46054601
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);

src/libsyntax/print/pprust.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,7 @@ impl<'a> State<'a> {
745745
self.bclose_maybe_open(span, true)
746746
}
747747

748-
crate fn break_offset_if_not_bol(&mut self, n: usize,
749-
off: isize) {
748+
crate fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
750749
if !self.s.is_beginning_of_line() {
751750
self.s.break_offset(n, off)
752751
} else {

src/test/ui/issues/issue-39848.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected `{`, found `foo`
22
--> $DIR/issue-39848.rs:8:19
33
|
44
LL | if $tgt.has_$field() {}
5-
| -- - help: try placing this code inside a block: `{ foo(); }`
5+
| -- - help: try placing this code inside a block: `{ ) }`
66
| |
77
| this `if` statement has a condition, but no block
88
...

src/test/ui/label/label_break_value_illegal_uses.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | if true 'b: {}
1111
| -- ^^----
1212
| | |
1313
| | expected `{`
14-
| | help: try placing this code inside a block: `{ 'b: { } }`
14+
| | help: try placing this code inside a block: `{ 'b: {} }`
1515
| this `if` statement has a condition, but no block
1616

1717
error: expected `{`, found `'b`
@@ -21,7 +21,7 @@ LL | if true {} else 'b: {}
2121
| ^^----
2222
| |
2323
| expected `{`
24-
| help: try placing this code inside a block: `{ 'b: { } }`
24+
| help: try placing this code inside a block: `{ 'b: {} }`
2525

2626
error: expected one of `.`, `?`, `{`, or an operator, found `'b`
2727
--> $DIR/label_break_value_illegal_uses.rs:18:17

0 commit comments

Comments
 (0)