harn_parser/typechecker/exits.rs
1//! Free helpers for "does this statement / block definitely exit?" analysis.
2//!
3//! Used both by `harn-lint` (publicly) and the type checker's flow narrowing
4//! logic (via the same-named methods on `TypeChecker`, which delegate here).
5
6use crate::ast::*;
7
8/// Check whether a single statement definitely exits (return/throw/break/continue
9/// or an if/else where both branches exit).
10pub fn stmt_definitely_exits(stmt: &SNode) -> bool {
11 match &stmt.node {
12 Node::ReturnStmt { .. } | Node::ThrowStmt { .. } | Node::BreakStmt | Node::ContinueStmt => {
13 true
14 }
15 Node::IfElse {
16 then_body,
17 else_body: Some(else_body),
18 ..
19 } => block_definitely_exits(then_body) && block_definitely_exits(else_body),
20 _ => false,
21 }
22}
23
24/// Check whether a block definitely exits (contains a terminating statement).
25pub fn block_definitely_exits(stmts: &[SNode]) -> bool {
26 stmts.iter().any(stmt_definitely_exits)
27}