Skip to content

Commit f5e7bf2

Browse files
committed
Auto merge of #13869 - Veykril:exit-points, r=Veykril
Improve exit point highlighting for non-loop loops in tail position
2 parents dbeee34 + ec125fe commit f5e7bf2

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

crates/ide-db/src/syntax_helpers/node_ext.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ pub fn is_pattern_cond(expr: ast::Expr) -> bool {
252252
/// Note that modifying the tree while iterating it will cause undefined iteration which might
253253
/// potentially results in an out of bounds panic.
254254
pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
255+
let walk_loop = |cb: &mut dyn FnMut(&ast::Expr), label, body: Option<ast::BlockExpr>| {
256+
for_each_break_expr(label, body.and_then(|it| it.stmt_list()), &mut |b| {
257+
cb(&ast::Expr::BreakExpr(b))
258+
})
259+
};
255260
match expr {
256261
ast::Expr::BlockExpr(b) => {
257262
match b.modifier() {
@@ -291,11 +296,9 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
291296
}
292297
}
293298
}
294-
ast::Expr::LoopExpr(l) => {
295-
for_each_break_expr(l.label(), l.loop_body().and_then(|it| it.stmt_list()), &mut |b| {
296-
cb(&ast::Expr::BreakExpr(b))
297-
})
298-
}
299+
ast::Expr::LoopExpr(l) => walk_loop(cb, l.label(), l.loop_body()),
300+
ast::Expr::WhileExpr(w) => walk_loop(cb, w.label(), w.loop_body()),
301+
ast::Expr::ForExpr(f) => walk_loop(cb, f.label(), f.loop_body()),
299302
ast::Expr::MatchExpr(m) => {
300303
if let Some(arms) = m.match_arm_list() {
301304
arms.arms().filter_map(|arm| arm.expr()).for_each(|e| for_each_tail_expr(&e, cb));
@@ -311,7 +314,6 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
311314
| ast::Expr::ClosureExpr(_)
312315
| ast::Expr::ContinueExpr(_)
313316
| ast::Expr::FieldExpr(_)
314-
| ast::Expr::ForExpr(_)
315317
| ast::Expr::IndexExpr(_)
316318
| ast::Expr::Literal(_)
317319
| ast::Expr::MacroExpr(_)
@@ -325,7 +327,6 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
325327
| ast::Expr::ReturnExpr(_)
326328
| ast::Expr::TryExpr(_)
327329
| ast::Expr::TupleExpr(_)
328-
| ast::Expr::WhileExpr(_)
329330
| ast::Expr::LetExpr(_)
330331
| ast::Expr::UnderscoreExpr(_)
331332
| ast::Expr::YieldExpr(_)

crates/ide/src/highlight_related.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,23 @@ fn foo() ->$0 u32 {
765765
);
766766
}
767767

768+
#[test]
769+
fn test_hl_inner_tail_exit_points_loops() {
770+
check(
771+
r#"
772+
fn foo() ->$0 u32 {
773+
'foo: while { return 0; true } {
774+
// ^^^^^^
775+
break 'foo 0;
776+
// ^^^^^
777+
return 0;
778+
// ^^^^^^
779+
}
780+
}
781+
"#,
782+
);
783+
}
784+
768785
#[test]
769786
fn test_hl_break_loop() {
770787
check(

0 commit comments

Comments
 (0)