]> git.lizzy.rs Git - rust.git/blob - src/stmt.rs
Update rustc-ap-* crates to 606.0.0 (#3835)
[rust.git] / src / stmt.rs
1 use syntax::ast;
2 use syntax_pos::Span;
3
4 use crate::comment::recover_comment_removed;
5 use crate::config::Version;
6 use crate::expr::{format_expr, ExprType};
7 use crate::rewrite::{Rewrite, RewriteContext};
8 use crate::shape::Shape;
9 use crate::source_map::LineRangeUtils;
10 use crate::spanned::Spanned;
11 use crate::utils::semicolon_for_stmt;
12
13 pub(crate) struct Stmt<'a> {
14     inner: &'a ast::Stmt,
15     is_last: bool,
16 }
17
18 impl<'a> Spanned for Stmt<'a> {
19     fn span(&self) -> Span {
20         self.inner.span()
21     }
22 }
23
24 impl<'a> Stmt<'a> {
25     pub(crate) fn as_ast_node(&self) -> &ast::Stmt {
26         self.inner
27     }
28
29     pub(crate) fn to_item(&self) -> Option<&ast::Item> {
30         match self.inner.kind {
31             ast::StmtKind::Item(ref item) => Some(&**item),
32             _ => None,
33         }
34     }
35
36     pub(crate) fn from_ast_node(inner: &'a ast::Stmt, is_last: bool) -> Self {
37         Stmt { inner, is_last }
38     }
39
40     pub(crate) fn from_ast_nodes<I>(iter: I) -> Vec<Self>
41     where
42         I: Iterator<Item = &'a ast::Stmt>,
43     {
44         let mut result = vec![];
45         let mut iter = iter.peekable();
46         while iter.peek().is_some() {
47             result.push(Stmt {
48                 inner: iter.next().unwrap(),
49                 is_last: iter.peek().is_none(),
50             })
51         }
52         result
53     }
54
55     fn is_last_expr(&self) -> bool {
56         if !self.is_last {
57             return false;
58         }
59
60         match self.as_ast_node().kind {
61             ast::StmtKind::Expr(ref expr) => match expr.kind {
62                 ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => {
63                     false
64                 }
65                 _ => true,
66             },
67             _ => false,
68         }
69     }
70 }
71
72 impl<'a> Rewrite for Stmt<'a> {
73     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
74         let expr_type = if context.config.version() == Version::Two && self.is_last_expr() {
75             ExprType::SubExpression
76         } else {
77             ExprType::Statement
78         };
79         format_stmt(context, shape, self.as_ast_node(), expr_type)
80     }
81 }
82
83 impl Rewrite for ast::Stmt {
84     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
85         format_stmt(context, shape, self, ExprType::Statement)
86     }
87 }
88
89 fn format_stmt(
90     context: &RewriteContext<'_>,
91     shape: Shape,
92     stmt: &ast::Stmt,
93     expr_type: ExprType,
94 ) -> Option<String> {
95     skip_out_of_file_lines_range!(context, stmt.span());
96
97     let result = match stmt.kind {
98         ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
99         ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
100             let suffix = if semicolon_for_stmt(context, stmt) {
101                 ";"
102             } else {
103                 ""
104             };
105
106             let shape = shape.sub_width(suffix.len())?;
107             format_expr(ex, expr_type, context, shape).map(|s| s + suffix)
108         }
109         ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
110     };
111     result.and_then(|res| recover_comment_removed(res, stmt.span(), context))
112 }