Skip to content

Commit 83b0fec

Browse files
authored
Merge pull request #1811 from topecongiro/rustfmt-skip-on-stmt
Support rustfmt_skip on statements
2 parents bb27bc5 + d7a57d3 commit 83b0fec

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

src/visitor.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_ass
2626
use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorTactic};
2727
use macros::{rewrite_macro, MacroPosition};
2828
use rewrite::{Rewrite, RewriteContext};
29-
use utils::{self, mk_sp};
29+
use utils::{self, contains_skip, mk_sp};
3030

3131
fn is_use_item(item: &ast::Item) -> bool {
3232
match item.node {
@@ -79,11 +79,15 @@ impl<'a> FmtVisitor<'a> {
7979
ast::StmtKind::Item(ref item) => {
8080
self.visit_item(item);
8181
}
82-
ast::StmtKind::Local(..) => {
83-
let rewrite = stmt.rewrite(
84-
&self.get_context(),
85-
Shape::indented(self.block_indent, self.config),
86-
);
82+
ast::StmtKind::Local(ref local) => {
83+
let rewrite = if contains_skip(&local.attrs) {
84+
None
85+
} else {
86+
stmt.rewrite(
87+
&self.get_context(),
88+
Shape::indented(self.block_indent, self.config),
89+
)
90+
};
8791
self.push_rewrite(stmt.span, rewrite);
8892
}
8993
ast::StmtKind::Expr(ref expr) => {
@@ -113,8 +117,12 @@ impl<'a> FmtVisitor<'a> {
113117
self.push_rewrite(span, rewrite)
114118
}
115119
ast::StmtKind::Mac(ref mac) => {
116-
let (ref mac, _macro_style, _) = **mac;
117-
self.visit_mac(mac, None, MacroPosition::Statement);
120+
let (ref mac, _macro_style, ref attrs) = **mac;
121+
if contains_skip(attrs) {
122+
self.push_rewrite(mac.span, None);
123+
} else {
124+
self.visit_mac(mac, None, MacroPosition::Statement);
125+
}
118126
self.format_missing(stmt.span.hi);
119127
}
120128
}

tests/source/skip.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,37 @@ fn issue1346() {
2929
}
3030
}))
3131
}
32+
33+
fn skip_on_statements() {
34+
// Semi
35+
#[cfg_attr(rustfmt, rustfmt_skip)]
36+
foo(
37+
1, 2, 3, 4,
38+
1, 2,
39+
1, 2, 3,
40+
);
41+
42+
// Local
43+
#[cfg_attr(rustfmt, rustfmt_skip)]
44+
let x = foo( a, b , c);
45+
46+
// Item
47+
#[cfg_attr(rustfmt, rustfmt_skip)]
48+
use foobar ;
49+
50+
// Mac
51+
#[cfg_attr(rustfmt, rustfmt_skip)]
52+
vec![
53+
1, 2, 3, 4,
54+
1, 2, 3, 4,
55+
1, 2, 3, 4,
56+
1, 2, 3,
57+
1,
58+
1, 2,
59+
1,
60+
];
61+
62+
// Expr
63+
#[cfg_attr(rustfmt, rustfmt_skip)]
64+
foo( a, b , c)
65+
}

tests/target/skip.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,37 @@ fn issue1346() {
2929
}
3030
}))
3131
}
32+
33+
fn skip_on_statements() {
34+
// Semi
35+
#[cfg_attr(rustfmt, rustfmt_skip)]
36+
foo(
37+
1, 2, 3, 4,
38+
1, 2,
39+
1, 2, 3,
40+
);
41+
42+
// Local
43+
#[cfg_attr(rustfmt, rustfmt_skip)]
44+
let x = foo( a, b , c);
45+
46+
// Item
47+
#[cfg_attr(rustfmt, rustfmt_skip)]
48+
use foobar ;
49+
50+
// Mac
51+
#[cfg_attr(rustfmt, rustfmt_skip)]
52+
vec![
53+
1, 2, 3, 4,
54+
1, 2, 3, 4,
55+
1, 2, 3, 4,
56+
1, 2, 3,
57+
1,
58+
1, 2,
59+
1,
60+
];
61+
62+
// Expr
63+
#[cfg_attr(rustfmt, rustfmt_skip)]
64+
foo( a, b , c)
65+
}

0 commit comments

Comments
 (0)