diff options
| author | WeiTheShinobi <weitheshinobi@gmail.com> | 2024-05-22 20:50:14 +0800 |
|---|---|---|
| committer | Yacin Tmimi <yacintmimi@gmail.com> | 2024-05-31 20:40:07 -0600 |
| commit | bf7bb5652469e78ae2ea636db1c77d163189badd (patch) | |
| tree | ec3c61bb0666c3b12e23a6915adcbab0953d3212 /src | |
| parent | 871113eb4246bf332fb4cd59d7aa432a6bb87520 (diff) | |
| download | rust-bf7bb5652469e78ae2ea636db1c77d163189badd.tar.gz rust-bf7bb5652469e78ae2ea636db1c77d163189badd.zip | |
Prevent rustfmt from removing inner attributes in inline const blocks
Fixes 6158
Diffstat (limited to 'src')
| -rw-r--r-- | src/expr.rs | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/expr.rs b/src/expr.rs index 6a21d88ac9d..8e92ef03770 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -139,7 +139,17 @@ pub(crate) fn format_expr( | ast::ExprKind::While(..) => to_control_flow(expr, expr_type) .and_then(|control_flow| control_flow.rewrite(context, shape)), ast::ExprKind::ConstBlock(ref anon_const) => { - Some(format!("const {}", anon_const.rewrite(context, shape)?)) + let rewrite = match anon_const.value.kind { + ast::ExprKind::Block(ref block, opt_label) => { + // Inner attributes are associated with the `ast::ExprKind::ConstBlock` node, + // not the `ast::Block` node we're about to rewrite. To prevent dropping inner + // attributes call `rewrite_block` directly. + // See https://github.com/rust-lang/rustfmt/issues/6158 + rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)? + } + _ => anon_const.rewrite(context, shape)?, + }; + Some(format!("const {}", rewrite)) } ast::ExprKind::Block(ref block, opt_label) => { match expr_type { |
