Merge pull request #1898 from spinda/overflow-controls

Add overflow_closures and overflow_match_arms opts
This commit is contained in:
Nick Cameron 2017-08-24 18:10:51 +12:00 committed by GitHub
commit ba6121c4e2
11 changed files with 167 additions and 2 deletions

View 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

View File

@ -615,6 +615,10 @@ create_config! {
"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)]

View 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,
),
}
}

View File

@ -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),
}
});
}

View File

@ -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),
});
}

View File

@ -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"),
}
}

View File

@ -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"),
}
}

View File

@ -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),
});
}

View File

@ -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),
}
});
}

View File

@ -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"),
}
}

View File

@ -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"),
}
}