]> git.lizzy.rs Git - rust.git/commitdiff
Add trailing_semicolon config option
authortopecongiro <seuchida@gmail.com>
Tue, 11 Jul 2017 13:41:38 +0000 (22:41 +0900)
committertopecongiro <seuchida@gmail.com>
Wed, 12 Jul 2017 05:16:06 +0000 (14:16 +0900)
trailing_semicolon controls whether to add a trailing semicolon after break,
continue and return.

Configurations.md
src/config.rs
src/expr.rs
src/items.rs
src/utils.rs
src/visitor.rs
tests/target/configs-trailing_semicolon-false.rs [new file with mode: 0644]
tests/target/configs-trailing_semicolon-true.rs [new file with mode: 0644]

index 58124b8bc9333a51d129b1d8cc3a9a62e7fb6218..2e1cdc9dccfe24099f7bf979d55ba1efa81e561a 100644 (file)
@@ -1698,6 +1698,27 @@ let Lorem {
 
 See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
 
+## `trailing_semicolon`
+
+Add trailing semicolon after break, continue and return
+
+- **Default value**: `true`
+- **Possible values**: `true`, `false`
+
+#### `true`:
+```rust
+fn foo() -> usize {
+    return 0;
+}
+```
+
+#### `false`:
+```rust
+fn foo() -> usize {
+    return 0
+}
+```
+
 ## `type_punctuation_density`
 
 Determines if `+` or `=` are wrapped in spaces in the punctuation of types
index 2fe6d3ea6b633beca3381222e3c2ac4dba8a0400..1965675a83eeaef3c5f51f18e391f81fac17542b 100644 (file)
@@ -519,6 +519,7 @@ pub fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
     impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
     trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
         "How to handle trailing commas for lists";
+    trailing_semicolon: bool, true, "Add trailing semicolon after break, continue and return";
     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,
index 9adb601c9cb3f532dfe447dc2708554838909f4a..e11625218597fff12cff508f1a61b72395e7fc45 100644 (file)
@@ -906,7 +906,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
         let result = match self.node {
             ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
             ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
-                let suffix = if semicolon_for_stmt(self) { ";" } else { "" };
+                let suffix = if semicolon_for_stmt(context, self) {
+                    ";"
+                } else {
+                    ""
+                };
 
                 format_expr(
                     ex,
index 7e79e0a0b45c2bd68ea25215c7867f9ae0a46455..2f3018a255243d59e6b6e4841e3378f28f8c54a5 100644 (file)
@@ -352,7 +352,11 @@ fn single_line_fn(&self, fn_str: &str, block: &ast::Block) -> Option<String> {
                 if let Some(ref stmt) = block.stmts.first() {
                     match stmt_expr(stmt) {
                         Some(e) => {
-                            let suffix = if semicolon_for_expr(e) { ";" } else { "" };
+                            let suffix = if semicolon_for_expr(&self.get_context(), e) {
+                                ";"
+                            } else {
+                                ""
+                            };
 
                             format_expr(
                                 &e,
index 2e0dce52e913e83728e8c70874c5f819c142a8bc..bdf5c2e4b5e4a41148e3f06f413ff3f8fba04792 100644 (file)
@@ -156,21 +156,26 @@ pub fn end_typaram(typaram: &ast::TyParam) -> BytePos {
 }
 
 #[inline]
-pub fn semicolon_for_expr(expr: &ast::Expr) -> bool {
+pub fn semicolon_for_expr(context: &RewriteContext, expr: &ast::Expr) -> bool {
     match expr.node {
-        ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => true,
+        ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => {
+            context.config.trailing_semicolon()
+        }
         _ => false,
     }
 }
 
 #[inline]
-pub fn semicolon_for_stmt(stmt: &ast::Stmt) -> bool {
+pub fn semicolon_for_stmt(context: &RewriteContext, stmt: &ast::Stmt) -> bool {
     match stmt.node {
         ast::StmtKind::Semi(ref expr) => match expr.node {
             ast::ExprKind::While(..) |
             ast::ExprKind::WhileLet(..) |
             ast::ExprKind::Loop(..) |
             ast::ExprKind::ForLoop(..) => false,
+            ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => {
+                context.config.trailing_semicolon()
+            }
             _ => true,
         },
         ast::StmtKind::Expr(..) => false,
index a1258092e8d8544035d3a2f935382d6833cd2d98..3bb533a00fd9c72536557d7a1eaf6c61be007943 100644 (file)
@@ -144,7 +144,7 @@ pub fn visit_block(&mut self, b: &ast::Block) {
 
         if !b.stmts.is_empty() {
             if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
-                if utils::semicolon_for_expr(expr) {
+                if utils::semicolon_for_expr(&self.get_context(), expr) {
                     self.buffer.push_str(";");
                 }
             }
diff --git a/tests/target/configs-trailing_semicolon-false.rs b/tests/target/configs-trailing_semicolon-false.rs
new file mode 100644 (file)
index 0000000..9fa746e
--- /dev/null
@@ -0,0 +1,27 @@
+// rustfmt-trailing_semicolon: false
+
+#![feature(loop_break_value)]
+
+fn main() {
+    'a: loop {
+        break 'a
+    }
+
+    let mut done = false;
+    'b: while !done {
+        done = true;
+        continue 'b
+    }
+
+    let x = loop {
+        break 5
+    };
+
+    let x = 'c: loop {
+        break 'c 5
+    };
+}
+
+fn foo() -> usize {
+    return 0
+}
diff --git a/tests/target/configs-trailing_semicolon-true.rs b/tests/target/configs-trailing_semicolon-true.rs
new file mode 100644 (file)
index 0000000..61b6843
--- /dev/null
@@ -0,0 +1,27 @@
+// rustfmt-trailing_semicolon: true
+
+#![feature(loop_break_value)]
+
+fn main() {
+    'a: loop {
+        break 'a;
+    }
+
+    let mut done = false;
+    'b: while !done {
+        done = true;
+        continue 'b;
+    }
+
+    let x = loop {
+        break 5;
+    };
+
+    let x = 'c: loop {
+        break 'c 5;
+    };
+}
+
+fn foo() -> usize {
+    return 0;
+}