10821: fix: wrap `inline_call` and `inline_into_callers` if it inlines into the left side of a binary expression r=Veykril a=rainy-me

close #10359

Co-authored-by: rainy-me <github@yue.coffee>
This commit is contained in:
bors[bot] 2021-11-20 17:25:17 +00:00 committed by GitHub
commit c9c6fa8f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -407,7 +407,17 @@ fn inline(
match body.tail_expr() {
Some(expr) if body.statements().next().is_none() => expr,
_ => ast::Expr::BlockExpr(body),
_ => match node
.syntax()
.parent()
.and_then(ast::BinExpr::cast)
.and_then(|bin_expr| bin_expr.lhs())
{
Some(lhs) if lhs.syntax() == node.syntax() => {
make::expr_paren(ast::Expr::BlockExpr(body)).clone_for_update()
}
_ => ast::Expr::BlockExpr(body),
},
}
}
@ -1076,4 +1086,60 @@ fn main() {
"#,
);
}
#[test]
fn inline_callers_wrapped_in_parentheses() {
check_assist(
inline_into_callers,
r#"
fn foo$0() -> u32 {
let x = 0;
x
}
fn bar() -> u32 {
foo() + foo()
}
"#,
r#"
fn bar() -> u32 {
({
let x = 0;
x
}) + {
let x = 0;
x
}
}
"#,
)
}
#[test]
fn inline_call_wrapped_in_parentheses() {
check_assist(
inline_call,
r#"
fn foo() -> u32 {
let x = 0;
x
}
fn bar() -> u32 {
foo$0() + foo()
}
"#,
r#"
fn foo() -> u32 {
let x = 0;
x
}
fn bar() -> u32 {
({
let x = 0;
x
}) + foo()
}
"#,
)
}
}