Skip to content

Allow attributes on expressions #1602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions tests/source/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,11 @@ fn issue_1555() {
"65454654654654654654654655464",
"4");
}

fn issue1178() {
macro_rules! foo {
(#[$attr:meta] $name:ident) => {}
}

foo!(#[doc = "bar"] baz);
}
13 changes: 13 additions & 0 deletions tests/source/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}))
}
8 changes: 8 additions & 0 deletions tests/target/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,11 @@ fn issue_1555() {
"65454654654654654654654655464",
"4");
}

fn issue1178() {
macro_rules! foo {
(#[$attr:meta] $name:ident) => {}
}

foo!(#[doc = "bar"] baz);
}
13 changes: 13 additions & 0 deletions tests/target/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}))
}