Skip to content

Commit 4a88598

Browse files
IceTDrinkercalebcartwright
authored andcommitted
BraceStyle AlwaysNewLine for unsafe blocks
* Unsafe blocks can now get an opening brace on a new line * Add test case for unsafe blocks
1 parent 39fe0e1 commit 4a88598

File tree

3 files changed

+90
-14
lines changed

3 files changed

+90
-14
lines changed

src/formatting/expr.rs

+35-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::token::{DelimToken, LitKind};
66
use rustc_ast::{ast, ptr};
77
use rustc_span::{BytePos, Span};
88

9-
use crate::config::{lists::*, Config, ControlBraceStyle, IndentStyle};
9+
use crate::config::{lists::*, BraceStyle, Config, ControlBraceStyle, IndentStyle};
1010
use crate::formatting::{
1111
chains::rewrite_chain,
1212
closures,
@@ -127,9 +127,19 @@ pub(crate) fn format_expr(
127127
ast::ExprKind::Block(ref block, opt_label) => {
128128
match expr_type {
129129
ExprType::Statement => {
130-
if let rw @ Some(_) =
131-
rewrite_empty_block(context, block, Some(&expr.attrs), opt_label, "", shape)
132-
{
130+
let empty_block_prefix = if is_unsafe_block(block) {
131+
"unsafe "
132+
} else {
133+
""
134+
};
135+
if let rw @ Some(_) = rewrite_empty_block(
136+
context,
137+
block,
138+
Some(&expr.attrs),
139+
opt_label,
140+
empty_block_prefix,
141+
shape,
142+
) {
133143
// Rewrite block without trying to put it in a single line.
134144
rw
135145
} else {
@@ -486,17 +496,28 @@ fn block_prefix(context: &RewriteContext<'_>, block: &ast::Block, shape: Shape)
486496
if !trimmed.is_empty() {
487497
// 9 = "unsafe {".len(), 7 = "unsafe ".len()
488498
let budget = shape.width.checked_sub(9)?;
489-
format!(
490-
"unsafe {} ",
491-
rewrite_comment(
492-
trimmed,
493-
true,
494-
Shape::legacy(budget, shape.indent + 7),
495-
context.config,
496-
)?
497-
)
499+
let rewritten_comment = rewrite_comment(
500+
trimmed,
501+
true,
502+
Shape::legacy(budget, shape.indent + 7),
503+
context.config,
504+
)?;
505+
match context.config.brace_style() {
506+
BraceStyle::AlwaysNextLine => format!(
507+
"unsafe {}{}",
508+
rewritten_comment,
509+
shape.indent.to_string_with_newline(&context.config)
510+
),
511+
_ => format!("unsafe {} ", rewritten_comment),
512+
}
498513
} else {
499-
"unsafe ".to_owned()
514+
match context.config.brace_style() {
515+
BraceStyle::AlwaysNextLine => format!(
516+
"unsafe{}",
517+
shape.indent.to_string_with_newline(&context.config)
518+
),
519+
_ => "unsafe ".to_owned(),
520+
}
500521
}
501522
}
502523
ast::BlockCheckMode::Default => String::new(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// AlwaysNextLine brace style for unsafe blocks
3+
4+
fn main()
5+
{
6+
unsafe
7+
{
8+
let good = ();
9+
}
10+
11+
unsafe {}
12+
13+
unsafe {
14+
let ugly = ();
15+
}
16+
17+
unsafe /* f */ {}
18+
19+
unsafe /* f*/ {
20+
let x = 1;
21+
}
22+
23+
unsafe { /*lol*/
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// AlwaysNextLine brace style for unsafe blocks
3+
4+
fn main()
5+
{
6+
unsafe
7+
{
8+
let good = ();
9+
}
10+
11+
unsafe {}
12+
13+
unsafe
14+
{
15+
let ugly = ();
16+
}
17+
18+
unsafe /* f */
19+
{
20+
}
21+
22+
unsafe /* f*/
23+
{
24+
let x = 1;
25+
}
26+
27+
unsafe
28+
{ /*lol*/
29+
}
30+
}

0 commit comments

Comments
 (0)