Skip to content

Commit

Permalink
Fix incorrect strip r# prefix from break and continue labels (#6425)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobatha authored Dec 22, 2024
1 parent 8a2c073 commit a43eef1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,22 @@ pub(crate) fn format_expr(
}
ast::ExprKind::Continue(ref opt_label) => {
let id_str = match *opt_label {
Some(label) => format!(" {}", label.ident),
Some(label) => {
// Ident lose the `r#` prefix in raw labels, so use the original snippet
let label_name = context.snippet(label.ident.span);
format!(" {}", label_name)
}
None => String::new(),
};
Ok(format!("continue{id_str}"))
}
ast::ExprKind::Break(ref opt_label, ref opt_expr) => {
let id_str = match *opt_label {
Some(label) => format!(" {}", label.ident),
Some(label) => {
// Ident lose the `r#` prefix in raw labels, so use the original snippet
let label_name = context.snippet(label.ident.span);
format!(" {}", label_name)
}
None => String::new(),
};

Expand Down
21 changes: 21 additions & 0 deletions tests/target/issue-6411.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// rustfmt-edition: 2021

fn test_break() {
'r#if: {
break 'r#if;
}

'r#a: {
break 'r#a;
}
}

fn test_continue() {
'r#if: {
continue 'r#if;
}

'r#a: {
continue 'r#a;
}
}

0 comments on commit a43eef1

Please sign in to comment.