]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #1732 from olson-sean-k/issue-1377
authorSeiichi Uchida <seuchida@gmail.com>
Tue, 20 Jun 2017 03:07:03 +0000 (12:07 +0900)
committerGitHub <noreply@github.com>
Tue, 20 Jun 2017 03:07:03 +0000 (12:07 +0900)
Keep brace on same line as `match` when using `ClosingNextLine`.

src/bin/cargo-fmt.rs
src/expr.rs
src/file_lines.rs
src/items.rs
src/utils.rs
tests/source/chains.rs
tests/source/configs-trailing_comma-never.rs
tests/target/chains.rs
tests/target/configs-trailing_comma-never.rs

index 93348b8299669870b45889105286f7c59838852c..c8202eb7e41bd09a77ec05c6b3f2ca50b2e95025 100644 (file)
@@ -50,6 +50,16 @@ fn execute() -> i32 {
     );
     opts.optflag("", "all", "format all packages (only usable in workspaces)");
 
+    // If there is any invalid argument passed to `cargo fmt`, return without formatting.
+    if let Some(arg) = env::args()
+        .skip(2)
+        .take_while(|a| a != "--")
+        .find(|a| !a.starts_with('-'))
+    {
+        print_usage(&opts, &format!("Invalid argument: `{}`.", arg));
+        return failure;
+    }
+
     let matches = match opts.parse(env::args().skip(1).take_while(|a| a != "--")) {
         Ok(m) => m,
         Err(e) => {
index 3c6e1ad52b960bd3231747c993220782b60383e2..2be7987d7c612e542b23e38298ed02721f9712a2 100644 (file)
@@ -1478,6 +1478,7 @@ fn rewrite_match(
         .codemap
         .span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])), "{");
 
+    let arm_num = arms.len();
     for (i, arm) in arms.iter().enumerate() {
         // Make sure we get the stuff between arms.
         let missed_str = if i == 0 {
@@ -1497,12 +1498,21 @@ fn rewrite_match(
 
         let arm_str = arm.rewrite(&context, arm_shape.with_max_width(context.config));
         if let Some(ref arm_str) = arm_str {
-            result.push_str(arm_str);
+            // Trim the trailing comma if necessary.
+            if i == arm_num - 1 && context.config.trailing_comma() == SeparatorTactic::Never &&
+                arm_str.ends_with(',')
+            {
+                result.push_str(&arm_str[0..arm_str.len() - 1])
+            } else {
+                result.push_str(arm_str)
+            }
         } else {
             // We couldn't format the arm, just reproduce the source.
             let snippet = context.snippet(mk_sp(arm_start_pos(arm), arm_end_pos(arm)));
             result.push_str(&snippet);
-            result.push_str(arm_comma(context.config, &arm.body));
+            if context.config.trailing_comma() != SeparatorTactic::Never {
+                result.push_str(arm_comma(context.config, &arm.body))
+            }
         }
     }
     // BytePos(1) = closing match brace.
index 6fa243dc1868f4d36204c2d8b920eca1e6062e21..dad7d0b061af1be3270d5843c80f1c2cda16fc07 100644 (file)
@@ -31,7 +31,7 @@ fn from(range: &'a LineRange) -> Range {
 }
 
 impl Range {
-    fn new(lo: usize, hi: usize) -> Range {
+    pub fn new(lo: usize, hi: usize) -> Range {
         Range { lo: lo, hi: hi }
     }
 
@@ -117,7 +117,7 @@ pub fn all() -> FileLines {
         FileLines(None)
     }
 
-    fn from_ranges(mut ranges: HashMap<String, Vec<Range>>) -> FileLines {
+    pub fn from_ranges(mut ranges: HashMap<String, Vec<Range>>) -> FileLines {
         normalize_ranges(&mut ranges);
         FileLines(Some(ranges))
     }
index b7c265748381da84a5e00f4cd3cf3f04e5619406..9adecb9be946db34d2b64022e1705e6b04a694b8 100644 (file)
@@ -2267,12 +2267,12 @@ enum ArgumentKind<'a> {
         IndentStyle::Block => {
             (
                 indent.block_indent(context.config),
-                SeparatorTactic::Vertical,
+                context.config.trailing_comma(),
                 true,
             )
         }
         IndentStyle::Visual if last_line_ends_with_comment => {
-            (arg_indent, SeparatorTactic::Vertical, true)
+            (arg_indent, context.config.trailing_comma(), true)
         }
         IndentStyle::Visual => (arg_indent, SeparatorTactic::Never, false),
     };
@@ -2564,7 +2564,7 @@ fn rewrite_where_clause_rfc_style(
     let comma_tactic = if suppress_comma {
         SeparatorTactic::Never
     } else {
-        SeparatorTactic::Always
+        context.config.trailing_comma()
     };
 
     let fmt = ListFormatting {
index 1be83d01babacad7726227d67aad20095ae1afb5..dc5dee6ba0d9b5501e6c99353eaf2621f77ffd57 100644 (file)
@@ -111,9 +111,10 @@ pub fn trimmed_last_line_width(s: &str) -> usize {
 #[inline]
 pub fn last_line_extendable(s: &str) -> bool {
     s.lines().last().map_or(false, |s| {
-        s.trim()
-            .chars()
-            .all(|c| c == ')' || c == ']' || c == '}' || c == '?')
+        s.ends_with("\"#") ||
+            s.trim()
+                .chars()
+                .all(|c| c == ')' || c == ']' || c == '}' || c == '?')
     })
 }
 
index 20d320ccde89d3830d37eae10186dee6fbff063b..99fe6176aa8483bbb8ef421b620434c43f576ddf 100644 (file)
@@ -152,3 +152,14 @@ fn issue_1004() {
             })
             ?;
 }
+
+fn issue1392() {
+    test_method(r#"
+        if foo {
+            a();
+        }
+        else {
+            b();
+        }
+        "#.trim());
+}
index 0577f2e5affa6d4d4dabeb55ef7a44fbb52aff16..4da3b996f2994fdf3f7020e5b704f6a433108e84 100644 (file)
@@ -10,4 +10,14 @@ fn main() {
         let _ = safe_assert_eq!(reply_req_num, request_num, op);
         return Ok((request_num, op, value));
     }
+
+    // #1710
+    pub struct FileInput {
+        input: StringInput,
+        file_name: OsString,
+    }
+    match len {
+        Some(len) => Ok(new(self.input, self.pos + len)),
+        None => Err(self),
+    }
 }
index 52e65b420e718af5f0dbb9797876763f1b20dc1b..d930f0a49badc891f00e8704bf3d0741d89e46d8 100644 (file)
@@ -169,3 +169,16 @@ fn issue_1004() {
         in_binder(f, tcx, &ty::Binder(""), Some(tap))
     })?;
 }
+
+fn issue1392() {
+    test_method(
+        r#"
+        if foo {
+            a();
+        }
+        else {
+            b();
+        }
+        "#.trim(),
+    );
+}
index 8f351e8dfc2e0c8c85981f1e651cee2edc32a83f..ae0e50f96d18c99ae85e1d6397de9c1c4997edcc 100644 (file)
@@ -22,4 +22,14 @@ fn main() {
         let _ = safe_assert_eq!(reply_req_num, request_num, op);
         return Ok((request_num, op, value));
     }
+
+    // #1710
+    pub struct FileInput {
+        input: StringInput,
+        file_name: OsString
+    }
+    match len {
+        Some(len) => Ok(new(self.input, self.pos + len)),
+        None => Err(self)
+    }
 }