Skip to content

Updated how nested function calls are overflowed #5443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,56 @@ impl<'a> Context<'a> {
}
}

// A nested call e.g a(b(...))
// In this case we're trying to figure out how to overflow function `b`.
ast::ExprKind::Call(ref callee, ref args) if self.items.len() == 1 => {
callee.rewrite(self.context, shape).and_then(|callee_str| {
if !callee_str.contains('\n') {
// if the rewritten callee does not contains a newline then return
// the rewritten expression
return expr.rewrite(self.context, shape);
}

// If the callee string contains a newline (likely because of generics),
// then we'll update how we'll overflow the argument.

let items = itemize_list(
self.context.snippet_provider,
args.iter(),
self.suffix,
",",
|item| item.span().lo(),
|item| item.span().hi(),
|item| item.rewrite(self.context, self.nested_shape),
callee.span.hi(),
expr.span.hi(),
true,
);
let items: Vec<_> = items.collect();
let tactic = self.default_tactic(&items);

let shape = if tactic == DefinitiveListTactic::Horizontal {
// if the argument list could be written horizontally, then unindent
// the shape before rewriting.
Shape::legacy(
self.item_max_width,
shape.indent.block_unindent(self.context.config),
)
} else {
// If the arguments need any other type of list tactic then
// indent the shape before rewriting.
shape.block_indent(self.item_max_width)
};

let rewrite = expr.rewrite(self.context, shape)?;
let indentation = shape
.indent
.to_string_with_newline(self.context.config)
.to_string();
Some(indentation + &rewrite)
})
}

_ => expr.rewrite(self.context, shape),
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/source/issue_5356.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn fail() {
{
{
if true {
return Err(SomeError::OutOfMemory(OutOfMemory::new(mem::size_of::<SomeType>())));
} else if true {
return Err(SomeError::OutOfMemory(OutOfMemory::new(core::mem::size_of::<SomeType>())));
} else {
return Err(SomeError::OutOfMemory(OutOfMemory::new(mem::size_of::<SomeType>(aaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbb, cccccccccccccc, dddddddddddddddddd, eeeeeeeeeeeeeeeee))));
}
}
}
}
25 changes: 25 additions & 0 deletions tests/target/issue_5356.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
fn fail() {
{
{
if true {
return Err(SomeError::OutOfMemory(OutOfMemory::new(
mem::size_of::<SomeType>()
)));
} else if true {
return Err(SomeError::OutOfMemory(OutOfMemory::new(
core::mem::size_of::<SomeType>()
)));
} else {
return Err(SomeError::OutOfMemory(OutOfMemory::new(
mem::size_of::<SomeType>(
aaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbb,
cccccccccccccc,
dddddddddddddddddd,
eeeeeeeeeeeeeeeee,
),
)));
}
}
}
}