]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_ast/src/util/classify.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / compiler / rustc_ast / src / util / classify.rs
CommitLineData
1a4d82fc
JJ
1//! Routines the parser uses to classify AST nodes
2
3// Predicates on exprs and stmts that the pretty-printer and parser use
223e47cc 4
9fa01778 5use crate::ast;
223e47cc 6
1a4d82fc
JJ
7/// Does this expression require a semicolon to be treated
8/// as a statement? The negation of this: 'can this expression
9/// be used as a statement without a semicolon' -- is used
10/// as an early-bail-out in the parser so that, for instance,
11/// if true {...} else {...}
12/// |x| 5
13/// isn't parsed as (if true {...} else {...} | x) | 5
14pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
5869c6ff
XL
15 !matches!(
16 e.kind,
dfeec247 17 ast::ExprKind::If(..)
5869c6ff
XL
18 | ast::ExprKind::Match(..)
19 | ast::ExprKind::Block(..)
20 | ast::ExprKind::While(..)
21 | ast::ExprKind::Loop(..)
22 | ast::ExprKind::ForLoop(..)
23 | ast::ExprKind::TryBlock(..)
487cf647 24 | ast::ExprKind::ConstBlock(..)
5869c6ff 25 )
223e47cc 26}
94222f64
XL
27
28/// If an expression ends with `}`, returns the innermost expression ending in the `}`
29pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
30 use ast::ExprKind::*;
31
32 loop {
33 match &expr.kind {
34 AddrOf(_, _, e)
35 | Assign(_, e, _)
36 | AssignOp(_, _, e)
37 | Binary(_, _, e)
94222f64 38 | Break(_, Some(e))
94222f64
XL
39 | Let(_, e, _)
40 | Range(_, Some(e), _)
41 | Ret(Some(e))
42 | Unary(_, e)
43 | Yield(Some(e)) => {
44 expr = e;
45 }
487cf647
FG
46 Closure(closure) => {
47 expr = &closure.body;
48 }
94222f64
XL
49 Async(..) | Block(..) | ForLoop(..) | If(..) | Loop(..) | Match(..) | Struct(..)
50 | TryBlock(..) | While(..) => break Some(expr),
51 _ => break None,
52 }
53 }
54}