From decafbbaea1f2131f2c414dfb21da594b43464cd Mon Sep 17 00:00:00 2001
From: Nick Cameron <ncameron@mozilla.com>
Date: Wed, 29 Apr 2015 09:53:33 +1200
Subject: [PATCH] Fix trailing commas in where clauses

---
 src/functions.rs          |  6 ++----
 src/mod.rs                |  4 +---
 tests/idem/attrib.rs      |  7 +++++++
 tests/idem/comments-fn.rs |  2 +-
 tests/idem/fn.rs          | 42 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 53 insertions(+), 8 deletions(-)
 create mode 100644 tests/idem/fn.rs

diff --git a/src/functions.rs b/src/functions.rs
index 039e7efd085..a753f38d9f7 100644
--- a/src/functions.rs
+++ b/src/functions.rs
@@ -88,9 +88,6 @@ impl<'a> FmtVisitor<'a> {
                result.len() + indent + ret_str.len() > MAX_WIDTH {
                 let indent = match FN_RETURN_INDENT {
                     ReturnIndent::WithWhereClause => indent + 4,
-                    ReturnIndent::WithWhereClauseOrArgs if where_clause.predicates.len() > 0 => {
-                        indent + 4
-                    }
                     // TODO we might want to check that using the arg indent doesn't
                     // blow our budget, and if it does, then fallback to the where
                     // clause indent.
@@ -254,7 +251,8 @@ impl<'a> FmtVisitor<'a> {
         // The fix is comments in the AST or a span for the closing paren.
         let snippet = self.snippet(codemap::mk_sp(prev_end, next_span_start));
         let snippet = snippet.trim();
-        let snippet = &snippet[..snippet.find(terminator).unwrap_or(snippet.len())];
+        let snippet = &snippet[..snippet.find(terminator)
+                                    .unwrap_or(snippet.find(separator).unwrap_or(snippet.len()))];
         let snippet = snippet.trim();
         result.push(snippet.to_string());
 
diff --git a/src/mod.rs b/src/mod.rs
index 8d360ab3376..7b18664b672 100644
--- a/src/mod.rs
+++ b/src/mod.rs
@@ -68,7 +68,7 @@ const MAX_WIDTH: usize = 100;
 const MIN_STRING: usize = 10;
 const TAB_SPACES: usize = 4;
 const FN_BRACE_STYLE: BraceStyle = BraceStyle::SameLineWhere;
-const FN_RETURN_INDENT: ReturnIndent = ReturnIndent::WithWhereClauseOrArgs;
+const FN_RETURN_INDENT: ReturnIndent = ReturnIndent::WithArgs;
 // When we get scoped annotations, we should have rustfmt::skip.
 const SKIP_ANNOTATION: &'static str = "rustfmt_skip";
 
@@ -99,8 +99,6 @@ enum ReturnIndent {
     WithArgs,
     // Aligned with the where clause
     WithWhereClause,
-    // Aligned with the where clause if there is one, otherwise the args.
-    WithWhereClauseOrArgs,
 }
 
 // Formatting which depends on the AST.
diff --git a/tests/idem/attrib.rs b/tests/idem/attrib.rs
index b66be08c728..aa2b1a69df0 100644
--- a/tests/idem/attrib.rs
+++ b/tests/idem/attrib.rs
@@ -1,5 +1,12 @@
 // Test attributes and doc comments are preserved.
 
+extern crate Foo;
+#[Attr1]
+extern crate Bar;
+#[Attr2]
+#[Attr2]
+extern crate Baz;
+
 /// Blah blah blah.
 impl Bar {
     /// Blah blah blooo.
diff --git a/tests/idem/comments-fn.rs b/tests/idem/comments-fn.rs
index f9f7bb27c07..12c0e72193f 100644
--- a/tests/idem/comments-fn.rs
+++ b/tests/idem/comments-fn.rs
@@ -9,7 +9,7 @@ fn foo<F, G>(a: aaaaaaaaaaaaa, // A comment
              //  A multi line comment
              // between args.
              e: eeeeeeeeeeeee /* comment before paren*/)
-    -> bar
+             -> bar
     where F: Foo, // COmment after where clause
           G: Goo /* final comment */
 {
diff --git a/tests/idem/fn.rs b/tests/idem/fn.rs
new file mode 100644
index 00000000000..e5013a61cee
--- /dev/null
+++ b/tests/idem/fn.rs
@@ -0,0 +1,42 @@
+// Tests different fns
+
+fn foo(a: AAAA, b: BBB, c: CCC) -> RetType {
+
+}
+
+fn foo(a: AAAA, b: BBB, c: CCC) -> RetType
+    where T: Blah
+{
+
+}
+
+fn foo(a: AAA)
+    where T: Blah
+{
+
+}
+
+fn foo(a: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
+       b: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB)
+       -> RetType
+    where T: Blah
+{
+
+}
+
+
+fn foo<U, T>(a: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
+             b: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB)
+             -> RetType
+    where T: Blah,
+          U: dsfasdfasdfasd
+{
+
+}
+
+impl Foo {
+    fn with_no_errors<T, F>(&mut self, f: F) -> T
+        where F: FnOnce(&mut Resolver) -> T
+    {
+    }
+}