Skip to content

Commit d7923b1

Browse files
committed
rustfmt: support const block items
1 parent a0dc8e3 commit d7923b1

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/visitor.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rustc_span::{BytePos, Ident, Pos, Span, symbol};
77
use tracing::debug;
88

99
use crate::attr::*;
10-
use crate::comment::{CodeCharKind, CommentCodeSlices, contains_comment, rewrite_comment};
10+
use crate::comment::{
11+
CodeCharKind, CommentCodeSlices, contains_comment, recover_comment_removed, rewrite_comment,
12+
};
1113
use crate::config::{BraceStyle, Config, MacroSelector, StyleEdition};
1214
use crate::coverage::transform_missing_snippet;
1315
use crate::items::{
@@ -533,6 +535,28 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
533535
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
534536
self.visit_static(&StaticParts::from_item(item));
535537
}
538+
ast::ItemKind::ConstBlock(ast::ConstBlockItem {
539+
id: _,
540+
span,
541+
ref block,
542+
}) => {
543+
let context = &self.get_context();
544+
let offset = self.block_indent;
545+
self.push_rewrite(
546+
item.span,
547+
block
548+
.rewrite(
549+
context,
550+
Shape::legacy(
551+
context.budget(offset.block_indent),
552+
offset.block_only(),
553+
),
554+
)
555+
.map(|rhs| {
556+
recover_comment_removed(format!("const {rhs}"), span, context)
557+
}),
558+
);
559+
}
536560
ast::ItemKind::Fn(ref fn_kind) => {
537561
let ast::Fn {
538562
defaultness,

tests/source/const-block-items.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(const_block_items)]
2+
3+
const {
4+
5+
6+
assert!(true)
7+
}
8+
9+
#[cfg(false)] const { assert!(false) }
10+
11+
12+
#[cfg(false)]
13+
// foo
14+
const
15+
16+
{
17+
// bar
18+
assert!(false)
19+
// baz
20+
} // 123
21+
22+
23+
#[expect(unused)]
24+
pub const { let a = 1; assert!(true); }

tests/target/const-block-items.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(const_block_items)]
2+
3+
const { assert!(true) }
4+
5+
#[cfg(false)]
6+
const { assert!(false) }
7+
8+
#[cfg(false)]
9+
// foo
10+
const {
11+
// bar
12+
assert!(false)
13+
// baz
14+
} // 123
15+
16+
#[expect(unused)]
17+
const {
18+
let a = 1;
19+
assert!(true);
20+
}

0 commit comments

Comments
 (0)