Skip to content

Commit a43eef1

Browse files
authored
Fix incorrect strip r# prefix from break and continue labels (#6425)
1 parent 8a2c073 commit a43eef1

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/expr.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,22 @@ pub(crate) fn format_expr(
200200
}
201201
ast::ExprKind::Continue(ref opt_label) => {
202202
let id_str = match *opt_label {
203-
Some(label) => format!(" {}", label.ident),
203+
Some(label) => {
204+
// Ident lose the `r#` prefix in raw labels, so use the original snippet
205+
let label_name = context.snippet(label.ident.span);
206+
format!(" {}", label_name)
207+
}
204208
None => String::new(),
205209
};
206210
Ok(format!("continue{id_str}"))
207211
}
208212
ast::ExprKind::Break(ref opt_label, ref opt_expr) => {
209213
let id_str = match *opt_label {
210-
Some(label) => format!(" {}", label.ident),
214+
Some(label) => {
215+
// Ident lose the `r#` prefix in raw labels, so use the original snippet
216+
let label_name = context.snippet(label.ident.span);
217+
format!(" {}", label_name)
218+
}
211219
None => String::new(),
212220
};
213221

tests/target/issue-6411.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// rustfmt-edition: 2021
2+
3+
fn test_break() {
4+
'r#if: {
5+
break 'r#if;
6+
}
7+
8+
'r#a: {
9+
break 'r#a;
10+
}
11+
}
12+
13+
fn test_continue() {
14+
'r#if: {
15+
continue 'r#if;
16+
}
17+
18+
'r#a: {
19+
continue 'r#a;
20+
}
21+
}

0 commit comments

Comments
 (0)