From 2ab5b8297aa022173d0a46715992516bac0f2c07 Mon Sep 17 00:00:00 2001 From: sobatha Date: Sun, 22 Dec 2024 22:06:52 +0900 Subject: [PATCH] Fix incorrect strip r# prefix from labels --- src/expr.rs | 12 ++++++++++-- tests/target/issue-6411.rs | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/target/issue-6411.rs diff --git a/src/expr.rs b/src/expr.rs index 068af184e3c..0751d65a438 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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(), }; diff --git a/tests/target/issue-6411.rs b/tests/target/issue-6411.rs new file mode 100644 index 00000000000..a1166a749f2 --- /dev/null +++ b/tests/target/issue-6411.rs @@ -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; + } +}