diff --git a/src/expr.rs b/src/expr.rs index 8e6d811e87b..afa31983046 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1850,6 +1850,9 @@ where }; let used_width = extra_offset(callee_str, shape); let one_line_width = shape.width.checked_sub(used_width + 2 * paren_overhead)?; + // 1 = "(" + let combine_arg_with_callee = + callee_str.len() + 1 <= context.config.tab_spaces() && args.len() == 1; // 1 = "(" or ")" let one_line_shape = shape @@ -1874,6 +1877,7 @@ where one_line_width, args_max_width, force_trailing_comma, + combine_arg_with_callee, )?; if !context.use_block_indent() && need_block_indent(&list_str, nested_shape) && !extendable { @@ -1914,6 +1918,7 @@ fn rewrite_call_args<'a, T>( one_line_width: usize, args_max_width: usize, force_trailing_comma: bool, + combine_arg_with_callee: bool, ) -> Option<(bool, String)> where T: Rewrite + Spanned + ToExpr + 'a, @@ -1943,6 +1948,7 @@ where nested_shape, one_line_width, args_max_width, + combine_arg_with_callee, ); let fmt = ListFormatting { @@ -1973,19 +1979,22 @@ fn try_overflow_last_arg<'a, T>( nested_shape: Shape, one_line_width: usize, args_max_width: usize, + combine_arg_with_callee: bool, ) -> DefinitiveListTactic where T: Rewrite + Spanned + ToExpr + 'a, { - let overflow_last = can_be_overflowed(context, args); + let overflow_last = combine_arg_with_callee || can_be_overflowed(context, args); // Replace the last item with its first line to see if it fits with // first arguments. let placeholder = if overflow_last { let mut context = context.clone(); - if let Some(expr) = args[args.len() - 1].to_expr() { - if let ast::ExprKind::MethodCall(..) = expr.node { - context.force_one_line_chain = true; + if !combine_arg_with_callee { + if let Some(expr) = args[args.len() - 1].to_expr() { + if let ast::ExprKind::MethodCall(..) = expr.node { + context.force_one_line_chain = true; + } } } last_arg_shape(args, item_vec, one_line_shape, args_max_width).and_then(|arg_shape| { diff --git a/tests/source/expr.rs b/tests/source/expr.rs index 1a8d35f2f0c..4353efac4ec 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -355,3 +355,7 @@ fn newlines_between_list_like_expr() { _ => bar(), }; } + +fn issue2178() { + Ok(result.iter().map(|item| ls_util::rls_to_location(item)).collect()) +} diff --git a/tests/target/chains-visual.rs b/tests/target/chains-visual.rs index 507f91113cc..8a6e44ed86b 100644 --- a/tests/target/chains-visual.rs +++ b/tests/target/chains-visual.rs @@ -16,9 +16,9 @@ fn main() { // Test case where first chain element isn't a path, but is shorter than // the size of a tab. x().y(|| match cond() { - true => (), - false => (), - }); + true => (), + false => (), + }); loong_func().quux(move || if true { 1 } else { 2 }); diff --git a/tests/target/expr.rs b/tests/target/expr.rs index 08803e5db4e..0206548b31b 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -408,3 +408,10 @@ fn newlines_between_list_like_expr() { _ => bar(), }; } + +fn issue2178() { + Ok(result + .iter() + .map(|item| ls_util::rls_to_location(item)) + .collect()) +}