From 15ec5b291292f87697b32f30cc31f863538959a2 Mon Sep 17 00:00:00 2001 From: Kevin Yeh Date: Thu, 19 Nov 2015 20:11:32 -0600 Subject: [PATCH] Fix empty body format, add fn_empty_single_line option, refactor block tests --- src/config.rs | 1 + src/expr.rs | 29 ++++++++------------------ src/items.rs | 17 +++++++-------- src/visitor.rs | 15 +++++++------ tests/target/attrib.rs | 6 +++--- tests/target/comment.rs | 2 +- tests/target/comments-fn.rs | 4 ++-- tests/target/fn-simple.rs | 4 ++-- tests/target/fn-single-line.rs | 6 +++--- tests/target/fn.rs | 12 +++++------ tests/target/multiple.rs | 6 +++--- tests/target/nestedmod/mod2c.rs | 2 +- tests/target/nestedmod/mymod1/mod3a.rs | 2 +- tests/target/nestedmod/submod2/a.rs | 2 +- tests/target/no_new_line_beginning.rs | 2 +- tests/target/paths.rs | 2 +- 16 files changed, 50 insertions(+), 62 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7d56e3e8a80..295c1076b5f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -269,6 +269,7 @@ create_config! { newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings"; fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions"; item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums"; + fn_empty_single_line: bool, true, "Put empty-body functions on a single line"; fn_single_line: bool, false, "Put single-expression functions on a single line"; fn_return_indent: ReturnIndent, ReturnIndent::WithArgs, "Location of return type in function declaration"; diff --git a/src/expr.rs b/src/expr.rs index 5ec12876c4b..b494cb08fcc 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -705,36 +705,25 @@ fn single_line_if_else(context: &RewriteContext, None } -// Checks that a block contains no statements, an expression and no comments. -fn is_simple_block(block: &ast::Block, codemap: &CodeMap) -> bool { - if !block.stmts.is_empty() || block.expr.is_none() { - return false; - } - +fn block_contains_comment(block: &ast::Block, codemap: &CodeMap) -> bool { let snippet = codemap.span_to_snippet(block.span).unwrap(); + contains_comment(&snippet) +} - !contains_comment(&snippet) +// Checks that a block contains no statements, an expression and no comments. +pub fn is_simple_block(block: &ast::Block, codemap: &CodeMap) -> bool { + block.stmts.is_empty() && block.expr.is_some() && !block_contains_comment(block, codemap) } /// Checks whether a block contains at most one statement or expression, and no comments. pub fn is_simple_block_stmt(block: &ast::Block, codemap: &CodeMap) -> bool { - if (!block.stmts.is_empty() && block.expr.is_some()) || - (block.stmts.len() != 1 && block.expr.is_none()) { - return false; - } - - let snippet = codemap.span_to_snippet(block.span).unwrap(); - !contains_comment(&snippet) + (block.stmts.is_empty() || (block.stmts.len() == 1 && block.expr.is_none())) && + !block_contains_comment(block, codemap) } /// Checks whether a block contains no statements, expressions, or comments. pub fn is_empty_block(block: &ast::Block, codemap: &CodeMap) -> bool { - if !block.stmts.is_empty() || block.expr.is_some() { - return false; - } - - let snippet = codemap.span_to_snippet(block.span).unwrap(); - !contains_comment(&snippet) + block.stmts.is_empty() && block.expr.is_none() && !block_contains_comment(block, codemap) } // inter-match-arm-comment-rules: diff --git a/src/items.rs b/src/items.rs index eac01242737..01418a08a9a 100644 --- a/src/items.rs +++ b/src/items.rs @@ -448,20 +448,19 @@ impl<'a> FmtVisitor<'a> { } pub fn rewrite_single_line_fn(&self, - fn_rewrite: &Option, + fn_str: &str, block: &ast::Block) -> Option { - let fn_str = match *fn_rewrite { - Some(ref s) if !s.contains('\n') => s, - _ => return None, - }; + if fn_str.contains('\n') { + return None; + } let codemap = self.get_context().codemap; - if is_empty_block(block, codemap) && - self.block_indent.width() + fn_str.len() + 3 <= self.config.max_width { - return Some(format!("{}{{ }}", fn_str)); + if self.config.fn_empty_single_line && is_empty_block(block, codemap) && + self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width { + return Some(format!("{}{{}}", fn_str)); } if self.config.fn_single_line && is_simple_block_stmt(block, codemap) { @@ -488,7 +487,7 @@ impl<'a> FmtVisitor<'a> { }; if let Some(res) = rewrite { - let width = self.block_indent.width() + fn_str.len() + res.len() + 3; + 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)); } diff --git a/src/visitor.rs b/src/visitor.rs index 6b98fd276b6..b51274b4d8d 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -154,16 +154,15 @@ impl<'a> FmtVisitor<'a> { visit::FnKind::Closure => None, }; - if let Some(ref single_line_fn) = self.rewrite_single_line_fn(&rewrite, &b) { - self.format_missing_with_indent(s.lo); - self.buffer.push_str(single_line_fn); - self.last_pos = b.span.hi; - return; - } - if let Some(fn_str) = rewrite { self.format_missing_with_indent(s.lo); - self.buffer.push_str(&fn_str); + if let Some(ref single_line_fn) = self.rewrite_single_line_fn(&fn_str, &b) { + self.buffer.push_str(single_line_fn); + self.last_pos = b.span.hi; + return; + } else { + self.buffer.push_str(&fn_str); + } } else { self.format_missing(b.span.lo); } diff --git a/tests/target/attrib.rs b/tests/target/attrib.rs index 8b793c9a925..9317a837065 100644 --- a/tests/target/attrib.rs +++ b/tests/target/attrib.rs @@ -13,7 +13,7 @@ impl Bar { /// Blah blah blooo. /// Blah blah blooo. #[an_attribute] - fn foo(&mut self) -> isize { } + fn foo(&mut self) -> isize {} /// Blah blah bing. /// Blah blah bing. @@ -27,7 +27,7 @@ impl Bar { } #[another_attribute] - fn f3(self) -> Dog { } + fn f3(self) -> Dog {} /// Blah blah bing. #[attrib1] @@ -36,5 +36,5 @@ impl Bar { // Another comment that needs rewrite because it's tooooooooooooooooooooooooooooooo // loooooooooooong. /// Blah blah bing. - fn f4(self) -> Cat { } + fn f4(self) -> Cat {} } diff --git a/tests/target/comment.rs b/tests/target/comment.rs index 04053bd804a..2d90d83edb6 100644 --- a/tests/target/comment.rs +++ b/tests/target/comment.rs @@ -32,7 +32,7 @@ fn test() { } /// test123 -fn doc_comment() { } +fn doc_comment() {} fn chains() { foo.bar(|| { diff --git a/tests/target/comments-fn.rs b/tests/target/comments-fn.rs index c96489b0817..fa607e131ea 100644 --- a/tests/target/comments-fn.rs +++ b/tests/target/comments-fn.rs @@ -16,6 +16,6 @@ fn foo(a: aaaaaaaaaaaaa, // A comment } -fn bar() { } +fn bar() {} -fn baz() -> Baz /* Comment after return type */ { } +fn baz() -> Baz /* Comment after return type */ {} diff --git a/tests/target/fn-simple.rs b/tests/target/fn-simple.rs index 0f46b6d1318..9db1c8831b6 100644 --- a/tests/target/fn-simple.rs +++ b/tests/target/fn-simple.rs @@ -28,13 +28,13 @@ fn generic(arg: T) -> &SomeType arg(a, b, c, d, e) } -fn foo() -> ! { } +fn foo() -> ! {} pub fn http_fetch_async(listener: Box, script_chan: Box) { } -fn some_func>(val: T) { } +fn some_func>(val: T) {} fn zzzzzzzzzzzzzzzzzzzz(selff: Type, mut handle: node::Handle>, diff --git a/tests/target/fn-single-line.rs b/tests/target/fn-single-line.rs index 57c926e83e4..674ce1c89f9 100644 --- a/tests/target/fn-single-line.rs +++ b/tests/target/fn-single-line.rs @@ -9,7 +9,7 @@ fn foo_decl_local() { let z = 5; } fn foo_decl_item(x: &mut i32) { x = 3; } -fn empty() { } +fn empty() {} fn foo_return() -> String { "yay" } @@ -55,9 +55,9 @@ fn lots_of_space() { 1 } fn mac() -> Vec { vec![] } trait CoolTypes { - fn dummy(&self) { } + fn dummy(&self) {} } trait CoolerTypes { - fn dummy(&self) { } + fn dummy(&self) {} } diff --git a/tests/target/fn.rs b/tests/target/fn.rs index 97e1172470f..0ae9fd7ef1f 100644 --- a/tests/target/fn.rs +++ b/tests/target/fn.rs @@ -1,6 +1,6 @@ // Tests different fns -fn foo(a: AAAA, b: BBB, c: CCC) -> RetType { } +fn foo(a: AAAA, b: BBB, c: CCC) -> RetType {} fn foo(a: AAAA, b: BBB /* some, weird, inline comment */, c: CCC) -> RetType where T: Blah @@ -32,7 +32,7 @@ fn foo(a: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, } -fn foo B /* paren inside generics */>() { } +fn foo B /* paren inside generics */>() {} impl Foo { fn with_no_errors(&mut self, f: F) -> T @@ -40,9 +40,9 @@ impl Foo { { } - fn foo(mut self, mut bar: u32) { } + fn foo(mut self, mut bar: u32) {} - fn bar(self, mut bazz: u32) { } + fn bar(self, mut bazz: u32) {} } pub fn render<'a, @@ -70,9 +70,9 @@ impl Foo { } } -fn homura>(_: T) { } +fn homura>(_: T) {} -fn issue377() -> (Box, Box) { } +fn issue377() -> (Box, Box) {} fn main() { let _ = function(move || 5); diff --git a/tests/target/multiple.rs b/tests/target/multiple.rs index f39a7bcd53d..7b371409fba 100644 --- a/tests/target/multiple.rs +++ b/tests/target/multiple.rs @@ -26,7 +26,7 @@ mod other; // sfdgfffffffffffffffffffffffffffffffffffffffffffffffffffffff // ffffffffffffffffffffffffffffffffffffffffff -fn foo(a: isize, b: u32 /* blah blah */, c: f64) { } +fn foo(a: isize, b: u32 /* blah blah */, c: f64) {} fn foo() -> Box where 'a: 'b, @@ -75,7 +75,7 @@ impl Bar { } #[an_attribute] - fn f3(self) -> Dog { } + fn f3(self) -> Dog {} } /// The `nodes` and `edges` method each return instantiations of @@ -115,7 +115,7 @@ pub struct Foo<'a, Y: Baz> f: SomeType, // Comment beside a field } -fn foo(ann: &'a (PpAnn + 'a)) { } +fn foo(ann: &'a (PpAnn + 'a)) {} fn main() { for i in 0i32..4 { diff --git a/tests/target/nestedmod/mod2c.rs b/tests/target/nestedmod/mod2c.rs index 0a03d2b3994..7db4572e777 100644 --- a/tests/target/nestedmod/mod2c.rs +++ b/tests/target/nestedmod/mod2c.rs @@ -1,3 +1,3 @@ // A standard mod -fn a() { } +fn a() {} diff --git a/tests/target/nestedmod/mymod1/mod3a.rs b/tests/target/nestedmod/mymod1/mod3a.rs index febe1ff7b25..ae09d8ddac0 100644 --- a/tests/target/nestedmod/mymod1/mod3a.rs +++ b/tests/target/nestedmod/mymod1/mod3a.rs @@ -1,2 +1,2 @@ // Another mod -fn a() { } +fn a() {} diff --git a/tests/target/nestedmod/submod2/a.rs b/tests/target/nestedmod/submod2/a.rs index 53540b8b659..120b17145e3 100644 --- a/tests/target/nestedmod/submod2/a.rs +++ b/tests/target/nestedmod/submod2/a.rs @@ -3,4 +3,4 @@ use c::a; -fn foo() { } +fn foo() {} diff --git a/tests/target/no_new_line_beginning.rs b/tests/target/no_new_line_beginning.rs index 45590d86ba6..f328e4d9d04 100644 --- a/tests/target/no_new_line_beginning.rs +++ b/tests/target/no_new_line_beginning.rs @@ -1 +1 @@ -fn main() { } +fn main() {} diff --git a/tests/target/paths.rs b/tests/target/paths.rs index 303a4df4a9d..f1b142b3a5c 100644 --- a/tests/target/paths.rs +++ b/tests/target/paths.rs @@ -19,4 +19,4 @@ fn main() { let x: Foo; } -fn op(foo: Bar, key: &[u8], upd: Fn(Option<&memcache::Item>, Baz) -> Result) -> MapResult { } +fn op(foo: Bar, key: &[u8], upd: Fn(Option<&memcache::Item>, Baz) -> Result) -> MapResult {}