Skip to main content

syntax/validation/
block.rs

1//! Logic for validating block expressions i.e. `ast::BlockExpr`.
2
3use crate::{
4    SyntaxError,
5    SyntaxKind::*,
6    ast::{self, AstNode, HasAttrs},
7};
8
9pub(crate) fn validate_block_expr(block: ast::BlockExpr, errors: &mut Vec<SyntaxError>) {
10    if let Some(parent) = block.syntax().parent() {
11        match parent.kind() {
12            FN | EXPR_STMT | STMT_LIST | MACRO_STMTS | LOOP_EXPR | WHILE_EXPR | FOR_EXPR
13            | TUPLE_EXPR | ARRAY_EXPR | ARG_LIST => {
14                return;
15            }
16            _ => {}
17        }
18    }
19    if let Some(stmt_list) = block.stmt_list() {
20        errors.extend(stmt_list.attrs().filter(|attr| attr.kind().is_inner()).map(|attr| {
21            SyntaxError::new(
22                "A block in this position cannot accept inner attributes",
23                attr.syntax().text_range(),
24            )
25        }));
26    }
27}