From 0b38ac40d78dc40cd5bbebddb9be2b5b598d7a76 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sat, 22 Jul 2017 11:18:47 +0900 Subject: [PATCH 1/2] Support rustfmt_skip on statements --- src/visitor.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/visitor.rs b/src/visitor.rs index e6e7f038ce7..813f5422f2c 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -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 { @@ -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) => { @@ -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); } } From d7a57d36e7b233937fb7a620f755d244d2518d7b Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sat, 22 Jul 2017 11:18:59 +0900 Subject: [PATCH 2/2] Update tests --- tests/source/skip.rs | 34 ++++++++++++++++++++++++++++++++++ tests/target/skip.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/tests/source/skip.rs b/tests/source/skip.rs index d13c8159039..69c7b53a57b 100644 --- a/tests/source/skip.rs +++ b/tests/source/skip.rs @@ -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) +} diff --git a/tests/target/skip.rs b/tests/target/skip.rs index d13c8159039..69c7b53a57b 100644 --- a/tests/target/skip.rs +++ b/tests/target/skip.rs @@ -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) +}