]> git.lizzy.rs Git - rust.git/commitdiff
Add multiline_{closure,match_arm}_forces_block
authorMichael Smith <michael@spinda.net>
Fri, 18 Aug 2017 23:50:39 +0000 (16:50 -0700)
committerMichael Smith <michael@spinda.net>
Mon, 21 Aug 2017 21:05:20 +0000 (14:05 -0700)
multiline_closure_forces_block = false (default):
    result.and_then(|maybe_value| match maybe_value {
        None => ...,
        Some(value) => ...,
    })

multiline_closure_forces_block = true:
    result.and_then(|maybe_value| {
        match maybe_value {
            None => ...,
            Some(value) => ...,
        }
    })

multiline_match_arm_forces_block = false (default):
    match lorem {
        None => if ipsum {
            println!("Hello World");
        },
        Some(dolor) => ...,
    }

multiline_match_arm_forces_block = true:
    match lorem {
        None => {
            if ipsum {
                println!("Hello World");
            }
        }
        Some(dolor) => ...,
    }

Configurations.md
src/config.rs
src/expr.rs
tests/source/configs-multiline_closure_forces_block-false.rs [new file with mode: 0644]
tests/source/configs-multiline_closure_forces_block-true.rs [new file with mode: 0644]
tests/source/configs-multiline_match_arm_forces_block-false.rs [new file with mode: 0644]
tests/source/configs-multiline_match_arm_forces_block-true.rs [new file with mode: 0644]
tests/target/configs-multiline_closure_forces_block-false.rs [new file with mode: 0644]
tests/target/configs-multiline_closure_forces_block-true.rs [new file with mode: 0644]
tests/target/configs-multiline_match_arm_forces_block-false.rs [new file with mode: 0644]
tests/target/configs-multiline_match_arm_forces_block-true.rs [new file with mode: 0644]

index 27c1c47280c558a654cf9342bce993820f274721..13a704240c377afa89c6a2eff0475867592c8ece 100644 (file)
@@ -1265,6 +1265,64 @@ Maximum width of each line
 
 See also [`error_on_line_overflow`](#error_on_line_overflow).
 
+## `multiline_closure_forces_block`
+
+Force multiline closure bodies to be wrapped in a block
+
+- **Default value**: `false`
+- **Possible values**: `false`, `true`
+
+#### `false`:
+
+```rust
+result.and_then(|maybe_value| match maybe_value {
+    None => ...,
+    Some(value) => ...,
+})
+```
+
+#### `true`:
+
+```rust
+result.and_then(|maybe_value| {
+    match maybe_value {
+        None => ...,
+        Some(value) => ...,
+    }
+})
+```
+
+## `multiline_match_arm_forces_block`
+
+Force multiline match arm bodies to be wrapped in a block
+
+- **Default value**: `false`
+- **Possible values**: `false`, `true`
+
+#### `false`:
+
+```rust
+match lorem {
+    None => if ipsum {
+        println!("Hello World");
+    },
+    Some(dolor) => ...,
+}
+```
+
+#### `true`:
+
+```rust
+match lorem {
+    None => {
+        if ipsum {
+            println!("Hello World");
+        }
+    }
+    Some(dolor) => ...,
+}
+```
+
 ## `newline_style`
 
 Unix or Windows line endings
index 02fd111fd9aa6e1814d05116bd7ce47a95c3b006..9659f19377d06227983f8df20608769551656321 100644 (file)
@@ -615,6 +615,10 @@ pub fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
         "Try to put attributes on the same line as fields.";
     attributes_on_same_line_as_variant: bool, true,
         "Try to put attributes on the same line as variants in enum declarations.";
+    multiline_closure_forces_block: bool, false,
+        "Force multiline closure bodies to be wrapped in a block";
+    multiline_match_arm_forces_block: bool, false,
+        "Force multiline match arm bodies to be wrapped in a block";
 }
 
 #[cfg(test)]
index 5e8c66663c45a96ac290b23631f416240b15f190..a42ab973e0d25ac3a891cb5dae35d35a6bb1b9ff 100644 (file)
@@ -690,6 +690,13 @@ fn rewrite_closure_expr(
     if classify::expr_requires_semi_to_be_stmt(left_most_sub_expr(expr)) {
         rewrite = and_one_line(rewrite);
     }
+    rewrite = rewrite.and_then(|rw| {
+        if context.config.multiline_closure_forces_block() && rw.contains('\n') {
+            None
+        } else {
+            Some(rw)
+        }
+    });
     rewrite.map(|rw| format!("{} {}", prefix, rw))
 }
 
@@ -1690,12 +1697,20 @@ fn flatten_arm_body<'a>(context: &'a RewriteContext, body: &'a ast::Expr) -> (bo
             if !is_unsafe_block(block) && is_simple_block(block, context.codemap) =>
         {
             if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
-                (expr.can_be_overflowed(context, 1), &**expr)
+                (
+                    !context.config.multiline_match_arm_forces_block() &&
+                        expr.can_be_overflowed(context, 1),
+                    &**expr,
+                )
             } else {
                 (false, &*body)
             }
         }
-        _ => (body.can_be_overflowed(context, 1), &*body),
+        _ => (
+            !context.config.multiline_match_arm_forces_block() &&
+                body.can_be_overflowed(context, 1),
+            &*body,
+        ),
     }
 }
 
diff --git a/tests/source/configs-multiline_closure_forces_block-false.rs b/tests/source/configs-multiline_closure_forces_block-false.rs
new file mode 100644 (file)
index 0000000..e885dff
--- /dev/null
@@ -0,0 +1,11 @@
+// rustfmt-multiline_closure_forces_block: false
+// Option forces multiline closure bodies to be wrapped in a block
+
+fn main() {
+    result.and_then(|maybe_value| {
+        match maybe_value {
+            None => Err("oops"),
+            Some(value) => Ok(1),
+        }
+    });
+}
diff --git a/tests/source/configs-multiline_closure_forces_block-true.rs b/tests/source/configs-multiline_closure_forces_block-true.rs
new file mode 100644 (file)
index 0000000..f267466
--- /dev/null
@@ -0,0 +1,9 @@
+// rustfmt-multiline_closure_forces_block: true
+// Option forces multiline closure bodies to be wrapped in a block
+
+fn main() {
+    result.and_then(|maybe_value| match maybe_value {
+        None => Err("oops"),
+        Some(value) => Ok(1),
+    });
+}
diff --git a/tests/source/configs-multiline_match_arm_forces_block-false.rs b/tests/source/configs-multiline_match_arm_forces_block-false.rs
new file mode 100644 (file)
index 0000000..4cbec0c
--- /dev/null
@@ -0,0 +1,13 @@
+// rustfmt-multiline_match_arm_forces_block: false
+// Option forces multiline match arm bodies to be wrapped in a block
+
+fn main() {
+    match lorem {
+        Lorem::Ipsum => {
+            if ipsum {
+                println!("dolor");
+            }
+        }
+        Lorem::Dolor => println!("amet"),
+    }
+}
diff --git a/tests/source/configs-multiline_match_arm_forces_block-true.rs b/tests/source/configs-multiline_match_arm_forces_block-true.rs
new file mode 100644 (file)
index 0000000..602076a
--- /dev/null
@@ -0,0 +1,11 @@
+// rustfmt-multiline_match_arm_forces_block: true
+// Option forces multiline match arm bodies to be wrapped in a block
+
+fn main() {
+    match lorem {
+        Lorem::Ipsum => if ipsum {
+            println!("dolor");
+        },
+        Lorem::Dolor => println!("amet"),
+    }
+}
diff --git a/tests/target/configs-multiline_closure_forces_block-false.rs b/tests/target/configs-multiline_closure_forces_block-false.rs
new file mode 100644 (file)
index 0000000..7fb3d59
--- /dev/null
@@ -0,0 +1,9 @@
+// rustfmt-multiline_closure_forces_block: false
+// Option forces multiline closure bodies to be wrapped in a block
+
+fn main() {
+    result.and_then(|maybe_value| match maybe_value {
+        None => Err("oops"),
+        Some(value) => Ok(1),
+    });
+}
diff --git a/tests/target/configs-multiline_closure_forces_block-true.rs b/tests/target/configs-multiline_closure_forces_block-true.rs
new file mode 100644 (file)
index 0000000..01e2de4
--- /dev/null
@@ -0,0 +1,11 @@
+// rustfmt-multiline_closure_forces_block: true
+// Option forces multiline closure bodies to be wrapped in a block
+
+fn main() {
+    result.and_then(|maybe_value| {
+        match maybe_value {
+            None => Err("oops"),
+            Some(value) => Ok(1),
+        }
+    });
+}
diff --git a/tests/target/configs-multiline_match_arm_forces_block-false.rs b/tests/target/configs-multiline_match_arm_forces_block-false.rs
new file mode 100644 (file)
index 0000000..3c4c147
--- /dev/null
@@ -0,0 +1,11 @@
+// rustfmt-multiline_match_arm_forces_block: false
+// Option forces multiline match arm bodies to be wrapped in a block
+
+fn main() {
+    match lorem {
+        Lorem::Ipsum => if ipsum {
+            println!("dolor");
+        },
+        Lorem::Dolor => println!("amet"),
+    }
+}
diff --git a/tests/target/configs-multiline_match_arm_forces_block-true.rs b/tests/target/configs-multiline_match_arm_forces_block-true.rs
new file mode 100644 (file)
index 0000000..c36d59c
--- /dev/null
@@ -0,0 +1,13 @@
+// rustfmt-multiline_match_arm_forces_block: true
+// Option forces multiline match arm bodies to be wrapped in a block
+
+fn main() {
+    match lorem {
+        Lorem::Ipsum => {
+            if ipsum {
+                println!("dolor");
+            }
+        }
+        Lorem::Dolor => println!("amet"),
+    }
+}