Skip to content

Commit 6e97527

Browse files
committed
add is_blocklike func on BlockLike
1 parent 98990af commit 6e97527

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

crates/parser/src/grammar.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ impl BlockLike {
198198
fn is_block(self) -> bool {
199199
self == BlockLike::Block
200200
}
201+
202+
fn is_blocklike(kind: SyntaxKind) -> bool {
203+
matches!(kind, BLOCK_EXPR | IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR)
204+
}
201205
}
202206

203207
const VISIBILITY_FIRST: TokenSet = TokenSet::new(&[T![pub], T![crate]]);

crates/parser/src/grammar/expressions.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,22 @@ pub(super) fn stmt(p: &mut Parser<'_>, semicolon: Semicolon) {
121121
types::ascription(p);
122122
}
123123

124-
let mut is_block_like_expr_after_eq = false;
124+
let mut expr_after_eq: Option<CompletedMarker> = None;
125125
if p.eat(T![=]) {
126126
// test let_stmt_init
127127
// fn f() { let x = 92; }
128-
let expr = expressions::expr(p);
129-
130-
if let Some(expr) = expr {
131-
is_block_like_expr_after_eq = match expr.kind() {
132-
IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => true,
133-
_ => false,
134-
};
135-
}
128+
expr_after_eq = expressions::expr(p);
136129
}
137130

138131
if p.at(T![else]) {
139132
// test_err let_else_right_curly_brace
140133
// fn func() { let Some(_) = {Some(1)} else { panic!("h") };}
141-
if is_block_like_expr_after_eq {
142-
p.error(
143-
"right curly brace `}` before `else` in a `let...else` statement not allowed",
144-
)
134+
if let Some(expr) = expr_after_eq {
135+
if BlockLike::is_blocklike(expr.kind()) {
136+
p.error(
137+
"right curly brace `}` before `else` in a `let...else` statement not allowed",
138+
)
139+
}
145140
}
146141

147142
// test let_else

crates/parser/src/grammar/expressions/atom.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,8 @@ pub(super) fn atom_expr(
163163
return None;
164164
}
165165
};
166-
let blocklike = match done.kind() {
167-
IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => BlockLike::Block,
168-
_ => BlockLike::NotBlock,
169-
};
166+
let blocklike =
167+
if BlockLike::is_blocklike(done.kind()) { BlockLike::Block } else { BlockLike::NotBlock };
170168
Some((done, blocklike))
171169
}
172170

0 commit comments

Comments
 (0)