File tree 3 files changed +13
-27
lines changed
3 files changed +13
-27
lines changed Original file line number Diff line number Diff line change 1
1
//! Format attributes and meta items.
2
2
3
3
use rustc_ast:: ast;
4
+ use rustc_ast:: attr:: HasAttrs ;
4
5
use rustc_span:: { symbol:: sym, BytePos , Span , DUMMY_SP } ;
5
6
6
7
use self :: doc_comment:: DocCommentFormatter ;
@@ -18,14 +19,8 @@ use crate::utils::{count_newlines, mk_sp};
18
19
mod doc_comment;
19
20
20
21
/// Returns attributes on the given statement.
21
- pub ( crate ) fn get_attrs_from_stmt ( stmt : & ast:: Stmt ) -> Option < & [ ast:: Attribute ] > {
22
- match stmt. kind {
23
- ast:: StmtKind :: Local ( ref local) => Some ( & local. attrs ) ,
24
- ast:: StmtKind :: Item ( ref item) => Some ( & item. attrs ) ,
25
- ast:: StmtKind :: Expr ( ref expr) | ast:: StmtKind :: Semi ( ref expr) => Some ( & expr. attrs ) ,
26
- ast:: StmtKind :: MacCall ( ref mac) => Some ( & mac. 2 ) ,
27
- ast:: StmtKind :: Empty => None ,
28
- }
22
+ pub ( crate ) fn get_attrs_from_stmt ( stmt : & ast:: Stmt ) -> & [ ast:: Attribute ] {
23
+ stmt. attrs ( )
29
24
}
30
25
31
26
pub ( crate ) fn get_span_without_attrs ( stmt : & ast:: Stmt ) -> Span {
Original file line number Diff line number Diff line change @@ -105,13 +105,9 @@ fn get_inner_expr<'a>(
105
105
106
106
// Figure out if a block is necessary.
107
107
fn needs_block ( block : & ast:: Block , prefix : & str , context : & RewriteContext < ' _ > ) -> bool {
108
- let has_attributes = block
109
- . stmts
110
- . first ( )
111
- . map_or ( false , |first_stmt| match get_attrs_from_stmt ( first_stmt) {
112
- Some ( attrs) => !attrs. is_empty ( ) ,
113
- None => false ,
114
- } ) ;
108
+ let has_attributes = block. stmts . first ( ) . map_or ( false , |first_stmt| {
109
+ !get_attrs_from_stmt ( first_stmt) . is_empty ( )
110
+ } ) ;
115
111
116
112
is_unsafe_block ( block)
117
113
|| block. stmts . len ( ) > 1
Original file line number Diff line number Diff line change @@ -133,18 +133,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
133
133
self . format_missing ( stmt. span ( ) . hi ( ) ) ;
134
134
}
135
135
ast:: StmtKind :: Local ( ..) | ast:: StmtKind :: Expr ( ..) | ast:: StmtKind :: Semi ( ..) => {
136
- if let Some ( attrs) = get_attrs_from_stmt ( stmt. as_ast_node ( ) ) {
137
- if contains_skip ( attrs) {
138
- self . push_skipped_with_span (
139
- attrs,
140
- stmt. span ( ) ,
141
- get_span_without_attrs ( stmt. as_ast_node ( ) ) ,
142
- ) ;
143
- } else {
144
- let shape = self . shape ( ) ;
145
- let rewrite = self . with_context ( |ctx| stmt. rewrite ( & ctx, shape) ) ;
146
- self . push_rewrite ( stmt. span ( ) , rewrite)
147
- }
136
+ let attrs = get_attrs_from_stmt ( stmt. as_ast_node ( ) ) ;
137
+ if contains_skip ( attrs) {
138
+ self . push_skipped_with_span (
139
+ attrs,
140
+ stmt. span ( ) ,
141
+ get_span_without_attrs ( stmt. as_ast_node ( ) ) ,
142
+ ) ;
148
143
} else {
149
144
let shape = self . shape ( ) ;
150
145
let rewrite = self . with_context ( |ctx| stmt. rewrite ( & ctx, shape) ) ;
You can’t perform that action at this time.
0 commit comments