Skip to content

Commit

Permalink
BraceStyle AlwaysNewLine for unsafe blocks
Browse files Browse the repository at this point in the history
* Unsafe blocks can now get an opening brace on a new line

* Add test case for unsafe blocks
  • Loading branch information
IceTDrinker authored and calebcartwright committed Sep 17, 2020
1 parent 39fe0e1 commit 4a88598
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/formatting/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 6,7 @@ use rustc_ast::token::{DelimToken, LitKind};
use rustc_ast::{ast, ptr};
use rustc_span::{BytePos, Span};

use crate::config::{lists::*, Config, ControlBraceStyle, IndentStyle};
use crate::config::{lists::*, BraceStyle, Config, ControlBraceStyle, IndentStyle};
use crate::formatting::{
chains::rewrite_chain,
closures,
Expand Down Expand Up @@ -127,9 127,19 @@ pub(crate) fn format_expr(
ast::ExprKind::Block(ref block, opt_label) => {
match expr_type {
ExprType::Statement => {
if let rw @ Some(_) =
rewrite_empty_block(context, block, Some(&expr.attrs), opt_label, "", shape)
{
let empty_block_prefix = if is_unsafe_block(block) {
"unsafe "
} else {
""
};
if let rw @ Some(_) = rewrite_empty_block(
context,
block,
Some(&expr.attrs),
opt_label,
empty_block_prefix,
shape,
) {
// Rewrite block without trying to put it in a single line.
rw
} else {
Expand Down Expand Up @@ -486,17 496,28 @@ fn block_prefix(context: &RewriteContext<'_>, block: &ast::Block, shape: Shape)
if !trimmed.is_empty() {
// 9 = "unsafe {".len(), 7 = "unsafe ".len()
let budget = shape.width.checked_sub(9)?;
format!(
"unsafe {} ",
rewrite_comment(
trimmed,
true,
Shape::legacy(budget, shape.indent 7),
context.config,
)?
)
let rewritten_comment = rewrite_comment(
trimmed,
true,
Shape::legacy(budget, shape.indent 7),
context.config,
)?;
match context.config.brace_style() {
BraceStyle::AlwaysNextLine => format!(
"unsafe {}{}",
rewritten_comment,
shape.indent.to_string_with_newline(&context.config)
),
_ => format!("unsafe {} ", rewritten_comment),
}
} else {
"unsafe ".to_owned()
match context.config.brace_style() {
BraceStyle::AlwaysNextLine => format!(
"unsafe{}",
shape.indent.to_string_with_newline(&context.config)
),
_ => "unsafe ".to_owned(),
}
}
}
ast::BlockCheckMode::Default => String::new(),
Expand Down
25 changes: 25 additions & 0 deletions tests/source/configs/brace_style/unsafe_always_next_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,25 @@
// rustfmt-brace_style: AlwaysNextLine
// AlwaysNextLine brace style for unsafe blocks

fn main()
{
unsafe
{
let good = ();
}

unsafe {}

unsafe {
let ugly = ();
}

unsafe /* f */ {}

unsafe /* f*/ {
let x = 1;
}

unsafe { /*lol*/
}
}
30 changes: 30 additions & 0 deletions tests/target/configs/brace_style/unsafe_always_next_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,30 @@
// rustfmt-brace_style: AlwaysNextLine
// AlwaysNextLine brace style for unsafe blocks

fn main()
{
unsafe
{
let good = ();
}

unsafe {}

unsafe
{
let ugly = ();
}

unsafe /* f */
{
}

unsafe /* f*/
{
let x = 1;
}

unsafe
{ /*lol*/
}
}

0 comments on commit 4a88598

Please sign in to comment.