Is there a config or attribute for `rustfmt` to only format but not modify my code?

to test a procedural macro, I creates this helper macro:

macro_rules! check_derive_expansion {
	 ($input:tt, $expected_output: tt) => {
		  let input = quote! $input;
		  let output = derive_foo(input);
		  assert_eq!(output.to_string(), quote! $expected_output . to_string());
	 };
}

but rustfmt keeps modifying my code in the macro invocaiton. for example, with this code:

#[test]
fn simple() {
	 check_derive_expansion!(
	 	 {
	 	 	 struct Foo(i32)
	 	 },
	 	 {
	 	 	 impl<> ::my_crate::Foo for Foo<> {}
	 	 }
	 );
}

if I run cargo fmt, the empty angle brackets got removed:

// this line
impl<> ::my_crate::Foo for Foo<> {}
// turns into this:
impl ::my_crate::Foo for Foo {}

I don't want to use #[rustfmt::skip] since I still want the basic formatting done automatically, like indentation, is there a config or attribute I can use for this? or is there a trick to change the helper macro to prevent rustfmt from modifying the code?

btw, I noticed rustfmt will NOT modify code inside quote! {} without my helper check_derive_expansion, so it must have something to do with the way my helper macro is defined.

I can’t reproduce this behavior by copying your code into a local project or Rust Playground.

That said, I believe rustfmt will typically not format macros that are called with braces foo! {} rather than parentheses foo!().

There is also an unstable option skip_macro_invocations which can be used to disable formatting for specific macro names.

your playground link is blank, but I do see the same behavior on the playground as my local machine.

thanks for the tip, I just checked, it is true, I think that's also the reason the quote!{} macro is not formatted.

however, it disables the formatting entirely, like, the indentations will not be formatted either.

More details.

I think a whitespace only mode would be great, but based on the design doc and this closed issue, I'm not holding my breath. So maybe your best bet is a future option that preserves empty <>s (though that also seems a bit far-fetched?).[1]


  1. Related issue, albeit in a different syntactical context. ↩︎

yeah, I only really need a quick, convenient "fix indentation", if would be great if rustfmt can support this, since it's tight integration with the toolings.

for now, I think I'll just disable the formatting of the macro invocation. bummer, but not a big deal.