refactor: use ast::attr:HasAttrs

This commit is contained in:
Caleb Cartwright 2020-03-30 13:11:00 -05:00
parent ac2d5b85a5
commit 9714a140c9
3 changed files with 13 additions and 27 deletions

View File

@ -1,6 +1,7 @@
//! Format attributes and meta items.
use rustc_ast::ast;
use rustc_ast::attr::HasAttrs;
use rustc_span::{symbol::sym, BytePos, Span, DUMMY_SP};
use self::doc_comment::DocCommentFormatter;
@ -18,14 +19,8 @@ use crate::utils::{count_newlines, mk_sp};
mod doc_comment;
/// Returns attributes on the given statement.
pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> Option<&[ast::Attribute]> {
match stmt.kind {
ast::StmtKind::Local(ref local) => Some(&local.attrs),
ast::StmtKind::Item(ref item) => Some(&item.attrs),
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => Some(&expr.attrs),
ast::StmtKind::MacCall(ref mac) => Some(&mac.2),
ast::StmtKind::Empty => None,
}
pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
stmt.attrs()
}
pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {

View File

@ -105,13 +105,9 @@ fn get_inner_expr<'a>(
// Figure out if a block is necessary.
fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -> bool {
let has_attributes = block
.stmts
.first()
.map_or(false, |first_stmt| match get_attrs_from_stmt(first_stmt) {
Some(attrs) => !attrs.is_empty(),
None => false,
});
let has_attributes = block.stmts.first().map_or(false, |first_stmt| {
!get_attrs_from_stmt(first_stmt).is_empty()
});
is_unsafe_block(block)
|| block.stmts.len() > 1

View File

@ -133,18 +133,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.format_missing(stmt.span().hi());
}
ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
if let Some(attrs) = get_attrs_from_stmt(stmt.as_ast_node()) {
if contains_skip(attrs) {
self.push_skipped_with_span(
attrs,
stmt.span(),
get_span_without_attrs(stmt.as_ast_node()),
);
} else {
let shape = self.shape();
let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));
self.push_rewrite(stmt.span(), rewrite)
}
let attrs = get_attrs_from_stmt(stmt.as_ast_node());
if contains_skip(attrs) {
self.push_skipped_with_span(
attrs,
stmt.span(),
get_span_without_attrs(stmt.as_ast_node()),
);
} else {
let shape = self.shape();
let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));