Skip to content

Commit bfcfaf1

Browse files
authored
Merge pull request rust-lang#3308 from topecongiro/issue-2835
Prioritize single_line_fn and empty_item_single_line over brace_style
2 parents efd6fda + 0142e96 commit bfcfaf1

File tree

5 files changed

+53
-51
lines changed

5 files changed

+53
-51
lines changed

src/items.rs

+39-42
Original file line numberDiff line numberDiff line change
@@ -333,19 +333,21 @@ impl<'a> FmtVisitor<'a> {
333333
newline_brace = false;
334334
}
335335

336-
// Prepare for the function body by possibly adding a newline and
337-
// indent.
338-
// FIXME we'll miss anything between the end of the signature and the
339-
// start of the body, but we need more spans from the compiler to solve
340-
// this.
341-
if newline_brace {
342-
result.push_str(&indent.to_string_with_newline(self.config));
336+
if let rw @ Some(..) = self.single_line_fn(&result, block, inner_attrs) {
337+
rw
343338
} else {
344-
result.push(' ');
339+
// Prepare for the function body by possibly adding a newline and
340+
// indent.
341+
// FIXME we'll miss anything between the end of the signature and the
342+
// start of the body, but we need more spans from the compiler to solve
343+
// this.
344+
if newline_brace {
345+
result.push_str(&indent.to_string_with_newline(self.config));
346+
} else {
347+
result.push(' ');
348+
}
349+
Some(result)
345350
}
346-
347-
self.single_line_fn(&result, block, inner_attrs)
348-
.or_else(|| Some(result))
349351
}
350352

351353
pub fn rewrite_required_fn(
@@ -390,42 +392,37 @@ impl<'a> FmtVisitor<'a> {
390392

391393
if self.config.empty_item_single_line()
392394
&& is_empty_block(block, None, source_map)
393-
&& self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width()
395+
&& self.block_indent.width() + fn_str.len() + 3 <= self.config.max_width()
396+
&& !last_line_contains_single_line_comment(fn_str)
394397
{
395-
return Some(format!("{}{{}}", fn_str));
396-
}
397-
398-
if self.config.fn_single_line() && is_simple_block_stmt(block, None, source_map) {
399-
let rewrite = {
400-
if let Some(stmt) = block.stmts.first() {
401-
match stmt_expr(stmt) {
402-
Some(e) => {
403-
let suffix = if semicolon_for_expr(&self.get_context(), e) {
404-
";"
405-
} else {
406-
""
407-
};
408-
409-
format_expr(e, ExprType::Statement, &self.get_context(), self.shape())
410-
.map(|s| s + suffix)
411-
.or_else(|| Some(self.snippet(e.span).to_owned()))
412-
}
413-
None => stmt.rewrite(&self.get_context(), self.shape()),
414-
}
398+
return Some(format!("{} {{}}", fn_str));
399+
}
400+
401+
if !self.config.fn_single_line() || !is_simple_block_stmt(block, None, source_map) {
402+
return None;
403+
}
404+
405+
let stmt = block.stmts.first()?;
406+
let res = match stmt_expr(stmt) {
407+
Some(e) => {
408+
let suffix = if semicolon_for_expr(&self.get_context(), e) {
409+
";"
415410
} else {
416-
None
417-
}
418-
};
411+
""
412+
};
419413

420-
if let Some(res) = rewrite {
421-
let width = self.block_indent.width() + fn_str.len() + res.len() + 4;
422-
if !res.contains('\n') && width <= self.config.max_width() {
423-
return Some(format!("{}{{ {} }}", fn_str, res));
424-
}
414+
format_expr(e, ExprType::Statement, &self.get_context(), self.shape())
415+
.map(|s| s + suffix)?
425416
}
426-
}
417+
None => stmt.rewrite(&self.get_context(), self.shape())?,
418+
};
427419

428-
None
420+
let width = self.block_indent.width() + fn_str.len() + res.len() + 5;
421+
if !res.contains('\n') && width <= self.config.max_width() {
422+
Some(format!("{} {{ {} }}", fn_str, res))
423+
} else {
424+
None
425+
}
429426
}
430427

431428
pub fn visit_static(&mut self, static_parts: &StaticParts) {

tests/source/issue-2835.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// rustfmt-fn_single_line: true
3+
4+
fn lorem() -> i32
5+
{
6+
42
7+
}

tests/target/configs/brace_style/item_always_next_line.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,5 @@ where
2121
mod tests
2222
{
2323
#[test]
24-
fn it_works()
25-
{
26-
}
24+
fn it_works() {}
2725
}

tests/target/fn-custom-7.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ fn foo(
3030

3131
trait Test
3232
{
33-
fn foo(a: u8)
34-
{
35-
}
33+
fn foo(a: u8) {}
3634

37-
fn bar(a: u8) -> String
38-
{
39-
}
35+
fn bar(a: u8) -> String {}
4036
}

tests/target/issue-2835.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// rustfmt-fn_single_line: true
3+
4+
fn lorem() -> i32 { 42 }

0 commit comments

Comments
 (0)