]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast/src/util/classify.rs
Auto merge of #99918 - WaffleLapkin:fnFnfun, r=estebank
[rust.git] / compiler / rustc_ast / src / util / classify.rs
1 //! Routines the parser uses to classify AST nodes
2
3 // Predicates on exprs and stmts that the pretty-printer and parser use
4
5 use crate::ast;
6
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
14 pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
15     !matches!(
16         e.kind,
17         ast::ExprKind::If(..)
18             | ast::ExprKind::Match(..)
19             | ast::ExprKind::Block(..)
20             | ast::ExprKind::While(..)
21             | ast::ExprKind::Loop(..)
22             | ast::ExprKind::ForLoop(..)
23             | ast::ExprKind::TryBlock(..)
24     )
25 }
26
27 /// If an expression ends with `}`, returns the innermost expression ending in the `}`
28 pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
29     use ast::ExprKind::*;
30
31     loop {
32         match &expr.kind {
33             AddrOf(_, _, e)
34             | Assign(_, e, _)
35             | AssignOp(_, _, e)
36             | Binary(_, _, e)
37             | Box(e)
38             | Break(_, Some(e))
39             | Closure(.., e, _)
40             | Let(_, e, _)
41             | Range(_, Some(e), _)
42             | Ret(Some(e))
43             | Unary(_, e)
44             | Yield(Some(e)) => {
45                 expr = e;
46             }
47             Async(..) | Block(..) | ForLoop(..) | If(..) | Loop(..) | Match(..) | Struct(..)
48             | TryBlock(..) | While(..) => break Some(expr),
49             _ => break None,
50         }
51     }
52 }