Skip to content

Commit

Permalink
Add overflow_closures and overflow_match_arms opts
Browse files Browse the repository at this point in the history
overflow_closures = true:
    result.and_then(|maybe_value| match maybe_value {
        None => ...,
        Some(value) => ...,
    })

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

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

overflow_match_arms = false:
    match lorem {
        None => {
            if ipsum {
                println!("Hello World");
            }
        }
        Some(dolor) => ...,
    }
  • Loading branch information
spinda committed Aug 19, 2017
1 parent c166004 commit 48b0c35
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 2 deletions.
58 changes: 58 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,64 @@ fn dolor() -> usize {}
fn adipiscing() -> usize {}
```

## `overflow_closures`

Allow overflow on closures

- **Default value**: `true`
- **Possible values**: `true`, `false`

#### `true`:

```rust
result.and_then(|maybe_value| match maybe_value {
None => ...,
Some(value) => ...,
})
```

#### `false`:

```rust
result.and_then(|maybe_value| {
match maybe_value {
None => ...,
Some(value) => ...,
}
})
```

## `overflow_match_arms`

Allow overflow on match arms

- **Default value**: `true`
- **Possible values**: `true`, `false`

#### `true`:

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

#### `false`:

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

## `reorder_imported_names`

Reorder lists of names in import statements alphabetically
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,8 @@ create_config! {
threshold.";
remove_blank_lines_at_start_or_end_of_block: bool, true,
"Remove blank lines at start or end of a block";
overflow_match_arms: bool, true, "Allow overflow on match arms.";
overflow_closures: bool, true, "Allow overflow on closures.";
}

#[cfg(test)]
Expand Down
17 changes: 15 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,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.overflow_closures() && rw.contains('\n') {
None
} else {
Some(rw)
}
});
rewrite.map(|rw| format!("{} {}", prefix, rw))
}

Expand Down Expand Up @@ -1697,12 +1704,18 @@ fn rewrite_match_body(
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.overflow_match_arms() && expr.can_be_overflowed(context, 1),
&**expr,
)
} else {
(false, &**body)
}
}
_ => (body.can_be_overflowed(context, 1), &**body),
_ => (
context.config.overflow_match_arms() && body.can_be_overflowed(context, 1),
&**body,
),
};

let comma = arm_comma(&context.config, body);
Expand Down
9 changes: 9 additions & 0 deletions tests/source/configs-overflow_closures-false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-overflow_closures: false
// Overflowing closures

fn main() {
result.and_then(|maybe_value| match maybe_value {
None => Err("oops"),
Some(value) => Ok(1),
});
}
11 changes: 11 additions & 0 deletions tests/source/configs-overflow_closures-true.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-overflow_closures: true
// Overflowing closures

fn main() {
result.and_then(|maybe_value| {
match maybe_value {
None => Err("oops"),
Some(value) => Ok(1),
}
});
}
11 changes: 11 additions & 0 deletions tests/source/configs-overflow_match_arms-false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-overflow_match_arms: false
// Overflowing match arms

fn main() {
match lorem {
Lorem::Ipsum => if ipsum {
println!("dolor");
},
Lorem::Dolor => println!("amet"),
}
}
13 changes: 13 additions & 0 deletions tests/source/configs-overflow_match_arms-true.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// rustfmt-overflow_match_arms: true
// Overflowing match arms

fn main() {
match lorem {
Lorem::Ipsum => {
if ipsum {
println!("dolor");
}
}
Lorem::Dolor => println!("amet"),
}
}
11 changes: 11 additions & 0 deletions tests/target/configs-overflow_closures-false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-overflow_closures: false
// Overflowing closures

fn main() {
result.and_then(|maybe_value| {
match maybe_value {
None => Err("oops"),
Some(value) => Ok(1),
}
});
}
9 changes: 9 additions & 0 deletions tests/target/configs-overflow_closures-true.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-overflow_closures: true
// Overflowing closures

fn main() {
result.and_then(|maybe_value| match maybe_value {
None => Err("oops"),
Some(value) => Ok(1),
});
}
13 changes: 13 additions & 0 deletions tests/target/configs-overflow_match_arms-false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// rustfmt-overflow_match_arms: false
// Overflowing match arms

fn main() {
match lorem {
Lorem::Ipsum => {
if ipsum {
println!("dolor");
}
}
Lorem::Dolor => println!("amet"),
}
}
11 changes: 11 additions & 0 deletions tests/target/configs-overflow_match_arms-true.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-overflow_match_arms: true
// Overflowing match arms

fn main() {
match lorem {
Lorem::Ipsum => if ipsum {
println!("dolor");
},
Lorem::Dolor => println!("amet"),
}
}

0 comments on commit 48b0c35

Please sign in to comment.