Skip to content

Commit

Permalink
Add multiline_{closure,match_arm}_forces_block
Browse files Browse the repository at this point in the history
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) => ...,
    }
  • Loading branch information
spinda committed Aug 21, 2017
1 parent 7e17183 commit 411c73c
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 2 deletions.
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"),
}
}

0 comments on commit 411c73c

Please sign in to comment.