Skip to content

Commit

Permalink
Changes for #4394 - Support preserving block wrapping of closure bodi…
Browse files Browse the repository at this point in the history
…es (#4519)

* Changes for #4394 - Support preserving block wrapping of closure bodies

* Add preserve_closure_block_wrapping config option support

* Code purifying based on the discussion in PR #4519

* Further purifying code

* Cosmetic comment change
  • Loading branch information
davidBar-On authored Dec 2, 2020
1 parent a96e706 commit 10b95e0
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
27 changes: 27 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,33 @@ fn say_hi() {
}
```

## `preserve_closure_block_wrapping`

Preserves block wraping arround closures. For example, useful when the closure `||` can be
confused with OR.

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

#### `true`:
Original block wrapping is preserved:
```rust
fn main() {
let explicit_conversion_preserves_semantics =
|| { !is_mod || (is_mod && attrs.map_or(true, |a| a.is_empty())) };
}
```

#### `false` (default):
Block is not preserved:
```rust
fn main() {
let explicit_conversion_preserves_semantics =
|| !is_mod || (is_mod && attrs.map_or(true, |a| a.is_empty()));
}
```

## `overflow_delimited_expr`

When structs, slices, arrays, and block/array-like macros are used as the last
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ create_config! {
format_generated_files: bool, false, false, "Format generated files";
preserve_block_start_blank_lines: bool, false, false, "Preserve blank lines at the start of \
blocks.";
preserve_closure_block_wrapping: bool, false , false, "Preserve block wrapping around closures";

// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one";
Expand Down Expand Up @@ -626,6 +627,7 @@ edition = "2018"
inline_attribute_width = 0
format_generated_files = false
preserve_block_start_blank_lines = false
preserve_closure_block_wrapping = false
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
Expand Down
13 changes: 12 additions & 1 deletion src/formatting/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,19 @@ pub(crate) fn rewrite_closure(
.map(|s| format!("{} {}", prefix, s));
}

// Whether a closure block wrapping may not be preserved (#4394).
let can_try_rewrite_without_block = if context.inside_macro() {
false
} else if context.config.preserve_closure_block_wrapping()
&& context.snippet(body.span).trim_start().starts_with('{')
{
false
} else {
true
};

let result = match fn_decl.output {
ast::FnRetTy::Default(_) if !context.inside_macro() => {
ast::FnRetTy::Default(_) if can_try_rewrite_without_block => {
try_rewrite_without_block(body, &prefix, capture, context, shape, body_shape)
}
_ => None,
Expand Down
32 changes: 32 additions & 0 deletions tests/source/preserve_closure_block_wrapping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// rustfmt-preserve_closure_block_wrapping: true

fn main() {
let explicit_conversion_preserves_semantics =
|| { !is_mod || (is_mod && attrs.map_or(true, |a| a.is_empty())) };
}

fn main() {
|| {{}};
}

fn issue1524() {
let f = |x| {{{{x}}}};
let f = |x| {{{x}}};
let f = |x| {{x}};
let f = |x| {x};
let f = |x| x;
}

fn main() {
let arg_test2 = |big_argument_name, test123| {looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame()};
}

impl Foo {
pub fn bar(&self) {
Some(SomeType {
push_closure_out_to_100_chars: iter(otherwise_it_works_ok.into_iter().map(|f| {
Ok(f)
})),
})
}
}
45 changes: 45 additions & 0 deletions tests/target/preserve_closure_block_wrapping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// rustfmt-preserve_closure_block_wrapping: true

fn main() {
let explicit_conversion_preserves_semantics =
|| { !is_mod || (is_mod && attrs.map_or(true, |a| a.is_empty())) };
}

fn main() {
|| { {} };
}

fn issue1524() {
let f = |x| {
{
{
{ x }
}
}
};
let f = |x| {
{
{ x }
}
};
let f = |x| {
{ x }
};
let f = |x| { x };
let f = |x| x;
}

fn main() {
let arg_test2 =
|big_argument_name, test123| { looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame() };
}

impl Foo {
pub fn bar(&self) {
Some(SomeType {
push_closure_out_to_100_chars: iter(
otherwise_it_works_ok.into_iter().map(|f| { Ok(f) }),
),
})
}
}

0 comments on commit 10b95e0

Please sign in to comment.