Skip to content

Commit f9cf864

Browse files
bors[bot]matklad
andauthored
Merge #3741
3741: More general ctor for ifs r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 3152862 + cbb53cf commit f9cf864

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

crates/ra_assists/src/handlers/early_return.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
104104
let then_branch =
105105
make::block_expr(once(make::expr_stmt(early_expression).into()), None);
106106
let cond = invert_boolean_expression(cond_expr);
107-
let e = make::expr_if(cond, then_branch);
107+
let e = make::expr_if(make::condition(cond, None), then_branch);
108108
if_indent_level.increase_indent(e)
109109
};
110110
replace(new_expr.syntax(), &then_block, &parent_block, &if_expr)

crates/ra_syntax/src/ast/make.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn expr_return() -> ast::Expr {
108108
pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr {
109109
expr_from_text(&format!("match {} {}", expr, match_arm_list))
110110
}
111-
pub fn expr_if(condition: ast::Expr, then_branch: ast::BlockExpr) -> ast::Expr {
111+
pub fn expr_if(condition: ast::Condition, then_branch: ast::BlockExpr) -> ast::Expr {
112112
expr_from_text(&format!("if {} {}", condition, then_branch))
113113
}
114114
pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
@@ -123,6 +123,15 @@ pub fn try_expr_from_text(text: &str) -> Option<ast::Expr> {
123123
try_ast_from_text(&format!("const C: () = {};", text))
124124
}
125125

126+
pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition {
127+
match pattern {
128+
None => ast_from_text(&format!("const _: () = while {} {{}};", expr)),
129+
Some(pattern) => {
130+
ast_from_text(&format!("const _: () = while {} = {} {{}};", pattern, expr))
131+
}
132+
}
133+
}
134+
126135
pub fn bind_pat(name: ast::Name) -> ast::BindPat {
127136
return from_text(name.text());
128137

0 commit comments

Comments
 (0)