Skip to content

Commit

Permalink
Merge pull request rust-lang#3308 from topecongiro/issue-2835
Browse files Browse the repository at this point in the history
Prioritize single_line_fn and empty_item_single_line over brace_style
  • Loading branch information
topecongiro committed Feb 3, 2019
2 parents efd6fda + 0142e96 commit bfcfaf1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 51 deletions.
81 changes: 39 additions & 42 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,19 +333,21 @@ impl<'a> FmtVisitor<'a> {
newline_brace = false;
}

// Prepare for the function body by possibly adding a newline and
// indent.
// FIXME we'll miss anything between the end of the signature and the
// start of the body, but we need more spans from the compiler to solve
// this.
if newline_brace {
result.push_str(&indent.to_string_with_newline(self.config));
if let rw @ Some(..) = self.single_line_fn(&result, block, inner_attrs) {
rw
} else {
result.push(' ');
// Prepare for the function body by possibly adding a newline and
// indent.
// FIXME we'll miss anything between the end of the signature and the
// start of the body, but we need more spans from the compiler to solve
// this.
if newline_brace {
result.push_str(&indent.to_string_with_newline(self.config));
} else {
result.push(' ');
}
Some(result)
}

self.single_line_fn(&result, block, inner_attrs)
.or_else(|| Some(result))
}

pub fn rewrite_required_fn(
Expand Down Expand Up @@ -390,42 +392,37 @@ impl<'a> FmtVisitor<'a> {

if self.config.empty_item_single_line()
&& is_empty_block(block, None, source_map)
&& self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width()
&& self.block_indent.width() + fn_str.len() + 3 <= self.config.max_width()
&& !last_line_contains_single_line_comment(fn_str)
{
return Some(format!("{}{{}}", fn_str));
}

if self.config.fn_single_line() && is_simple_block_stmt(block, None, source_map) {
let rewrite = {
if let Some(stmt) = block.stmts.first() {
match stmt_expr(stmt) {
Some(e) => {
let suffix = if semicolon_for_expr(&self.get_context(), e) {
";"
} else {
""
};

format_expr(e, ExprType::Statement, &self.get_context(), self.shape())
.map(|s| s + suffix)
.or_else(|| Some(self.snippet(e.span).to_owned()))
}
None => stmt.rewrite(&self.get_context(), self.shape()),
}
return Some(format!("{} {{}}", fn_str));
}

if !self.config.fn_single_line() || !is_simple_block_stmt(block, None, source_map) {
return None;
}

let stmt = block.stmts.first()?;
let res = match stmt_expr(stmt) {
Some(e) => {
let suffix = if semicolon_for_expr(&self.get_context(), e) {
";"
} else {
None
}
};
""
};

if let Some(res) = rewrite {
let width = self.block_indent.width() + fn_str.len() + res.len() + 4;
if !res.contains('\n') && width <= self.config.max_width() {
return Some(format!("{}{{ {} }}", fn_str, res));
}
format_expr(e, ExprType::Statement, &self.get_context(), self.shape())
.map(|s| s + suffix)?
}
}
None => stmt.rewrite(&self.get_context(), self.shape())?,
};

None
let width = self.block_indent.width() + fn_str.len() + res.len() + 5;
if !res.contains('\n') && width <= self.config.max_width() {
Some(format!("{} {{ {} }}", fn_str, res))
} else {
None
}
}

pub fn visit_static(&mut self, static_parts: &StaticParts) {
Expand Down
7 changes: 7 additions & 0 deletions tests/source/issue-2835.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-brace_style: AlwaysNextLine
// rustfmt-fn_single_line: true

fn lorem() -> i32
{
42
}
4 changes: 1 addition & 3 deletions tests/target/configs/brace_style/item_always_next_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ where
mod tests
{
#[test]
fn it_works()
{
}
fn it_works() {}
}
8 changes: 2 additions & 6 deletions tests/target/fn-custom-7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ fn foo(

trait Test
{
fn foo(a: u8)
{
}
fn foo(a: u8) {}

fn bar(a: u8) -> String
{
}
fn bar(a: u8) -> String {}
}
4 changes: 4 additions & 0 deletions tests/target/issue-2835.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// rustfmt-brace_style: AlwaysNextLine
// rustfmt-fn_single_line: true

fn lorem() -> i32 { 42 }

0 comments on commit bfcfaf1

Please sign in to comment.