Skip to content

Commit 9b79cb5

Browse files
authored
Merge pull request #1602 from topecongiro/attributes-on-expressions
Allow attributes on expressions
2 parents d82962d + 99c2eab commit 9b79cb5

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

src/expr.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTacti
2323
use string::{StringFormat, rewrite_string};
2424
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
2525
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
26-
colon_spaces};
26+
colon_spaces, contains_skip};
2727
use visitor::FmtVisitor;
2828
use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
2929
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
@@ -53,7 +53,11 @@ fn format_expr(expr: &ast::Expr,
5353
context: &RewriteContext,
5454
shape: Shape)
5555
-> Option<String> {
56-
let result = match expr.node {
56+
if contains_skip(&*expr.attrs) {
57+
return Some(context.snippet(expr.span));
58+
}
59+
let attr_rw = (&*expr.attrs).rewrite(context, shape);
60+
let expr_rw = match expr.node {
5761
ast::ExprKind::Array(ref expr_vec) => {
5862
rewrite_array(expr_vec.iter().map(|e| &**e),
5963
mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi),
@@ -251,7 +255,16 @@ fn format_expr(expr: &ast::Expr,
251255
shape)
252256
}
253257
};
254-
result.and_then(|res| recover_comment_removed(res, expr.span, context, shape))
258+
match (attr_rw, expr_rw) {
259+
(Some(attr_str), Some(expr_str)) => {
260+
let space = if attr_str.is_empty() { "" } else { " " };
261+
recover_comment_removed(format!("{}{}{}", attr_str, space, expr_str),
262+
expr.span,
263+
context,
264+
shape)
265+
}
266+
_ => None,
267+
}
255268
}
256269

257270
pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,

tests/source/macros.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,11 @@ fn issue_1555() {
103103
"65454654654654654654654655464",
104104
"4");
105105
}
106+
107+
fn issue1178() {
108+
macro_rules! foo {
109+
(#[$attr:meta] $name:ident) => {}
110+
}
111+
112+
foo!(#[doc = "bar"] baz);
113+
}

tests/source/skip.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ impl LateLintPass for UsedUnderscoreBinding {
1616
fn check_expr() { // comment
1717
}
1818
}
19+
20+
fn issue1346() {
21+
#[cfg_attr(rustfmt, rustfmt_skip)]
22+
Box::new(self.inner.call(req).then(move |result| {
23+
match result {
24+
Ok(resp) => Box::new(future::done(Ok(resp))),
25+
Err(e) => {
26+
try_error!(clo_stderr, "{}", e);
27+
Box::new(future::err(e))
28+
}
29+
}
30+
}))
31+
}

tests/target/macros.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,11 @@ fn issue_1555() {
108108
"65454654654654654654654655464",
109109
"4");
110110
}
111+
112+
fn issue1178() {
113+
macro_rules! foo {
114+
(#[$attr:meta] $name:ident) => {}
115+
}
116+
117+
foo!(#[doc = "bar"] baz);
118+
}

tests/target/skip.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ impl LateLintPass for UsedUnderscoreBinding {
1616
fn check_expr() { // comment
1717
}
1818
}
19+
20+
fn issue1346() {
21+
#[cfg_attr(rustfmt, rustfmt_skip)]
22+
Box::new(self.inner.call(req).then(move |result| {
23+
match result {
24+
Ok(resp) => Box::new(future::done(Ok(resp))),
25+
Err(e) => {
26+
try_error!(clo_stderr, "{}", e);
27+
Box::new(future::err(e))
28+
}
29+
}
30+
}))
31+
}

0 commit comments

Comments
 (0)