diff --git a/Configurations.md b/Configurations.md index 0e644f4509d..a0c6ba407d9 100644 --- a/Configurations.md +++ b/Configurations.md @@ -112,6 +112,28 @@ let lorem = ipsum.dolor().sit().amet().consectetur().adipiscing().elit(); #### Lines longer than `chain_one_line_max`: See [`chain_indent`](#chain_indent). +## `chain_split_single_child` + +Split a chain with a single child if its length exceeds [`chain_one_line_max`](#chain_one_line_max). + +- **Default value**: `false` +- **Possible values**: `false`, `true` + +#### `false` + +```rust +let files = fs::read_dir("tests/coverage/source").expect("Couldn't read source dir"); +``` + +#### `true` + +```rust +let files = fs::read_dir("tests/coverage/source") + .expect("Couldn't read source dir"); +``` + +See also [`chain_one_line_max`](#chain_one_line_max). + ## `closure_block_indent_threshold` How many lines a closure must have before it is block indented. -1 means never use block indent. diff --git a/src/chains.rs b/src/chains.rs index e5450aa687e..9a8a6914440 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -163,10 +163,10 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) - first_child_shape, other_child_shape); - let child_shape_iter = Some(first_child_shape) - .into_iter() - .chain(::std::iter::repeat(other_child_shape) - .take(subexpr_list.len() - 1)); + let child_shape_iter = + Some(first_child_shape) + .into_iter() + .chain(::std::iter::repeat(other_child_shape).take(subexpr_list.len() - 1)); let iter = subexpr_list.iter().rev().zip(child_shape_iter); let mut rewrites = try_opt!(iter.map(|(e, shape)| { rewrite_chain_subexpr(e, total_span, context, shape) @@ -186,7 +186,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) - if rewrites.len() > 1 { true } else if rewrites.len() == 1 { - parent_rewrite.len() > context.config.chain_one_line_max() / 2 + context.config.chain_split_single_child() || one_line_len > shape.width } else { false } diff --git a/tests/target/configs-chain_split_single_child-false.rs b/tests/target/configs-chain_split_single_child-false.rs new file mode 100644 index 00000000000..1863c6ac33f --- /dev/null +++ b/tests/target/configs-chain_split_single_child-false.rs @@ -0,0 +1,5 @@ +// rustfmt-chain_split_single_child: false + +fn main() { + let files = fs::read_dir("tests/source").expect("Couldn't read source dir"); +} diff --git a/tests/target/configs-chain_split_single_child-true.rs b/tests/target/configs-chain_split_single_child-true.rs new file mode 100644 index 00000000000..154fa0bfa2e --- /dev/null +++ b/tests/target/configs-chain_split_single_child-true.rs @@ -0,0 +1,6 @@ +// rustfmt-chain_split_single_child: true + +fn main() { + let files = fs::read_dir("tests/source") + .expect("Couldn't read source dir"); +}