Skip to content

Commit 75180ed

Browse files
committed
enhance const block formatting
1 parent cbe01e4 commit 75180ed

File tree

5 files changed

+207
-1
lines changed

5 files changed

+207
-1
lines changed

src/formatting/expr.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,29 @@ pub(crate) fn format_expr(
125125
| ast::ExprKind::While(..) => to_control_flow(expr, expr_type)
126126
.and_then(|control_flow| control_flow.rewrite(context, shape)),
127127
ast::ExprKind::ConstBlock(ref anon_const) => {
128-
Some(format!("const {}", anon_const.rewrite(context, shape)?))
128+
let between_span = mk_sp(
129+
context.snippet_provider.span_after(expr.span, "const"),
130+
anon_const.value.span.lo(),
131+
);
132+
let mut result = "const".to_string();
133+
let post_comment = rewrite_missing_comment(between_span, shape, context)?;
134+
if contains_comment(context.snippet(between_span)) {
135+
result.push_str(" ");
136+
result.push_str(&post_comment);
137+
}
138+
match context.config.brace_style() {
139+
BraceStyle::AlwaysNextLine => Some(format!(
140+
"{}{}{}",
141+
result,
142+
shape.to_string_with_newline(context.config),
143+
anon_const.rewrite(context, shape)?
144+
)),
145+
_ => Some(format!(
146+
"{} {}",
147+
result,
148+
anon_const.rewrite(context, shape)?
149+
)),
150+
}
129151
}
130152
ast::ExprKind::Block(ref block, opt_label) => {
131153
match expr_type {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// AlwaysNextLine brace style for const blocks
3+
4+
fn foo() -> i32 {
5+
const {
6+
let x = 5 + 10;
7+
x / 3
8+
}
9+
}
10+
11+
fn bar() -> i32 {
12+
const { 4 }
13+
}
14+
15+
fn foo() -> i32 {
16+
const
17+
{
18+
let x = 5 + 10;
19+
x / 3
20+
}
21+
}
22+
23+
fn foo() -> i32 {
24+
const /*qux */
25+
{
26+
let x = 5 + 10;
27+
x / 3
28+
}
29+
}
30+
31+
fn foo() -> i32 {
32+
const /*qux */ {
33+
let x = 5 + 10;
34+
x / 3
35+
}
36+
}
37+
38+
fn foo() -> i32 {
39+
const
40+
/*qux */
41+
{
42+
let x = 5 + 10;
43+
x / 3
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-brace_style: SameLineWhere
2+
// SameLineWhere brace style for const blocks
3+
4+
fn foo() -> i32 {
5+
const {
6+
let x = 5 + 10;
7+
x / 3
8+
}
9+
}
10+
11+
fn bar() -> i32 {
12+
const { 4 }
13+
}
14+
15+
fn foo() -> i32 {
16+
const
17+
{
18+
let x = 5 + 10;
19+
x / 3
20+
}
21+
}
22+
23+
fn foo() -> i32 {
24+
const /*qux */
25+
{
26+
let x = 5 + 10;
27+
x / 3
28+
}
29+
}
30+
31+
fn foo() -> i32 {
32+
const /*qux */ {
33+
let x = 5 + 10;
34+
x / 3
35+
}
36+
}
37+
38+
fn foo() -> i32 {
39+
const
40+
/*qux */
41+
{
42+
let x = 5 + 10;
43+
x / 3
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// AlwaysNextLine brace style for const blocks
3+
4+
fn foo() -> i32
5+
{
6+
const
7+
{
8+
let x = 5 + 10;
9+
x / 3
10+
}
11+
}
12+
13+
fn bar() -> i32
14+
{
15+
const
16+
{ 4 }
17+
}
18+
19+
fn foo() -> i32
20+
{
21+
const
22+
{
23+
let x = 5 + 10;
24+
x / 3
25+
}
26+
}
27+
28+
fn foo() -> i32
29+
{
30+
const /*qux */
31+
{
32+
let x = 5 + 10;
33+
x / 3
34+
}
35+
}
36+
37+
fn foo() -> i32
38+
{
39+
const /*qux */
40+
{
41+
let x = 5 + 10;
42+
x / 3
43+
}
44+
}
45+
46+
fn foo() -> i32
47+
{
48+
const /*qux */
49+
{
50+
let x = 5 + 10;
51+
x / 3
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// rustfmt-brace_style: SameLineWhere
2+
// SameLineWhere brace style for const blocks
3+
4+
fn foo() -> i32 {
5+
const {
6+
let x = 5 + 10;
7+
x / 3
8+
}
9+
}
10+
11+
fn bar() -> i32 {
12+
const { 4 }
13+
}
14+
15+
fn foo() -> i32 {
16+
const {
17+
let x = 5 + 10;
18+
x / 3
19+
}
20+
}
21+
22+
fn foo() -> i32 {
23+
const /*qux */ {
24+
let x = 5 + 10;
25+
x / 3
26+
}
27+
}
28+
29+
fn foo() -> i32 {
30+
const /*qux */ {
31+
let x = 5 + 10;
32+
x / 3
33+
}
34+
}
35+
36+
fn foo() -> i32 {
37+
const /*qux */ {
38+
let x = 5 + 10;
39+
x / 3
40+
}
41+
}

0 commit comments

Comments
 (0)