From 9714a140c99a8968b0becb5cf9227b1787768088 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Mon, 30 Mar 2020 13:11:00 -0500 Subject: [PATCH] refactor: use ast::attr:HasAttrs --- src/attr.rs | 11 +++-------- src/closures.rs | 10 +++------- src/visitor.rs | 19 +++++++------------ 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/attr.rs b/src/attr.rs index 5221caf2fad..b9e160020fb 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -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 { diff --git a/src/closures.rs b/src/closures.rs index 55a96a5c68a..82c49d806b5 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -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 diff --git a/src/visitor.rs b/src/visitor.rs index 27e4f6543b0..346628c0b89 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -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));