Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overflow_closures and overflow_match_arms opts #1898

Merged
merged 1 commit into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
19 changes: 17 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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,
),
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/source/configs-multiline_closure_forces_block-false.rs
Original file line number Diff line number Diff line change
@@ -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),
}
});
}
9 changes: 9 additions & 0 deletions tests/source/configs-multiline_closure_forces_block-true.rs
Original file line number Diff line number Diff line change
@@ -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),
});
}
13 changes: 13 additions & 0 deletions tests/source/configs-multiline_match_arm_forces_block-false.rs
Original file line number Diff line number Diff line change
@@ -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"),
}
}
11 changes: 11 additions & 0 deletions tests/source/configs-multiline_match_arm_forces_block-true.rs
Original file line number Diff line number Diff line change
@@ -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"),
}
}
9 changes: 9 additions & 0 deletions tests/target/configs-multiline_closure_forces_block-false.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions tests/target/configs-multiline_closure_forces_block-true.rs
Original file line number Diff line number Diff line change
@@ -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),
}
});
}
11 changes: 11 additions & 0 deletions tests/target/configs-multiline_match_arm_forces_block-false.rs
Original file line number Diff line number Diff line change
@@ -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"),
}
}
13 changes: 13 additions & 0 deletions tests/target/configs-multiline_match_arm_forces_block-true.rs
Original file line number Diff line number Diff line change
@@ -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"),
}
}