Change single_let() and is_pattern_cond() to free functions

This commit is contained in:
Chayim Refael Friedman 2022-02-21 08:15:21 +02:00
parent 9881614db1
commit f70512cc17
7 changed files with 44 additions and 46 deletions

View file

@ -528,42 +528,6 @@ impl ast::Item {
}
}
impl ast::Expr {
/// Returns the `let` only if there is exactly one (that is, `let pat = expr`
/// or `((let pat = expr))`, but not `let pat = expr && expr` or `non_let_expr`).
pub fn single_let(&self) -> Option<ast::LetExpr> {
return get_pat(self.clone());
fn get_pat(expr: ast::Expr) -> Option<ast::LetExpr> {
match expr {
ast::Expr::ParenExpr(expr) => expr.expr().and_then(get_pat),
ast::Expr::LetExpr(expr) => Some(expr),
_ => None,
}
}
}
pub fn is_pattern_cond(&self) -> bool {
return contains_let(self.clone());
fn contains_let(expr: ast::Expr) -> bool {
match expr {
ast::Expr::BinExpr(expr)
if expr.op_kind() == Some(ast::BinaryOp::LogicOp(ast::LogicOp::And)) =>
{
expr.lhs()
.map(contains_let)
.or_else(|| expr.rhs().map(contains_let))
.unwrap_or(false)
}
ast::Expr::ParenExpr(expr) => expr.expr().map_or(false, contains_let),
ast::Expr::LetExpr(_) => true,
_ => false,
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldKind {
Name(ast::NameRef),