Skip to content

Commit af663d8

Browse files
committed
Ignore fn_call_width when rewriting a call with a single non-call arg
1 parent 94a770a commit af663d8

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/expr.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,7 +1953,7 @@ where
19531953
context.force_one_line_chain = true;
19541954
}
19551955
}
1956-
last_arg_shape(&context, item_vec, one_line_shape, args_max_width).and_then(|arg_shape| {
1956+
last_arg_shape(args, item_vec, one_line_shape, args_max_width).and_then(|arg_shape| {
19571957
rewrite_last_arg_with_overflow(&context, args, &mut item_vec[args.len() - 1], arg_shape)
19581958
})
19591959
} else {
@@ -2000,26 +2000,32 @@ where
20002000
tactic
20012001
}
20022002

2003-
fn last_arg_shape(
2004-
context: &RewriteContext,
2003+
/// Returns a shape for the last argument which is going to be overflowed.
2004+
fn last_arg_shape<T>(
2005+
lists: &[&T],
20052006
items: &[ListItem],
20062007
shape: Shape,
20072008
args_max_width: usize,
2008-
) -> Option<Shape> {
2009-
let overhead = items.iter().rev().skip(1).fold(0, |acc, i| {
2010-
acc + i.item.as_ref().map_or(0, |s| first_line_width(s))
2009+
) -> Option<Shape>
2010+
where
2011+
T: Rewrite + Spanned + ToExpr,
2012+
{
2013+
let is_nested_call = lists
2014+
.iter()
2015+
.next()
2016+
.and_then(|item| item.to_expr())
2017+
.map_or(false, is_nested_call);
2018+
if items.len() == 1 && !is_nested_call {
2019+
return Some(shape);
2020+
}
2021+
let offset = items.iter().rev().skip(1).fold(0, |acc, i| {
2022+
// 2 = ", "
2023+
acc + 2 + i.inner_as_ref().len()
20112024
});
2012-
let max_width = min(args_max_width, shape.width);
2013-
let arg_indent = if context.use_block_indent() {
2014-
shape.block().indent.block_unindent(context.config)
2015-
} else {
2016-
shape.block().indent
2017-
};
2018-
Some(Shape {
2019-
width: max_width.checked_sub(overhead)?,
2020-
indent: arg_indent,
2021-
offset: 0,
2022-
})
2025+
Shape {
2026+
width: min(args_max_width, shape.width),
2027+
..shape
2028+
}.offset_left(offset)
20232029
}
20242030

20252031
fn rewrite_last_arg_with_overflow<'a, T>(
@@ -2101,6 +2107,18 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
21012107
}
21022108
}
21032109

2110+
fn is_nested_call(expr: &ast::Expr) -> bool {
2111+
match expr.node {
2112+
ast::ExprKind::Call(..) | ast::ExprKind::Mac(..) => true,
2113+
ast::ExprKind::AddrOf(_, ref expr)
2114+
| ast::ExprKind::Box(ref expr)
2115+
| ast::ExprKind::Try(ref expr)
2116+
| ast::ExprKind::Unary(_, ref expr)
2117+
| ast::ExprKind::Cast(ref expr, _) => is_nested_call(expr),
2118+
_ => false,
2119+
}
2120+
}
2121+
21042122
pub fn wrap_args_with_parens(
21052123
context: &RewriteContext,
21062124
args_str: &str,

0 commit comments

Comments
 (0)