mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-04 20:54:13 +00:00
Merge pull request #1898 from spinda/overflow-controls
Add overflow_closures and overflow_match_arms opts
This commit is contained in:
commit
ba6121c4e2
@ -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
|
||||
|
@ -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)]
|
||||
|
19
src/expr.rs
19
src/expr.rs
@ -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,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
11
tests/source/configs-multiline_closure_forces_block-false.rs
Normal file
11
tests/source/configs-multiline_closure_forces_block-false.rs
Normal 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),
|
||||
}
|
||||
});
|
||||
}
|
@ -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),
|
||||
});
|
||||
}
|
@ -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"),
|
||||
}
|
||||
}
|
@ -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"),
|
||||
}
|
||||
}
|
@ -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),
|
||||
});
|
||||
}
|
11
tests/target/configs-multiline_closure_forces_block-true.rs
Normal file
11
tests/target/configs-multiline_closure_forces_block-true.rs
Normal 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),
|
||||
}
|
||||
});
|
||||
}
|
@ -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"),
|
||||
}
|
||||
}
|
@ -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"),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user