Fix rewrite of closures with a return type
If the closure's body fits in a line, the block is removed but it is necessary if the closure has a return type.
This commit is contained in:
parent
4cfb9ef8f4
commit
c536d80dc1
3 changed files with 48 additions and 10 deletions
|
@ -339,18 +339,21 @@ pub(crate) fn rewrite_last_closure(
|
|||
if is_block_closure_forced(context, body) {
|
||||
return rewrite_closure_with_block(body, &prefix, context, body_shape).and_then(
|
||||
|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') =>
|
||||
{
|
||||
Some(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') =>
|
||||
{
|
||||
Some(single_line_body_str.clone())
|
||||
}
|
||||
_ => Some(body_str),
|
||||
}
|
||||
_ => Some(body_str),
|
||||
}
|
||||
} else {
|
||||
Some(body_str)
|
||||
_ => Some(body_str),
|
||||
}
|
||||
},
|
||||
);
|
||||
|
|
20
tests/source/issue-4577.rs
Normal file
20
tests/source/issue-4577.rs
Normal file
|
@ -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);
|
||||
}
|
15
tests/target/issue-4577.rs
Normal file
15
tests/target/issue-4577.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
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);
|
||||
}
|
Loading…
Reference in a new issue