Merge pull request #3308 from topecongiro/issue-2835

Prioritize single_line_fn and empty_item_single_line over brace_style
This commit is contained in:
Seiichi Uchida 2019-02-04 00:11:01 +09:00 committed by GitHub
commit bfcfaf1743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 48 deletions

View File

@ -333,19 +333,21 @@ pub fn rewrite_fn(
newline_brace = false; newline_brace = false;
} }
// Prepare for the function body by possibly adding a newline and if let rw @ Some(..) = self.single_line_fn(&result, block, inner_attrs) {
// indent. rw
// 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 { } 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( pub fn rewrite_required_fn(
@ -390,42 +392,37 @@ fn single_line_fn(
if self.config.empty_item_single_line() if self.config.empty_item_single_line()
&& is_empty_block(block, None, source_map) && 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)); return Some(format!("{} {{}}", fn_str));
} }
if self.config.fn_single_line() && is_simple_block_stmt(block, None, source_map) { if !self.config.fn_single_line() || !is_simple_block_stmt(block, None, source_map) {
let rewrite = { return None;
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()) let stmt = block.stmts.first()?;
.map(|s| s + suffix) let res = match stmt_expr(stmt) {
.or_else(|| Some(self.snippet(e.span).to_owned())) Some(e) => {
} let suffix = if semicolon_for_expr(&self.get_context(), e) {
None => stmt.rewrite(&self.get_context(), self.shape()), ";"
}
} else { } else {
None ""
} };
};
if let Some(res) = rewrite { format_expr(e, ExprType::Statement, &self.get_context(), self.shape())
let width = self.block_indent.width() + fn_str.len() + res.len() + 4; .map(|s| s + suffix)?
if !res.contains('\n') && width <= self.config.max_width() {
return Some(format!("{}{{ {} }}", fn_str, res));
}
} }
} 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) { pub fn visit_static(&mut self, static_parts: &StaticParts) {

View File

@ -0,0 +1,7 @@
// rustfmt-brace_style: AlwaysNextLine
// rustfmt-fn_single_line: true
fn lorem() -> i32
{
42
}

View File

@ -21,7 +21,5 @@ struct Dolor<T>
mod tests mod tests
{ {
#[test] #[test]
fn it_works() fn it_works() {}
{
}
} }

View File

@ -30,11 +30,7 @@ fn foo(
trait Test trait Test
{ {
fn foo(a: u8) fn foo(a: u8) {}
{
}
fn bar(a: u8) -> String fn bar(a: u8) -> String {}
{
}
} }

View File

@ -0,0 +1,4 @@
// rustfmt-brace_style: AlwaysNextLine
// rustfmt-fn_single_line: true
fn lorem() -> i32 { 42 }