Deduplicate variant matching

This commit is contained in:
Dániel Buga 2021-01-22 23:31:19 +01:00
parent 24f0cd8293
commit 5728d7186e
3 changed files with 25 additions and 42 deletions

View file

@ -248,3 +248,22 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
pub(crate) fn next_prev() -> impl Iterator<Item = Direction> {
[Direction::Next, Direction::Prev].iter().copied()
}
pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
let first_node_text = |pat: &ast::Pat| pat.syntax().first_child().map(|node| node.text());
let pat_head = match pat {
ast::Pat::IdentPat(bind_pat) => {
if let Some(p) = bind_pat.pat() {
first_node_text(&p)
} else {
return pat.syntax().text() == var.syntax().text();
}
}
pat => first_node_text(pat),
};
let var_head = first_node_text(var);
pat_head == var_head
}