Allow attributes on expressions

This commit is contained in:
topecongiro 2017-05-28 11:41:16 +09:00
parent 8ac3fc36cc
commit 99c2eab5ac
5 changed files with 58 additions and 3 deletions

View File

@ -23,7 +23,7 @@ use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTacti
use string::{StringFormat, rewrite_string};
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
colon_spaces};
colon_spaces, contains_skip};
use visitor::FmtVisitor;
use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
@ -53,7 +53,11 @@ fn format_expr(expr: &ast::Expr,
context: &RewriteContext,
shape: Shape)
-> Option<String> {
let result = match expr.node {
if contains_skip(&*expr.attrs) {
return Some(context.snippet(expr.span));
}
let attr_rw = (&*expr.attrs).rewrite(context, shape);
let expr_rw = match expr.node {
ast::ExprKind::Array(ref expr_vec) => {
rewrite_array(expr_vec.iter().map(|e| &**e),
mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi),
@ -251,7 +255,16 @@ fn format_expr(expr: &ast::Expr,
shape)
}
};
result.and_then(|res| recover_comment_removed(res, expr.span, context, shape))
match (attr_rw, expr_rw) {
(Some(attr_str), Some(expr_str)) => {
let space = if attr_str.is_empty() { "" } else { " " };
recover_comment_removed(format!("{}{}{}", attr_str, space, expr_str),
expr.span,
context,
shape)
}
_ => None,
}
}
pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,

View File

@ -103,3 +103,11 @@ fn issue_1555() {
"65454654654654654654654655464",
"4");
}
fn issue1178() {
macro_rules! foo {
(#[$attr:meta] $name:ident) => {}
}
foo!(#[doc = "bar"] baz);
}

View File

@ -16,3 +16,16 @@ impl LateLintPass for UsedUnderscoreBinding {
fn check_expr() { // comment
}
}
fn issue1346() {
#[cfg_attr(rustfmt, rustfmt_skip)]
Box::new(self.inner.call(req).then(move |result| {
match result {
Ok(resp) => Box::new(future::done(Ok(resp))),
Err(e) => {
try_error!(clo_stderr, "{}", e);
Box::new(future::err(e))
}
}
}))
}

View File

@ -108,3 +108,11 @@ fn issue_1555() {
"65454654654654654654654655464",
"4");
}
fn issue1178() {
macro_rules! foo {
(#[$attr:meta] $name:ident) => {}
}
foo!(#[doc = "bar"] baz);
}

View File

@ -16,3 +16,16 @@ impl LateLintPass for UsedUnderscoreBinding {
fn check_expr() { // comment
}
}
fn issue1346() {
#[cfg_attr(rustfmt, rustfmt_skip)]
Box::new(self.inner.call(req).then(move |result| {
match result {
Ok(resp) => Box::new(future::done(Ok(resp))),
Err(e) => {
try_error!(clo_stderr, "{}", e);
Box::new(future::err(e))
}
}
}))
}