Skip to content

Fix rewrite of closures with a return type #4580

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Dec 8, 2020
Merged
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
23 changes: 13 additions & 10 deletions src/formatting/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,18 +409,21 @@ pub(crate) fn rewrite_last_closure(
if is_block_closure_forced(context, body, capture) {
return rewrite_closure_with_block(body, &prefix, context, body_shape).map(
|body_str| {
// If the expression can fit in a single line, we need not force block closure.
if body_str.lines().count() <= 7 {
match rewrite_closure_expr(body, &prefix, context, shape) {
Some(ref single_line_body_str)
if !single_line_body_str.contains('\n') =>
{
single_line_body_str.clone()
match fn_decl.output {
ast::FnRetTy::Default(..) if body_str.lines().count() <= 7 => {
// If the expression can fit in a single line, we need not force block
// closure. However, if the closure has a return type, then we must
// keep the blocks.
match rewrite_closure_expr(body, &prefix, context, shape) {
Some(ref single_line_body_str)
if !single_line_body_str.contains('\n') =>
{
single_line_body_str.clone()
}
_ => body_str,
}
_ => body_str,
}
} else {
body_str
_ => body_str,
}
},
);
Expand Down
20 changes: 20 additions & 0 deletions tests/source/issue-4577.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
fn main() {
let s: String = "ABAABBAA".chars()
.filter(|c| {
if *c == 'A' {
true
}
else {
false
}
})
.map(|c| -> char {
if c == 'A' {
'0'
} else {
'1'
}
}).collect();

println!("{}", s);
}
11 changes: 11 additions & 0 deletions tests/target/issue-4577.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
let s: String = "ABAABBAA"
.chars()
.filter(|c| if *c == 'A' { true } else { false })
.map(|c| -> char {
if c == 'A' { '0' } else { '1' }
})
.collect();

println!("{}", s);
}