]> git.lizzy.rs Git - rust.git/commitdiff
Fix empty body format, add fn_empty_single_line option, refactor block tests
authorKevin Yeh <kevinyeah@utexas.edu>
Fri, 20 Nov 2015 02:11:32 +0000 (20:11 -0600)
committerKevin Yeh <kevinyeah@utexas.edu>
Fri, 20 Nov 2015 02:45:02 +0000 (20:45 -0600)
16 files changed:
src/config.rs
src/expr.rs
src/items.rs
src/visitor.rs
tests/target/attrib.rs
tests/target/comment.rs
tests/target/comments-fn.rs
tests/target/fn-simple.rs
tests/target/fn-single-line.rs
tests/target/fn.rs
tests/target/multiple.rs
tests/target/nestedmod/mod2c.rs
tests/target/nestedmod/mymod1/mod3a.rs
tests/target/nestedmod/submod2/a.rs
tests/target/no_new_line_beginning.rs
tests/target/paths.rs

index 7d56e3e8a80029c15c10711e8b17273206a0a962..295c1076b5fcb7c87391b242dfd9d155f26bf84f 100644 (file)
@@ -269,6 +269,7 @@ fn default() -> 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";
index 5ec12876c4bbcf332084b7f4db0f073608a5febd..b494cb08fccabb57fc0fc7dcd4d6005b00d3ee5f 100644 (file)
@@ -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:
index eac01242737e4c045afa77fcd616d4646af38c40..01418a08a9ac15cf5b6bd5c43fba1c79e03c3188 100644 (file)
@@ -448,20 +448,19 @@ fn rewrite_fn_base(&mut self,
     }
 
     pub fn rewrite_single_line_fn(&self,
-                                  fn_rewrite: &Option<String>,
+                                  fn_str: &str,
                                   block: &ast::Block)
                                   -> Option<String> {
 
-        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 @@ pub fn rewrite_single_line_fn(&self,
             };
 
             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));
                 }
index 6b98fd276b61debb30a68b166d84a374aa0ca96b..b51274b4d8d3590be24f7f9598cc78c5fac6afc0 100644 (file)
@@ -154,16 +154,15 @@ fn visit_fn(&mut self,
             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);
         }
index 8b793c9a9255db3d99939d7450525803354d04be..9317a83706571ad4f12fb86a9afea19ac6cc8dd3 100644 (file)
@@ -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 @@ pub fn f2(self) {
     }
 
     #[another_attribute]
-    fn f3(self) -> Dog { }
+    fn f3(self) -> Dog {}
 
     /// Blah blah bing.
     #[attrib1]
@@ -36,5 +36,5 @@ fn f3(self) -> Dog { }
     // Another comment that needs rewrite because it's tooooooooooooooooooooooooooooooo
     // loooooooooooong.
     /// Blah blah bing.
-    fn f4(self) -> Cat { }
+    fn f4(self) -> Cat {}
 }
index 04053bd804a68df5e0c27bb37d5e5abdb80dcca5..2d90d83edb69b65d0e323d3093e4c1951bc697b8 100644 (file)
@@ -32,7 +32,7 @@ fn test() {
 }
 
 /// test123
-fn doc_comment() { }
+fn doc_comment() {}
 
 fn chains() {
     foo.bar(|| {
index c96489b08173f523970b49edcb21914a71b08c19..fa607e131eaa5cdb0e5d9e0205cfad01f3045e61 100644 (file)
@@ -16,6 +16,6 @@ fn foo<F, G>(a: aaaaaaaaaaaaa, // A comment
 
 }
 
-fn bar<F /* comment on F */, G /* comment on G */>() { }
+fn bar<F /* comment on F */, G /* comment on G */>() {}
 
-fn baz() -> Baz /* Comment after return type */ { }
+fn baz() -> Baz /* Comment after return type */ {}
index 0f46b6d1318955fc9702b5e6a525040986cb2570..9db1c8831b60c365c145c7812d8883e1491acf76 100644 (file)
@@ -28,13 +28,13 @@ fn generic<T>(arg: T) -> &SomeType
     arg(a, b, c, d, e)
 }
 
-fn foo() -> ! { }
+fn foo() -> ! {}
 
 pub fn http_fetch_async(listener: Box<AsyncCORSResponseListener + Send>,
                         script_chan: Box<ScriptChan + Send>) {
 }
 
-fn some_func<T: Box<Trait + Bound>>(val: T) { }
+fn some_func<T: Box<Trait + Bound>>(val: T) {}
 
 fn zzzzzzzzzzzzzzzzzzzz<Type, NodeType>(selff: Type,
                                         mut handle: node::Handle<IdRef<'id, Node<K, V>>,
index 57c926e83e4025d6d4fb68bb60bd7e304c3306ca..674ce1c89f9b0d164a39648004d37f4c3bc7ae70 100644 (file)
@@ -9,7 +9,7 @@ fn foo_expr() { 1 }
 
 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<i32> { vec![] }
 
 trait CoolTypes {
-    fn dummy(&self) { }
+    fn dummy(&self) {}
 }
 
 trait CoolerTypes {
-    fn dummy(&self) { }
+    fn dummy(&self) {}
 }
index 97e1172470f8a97c81f130073018e1194995d3bf..0ae9fd7ef1fc871aa5166a9c25fbda2137acd2fd 100644 (file)
@@ -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<U, T>(a: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
 
 }
 
-fn foo<U: Fn(A) -> B /* paren inside generics */>() { }
+fn foo<U: Fn(A) -> B /* paren inside generics */>() {}
 
 impl Foo {
     fn with_no_errors<T, F>(&mut self, f: F) -> T
@@ -40,9 +40,9 @@ fn with_no_errors<T, F>(&mut self, f: F) -> T
     {
     }
 
-    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 @@ const fn foo() {
     }
 }
 
-fn homura<T: Deref<Target = i32>>(_: T) { }
+fn homura<T: Deref<Target = i32>>(_: T) {}
 
-fn issue377() -> (Box<CompositorProxy + Send>, Box<CompositorReceiver>) { }
+fn issue377() -> (Box<CompositorProxy + Send>, Box<CompositorReceiver>) {}
 
 fn main() {
     let _ = function(move || 5);
index f39a7bcd53da1515e556d657791f4bcb2541958e..7b371409fba96c33557ba633d6bb99d808f1c8b7 100644 (file)
@@ -26,7 +26,7 @@
 // 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<Write + 'static>
     where 'a: 'b,
@@ -75,7 +75,7 @@ pub fn f2(self) {
     }
 
     #[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 {
index 0a03d2b39946cd29498534a2ef5ddb697ce5dc9d..7db4572e777c0010537f78b12a52687a36032383 100644 (file)
@@ -1,3 +1,3 @@
 // A standard mod
 
-fn a() { }
+fn a() {}
index febe1ff7b2527eacf21f3e4960658ddd85ce313e..ae09d8ddac0d14b172fa37ecf5cf444f1558a2f5 100644 (file)
@@ -1,2 +1,2 @@
 // Another mod
-fn a() { }
+fn a() {}
index 53540b8b6590e168e4e72ec4803f16600dc343e2..120b17145e3a0879c0bb973e8d3a4fffb4f788e1 100644 (file)
@@ -3,4 +3,4 @@
 
 use c::a;
 
-fn foo() { }
+fn foo() {}
index 45590d86ba6c51f0babffb0b43e3e2f44d2f9e07..f328e4d9d04c31d0d70d16d21a07d1613be9d577 100644 (file)
@@ -1 +1 @@
-fn main() { }
+fn main() {}
index 303a4df4a9d676f7748c1410b51c14101f1c3492..f1b142b3a5c46096e6d8d2096e17190cdd0e5ae8 100644 (file)
@@ -19,4 +19,4 @@ fn main() {
     let x: Foo<A>;
 }
 
-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 {}