Skip to content

Support rustfmt_skip on statements #1811

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 2 commits into from
Jul 22, 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
24 changes: 16 additions & 8 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_ass
use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorTactic};
use macros::{rewrite_macro, MacroPosition};
use rewrite::{Rewrite, RewriteContext};
use utils::{self, mk_sp};
use utils::{self, contains_skip, mk_sp};

fn is_use_item(item: &ast::Item) -> bool {
match item.node {
Expand Down Expand Up @@ -79,11 +79,15 @@ impl<'a> FmtVisitor<'a> {
ast::StmtKind::Item(ref item) => {
self.visit_item(item);
}
ast::StmtKind::Local(..) => {
let rewrite = stmt.rewrite(
&self.get_context(),
Shape::indented(self.block_indent, self.config),
);
ast::StmtKind::Local(ref local) => {
let rewrite = if contains_skip(&local.attrs) {
None
} else {
stmt.rewrite(
&self.get_context(),
Shape::indented(self.block_indent, self.config),
)
};
self.push_rewrite(stmt.span, rewrite);
}
ast::StmtKind::Expr(ref expr) => {
Expand Down Expand Up @@ -113,8 +117,12 @@ impl<'a> FmtVisitor<'a> {
self.push_rewrite(span, rewrite)
}
ast::StmtKind::Mac(ref mac) => {
let (ref mac, _macro_style, _) = **mac;
self.visit_mac(mac, None, MacroPosition::Statement);
let (ref mac, _macro_style, ref attrs) = **mac;
if contains_skip(attrs) {
self.push_rewrite(mac.span, None);
} else {
self.visit_mac(mac, None, MacroPosition::Statement);
}
self.format_missing(stmt.span.hi);
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/source/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,37 @@ fn issue1346() {
}
}))
}

fn skip_on_statements() {
// Semi
#[cfg_attr(rustfmt, rustfmt_skip)]
foo(
1, 2, 3, 4,
1, 2,
1, 2, 3,
);

// Local
#[cfg_attr(rustfmt, rustfmt_skip)]
let x = foo( a, b , c);

// Item
#[cfg_attr(rustfmt, rustfmt_skip)]
use foobar ;

// Mac
#[cfg_attr(rustfmt, rustfmt_skip)]
vec![
1, 2, 3, 4,
1, 2, 3, 4,
1, 2, 3, 4,
1, 2, 3,
1,
1, 2,
1,
];

// Expr
#[cfg_attr(rustfmt, rustfmt_skip)]
foo( a, b , c)
}
34 changes: 34 additions & 0 deletions tests/target/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,37 @@ fn issue1346() {
}
}))
}

fn skip_on_statements() {
// Semi
#[cfg_attr(rustfmt, rustfmt_skip)]
foo(
1, 2, 3, 4,
1, 2,
1, 2, 3,
);

// Local
#[cfg_attr(rustfmt, rustfmt_skip)]
let x = foo( a, b , c);

// Item
#[cfg_attr(rustfmt, rustfmt_skip)]
use foobar ;

// Mac
#[cfg_attr(rustfmt, rustfmt_skip)]
vec![
1, 2, 3, 4,
1, 2, 3, 4,
1, 2, 3, 4,
1, 2, 3,
1,
1, 2,
1,
];

// Expr
#[cfg_attr(rustfmt, rustfmt_skip)]
foo( a, b , c)
}