From dd15b0eca1ff893b7a7e5de839b5e9c5b8212993 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 15 Nov 2024 20:27:37 -0800 Subject: [PATCH] Add `#[rustfmt::skip]` to disable rustfmt on generated code. (#1088) Add `#[rustfmt::skip]` to generated Rust code when we're using prettyplease, so that users don't see spurious diffs when running `cargo fmt` over their projects. --- crates/rust/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/rust/src/lib.rs b/crates/rust/src/lib.rs index e68994878..8c546c1fd 100644 --- a/crates/rust/src/lib.rs +++ b/crates/rust/src/lib.rs @@ -292,6 +292,14 @@ impl RustWasm { } cur.contents.push(module); } + + // Disable rustfmt. By default we already format the code + // using prettyplease, so we don't want `cargo fmt` to create + // extra diffs for users to deal with. + if self.opts.format { + uwriteln!(self.src, "#[rustfmt::skip]"); + } + emit(&mut self.src, map); fn emit(me: &mut Source, module: Module) { for (name, submodule) in module.submodules { @@ -366,6 +374,12 @@ impl RustWasm { if self.rt_module.is_empty() { return; } + + // As above, disable rustfmt, as we use prettyplease. + if self.opts.format { + uwriteln!(self.src, "#[rustfmt::skip]"); + } + self.src.push_str("mod _rt {\n"); let mut emitted = IndexSet::new(); while !self.rt_module.is_empty() {