File tree Expand file tree Collapse file tree 3 files changed +13
-27
lines changed Expand file tree Collapse file tree 3 files changed +13
-27
lines changed Original file line number Diff line number Diff line change 11//! Format attributes and meta items.
22
33use rustc_ast:: ast;
4+ use rustc_ast:: attr:: HasAttrs ;
45use rustc_span:: { symbol:: sym, BytePos , Span , DUMMY_SP } ;
56
67use self :: doc_comment:: DocCommentFormatter ;
@@ -18,14 +19,8 @@ use crate::utils::{count_newlines, mk_sp};
1819mod doc_comment;
1920
2021/// 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 ( )
2924}
3025
3126pub ( 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>(
105105
106106// Figure out if a block is necessary.
107107fn 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+ } ) ;
115111
116112 is_unsafe_block ( block)
117113 || block. stmts . len ( ) > 1
Original file line number Diff line number Diff line change @@ -133,18 +133,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
133133 self . format_missing ( stmt. span ( ) . hi ( ) ) ;
134134 }
135135 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+ ) ;
148143 } else {
149144 let shape = self . shape ( ) ;
150145 let rewrite = self . with_context ( |ctx| stmt. rewrite ( & ctx, shape) ) ;
You can’t perform that action at this time.
0 commit comments