Skip to content

Properly skip macros in impl and trait blocks #4209

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 27, 2020
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
6 changes: 6 additions & 0 deletions rustfmt-core/rustfmt-lib/src/formatting/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.push_skipped_with_span(ti.attrs.as_slice(), ti.span, ti.span);
return;
}
let skip_context_outer = self.skip_context.clone();
self.skip_context.update_with_attrs(&ti.attrs);

match ti.kind {
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
Expand Down Expand Up @@ -640,6 +642,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
}
}
self.skip_context = skip_context_outer;
}

pub(crate) fn visit_impl_item(&mut self, ii: &ast::AssocItem) {
Expand All @@ -649,6 +652,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.push_skipped_with_span(ii.attrs.as_slice(), ii.span, ii.span);
return;
}
let skip_context_outer = self.skip_context.clone();
self.skip_context.update_with_attrs(&ii.attrs);

match ii.kind {
ast::AssocItemKind::Fn(defaultness, ref sig, ref generics, Some(ref body)) => {
Expand Down Expand Up @@ -700,6 +705,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
}
}
self.skip_context = skip_context_outer;
}

fn visit_mac(&mut self, mac: &ast::MacCall, ident: Option<symbol::Ident>, pos: MacroPosition) {
Expand Down
47 changes: 47 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/issue-4167.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
impl Type {
#[rustfmt::skip::macros(macros)]
fn example() {
func(
"format me"
);
other!(
"format me"
);
macros!(
"do not format"
);
macros![
"do not format"
];
}
}

impl Trait for Type {
#[rustfmt::skip::macros(macros)]
fn example() {
func(
"format me"
);
other!(
"format me"
);
macros!(
"do not format"
);
}
}

trait Trait {
#[rustfmt::skip::macros(macros)]
fn example() {
func(
"format me"
);
other!(
"format me"
);
macros!(
"do not format"
);
}
}
35 changes: 35 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue-4167.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
impl Type {
#[rustfmt::skip::macros(macros)]
fn example() {
func("format me");
other!("format me");
macros!(
"do not format"
);
macros![
"do not format"
];
}
}

impl Trait for Type {
#[rustfmt::skip::macros(macros)]
fn example() {
func("format me");
other!("format me");
macros!(
"do not format"
);
}
}

trait Trait {
#[rustfmt::skip::macros(macros)]
fn example() {
func("format me");
other!("format me");
macros!(
"do not format"
);
}
}