Skip to content

Commit 964ab3e

Browse files
committed
Fix untyped syntax tree ans casts for convert_to_guarded_return
1 parent a56e577 commit 964ab3e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

crates/ide-assists/src/handlers/convert_to_guarded_return.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,8 @@ fn if_expr_to_guarded_return(
8080
return None;
8181
}
8282

83-
// FIXME: This relies on untyped syntax tree and casts to much. It should be
84-
// rewritten to use strongly-typed APIs.
85-
8683
// check for early return and continue
87-
let first_in_then_block = then_block.syntax().first_child()?;
88-
if ast::ReturnExpr::can_cast(first_in_then_block.kind())
89-
|| ast::ContinueExpr::can_cast(first_in_then_block.kind())
90-
|| first_in_then_block
91-
.children()
92-
.any(|x| ast::ReturnExpr::can_cast(x.kind()) || ast::ContinueExpr::can_cast(x.kind()))
93-
{
84+
if is_early_block(&then_block) {
9485
return None;
9586
}
9687

@@ -272,6 +263,17 @@ fn flat_let_chain(mut expr: ast::Expr) -> Vec<ast::Expr> {
272263
chains
273264
}
274265

266+
fn is_early_block(then_block: &ast::StmtList) -> bool {
267+
let is_early_expr =
268+
|expr| matches!(expr, ast::Expr::ReturnExpr(_) | ast::Expr::ContinueExpr(_));
269+
let into_expr = |stmt| match stmt {
270+
ast::Stmt::ExprStmt(expr_stmt) => expr_stmt.expr(),
271+
_ => None,
272+
};
273+
then_block.tail_expr().is_some_and(is_early_expr)
274+
|| then_block.statements().filter_map(into_expr).any(is_early_expr)
275+
}
276+
275277
#[cfg(test)]
276278
mod tests {
277279
use crate::tests::{check_assist, check_assist_not_applicable};

0 commit comments

Comments
 (0)