Skip to content

Commit 9714a14

Browse files
refactor: use ast::attr:HasAttrs
1 parent ac2d5b8 commit 9714a14

File tree

3 files changed

+13
-27
lines changed

3 files changed

+13
-27
lines changed

src/attr.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Format attributes and meta items.
22
33
use rustc_ast::ast;
4+
use rustc_ast::attr::HasAttrs;
45
use rustc_span::{symbol::sym, BytePos, Span, DUMMY_SP};
56

67
use self::doc_comment::DocCommentFormatter;
@@ -18,14 +19,8 @@ use crate::utils::{count_newlines, mk_sp};
1819
mod 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

3126
pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {

src/closures.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,9 @@ fn get_inner_expr<'a>(
105105

106106
// Figure out if a block is necessary.
107107
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+
});
115111

116112
is_unsafe_block(block)
117113
|| block.stmts.len() > 1

src/visitor.rs

+7-12
Original file line numberDiff line numberDiff 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));

0 commit comments

Comments
 (0)