From 980ce8aed4ea0c89ccc41bb1cd41e1d382af65e5 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 18 Nov 2024 09:58:11 -0800 Subject: [PATCH] Avoid emitting redundant `#[allow(dead_code)]` directives. Only emit `#[allow(dead_code)]` on top-level modules, and emit `#[rustfmt::skip]` on all top-level modules. --- crates/rust/src/interface.rs | 1 - crates/rust/src/lib.rs | 30 ++++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/crates/rust/src/interface.rs b/crates/rust/src/interface.rs index 8a7468960..ba42bbff0 100644 --- a/crates/rust/src/interface.rs +++ b/crates/rust/src/interface.rs @@ -439,7 +439,6 @@ macro_rules! {macro_name} {{ }; let module = format!( "\ - #[allow(dead_code, clippy::all)] pub mod {snake} {{ {used_static} {module} diff --git a/crates/rust/src/lib.rs b/crates/rust/src/lib.rs index 8c546c1fd..2d7f71e5b 100644 --- a/crates/rust/src/lib.rs +++ b/crates/rust/src/lib.rs @@ -293,23 +293,25 @@ 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) { + emit(&mut self.src, map, &self.opts, true); + fn emit(me: &mut Source, module: Module, opts: &Opts, toplevel: bool) { for (name, submodule) in module.submodules { - // Ignore dead-code warnings. If the bindings are only used - // within a crate, and not exported to a different crate, some - // parts may be unused, and that's ok. - uwriteln!(me, "#[allow(dead_code)]"); + if toplevel { + // 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 opts.format { + uwriteln!(me, "#[rustfmt::skip]"); + } + + // Ignore dead-code and clippy warnings. If the bindings are + // only used within a crate, and not exported to a different + // crate, some parts may be unused, and that's ok. + uwriteln!(me, "#[allow(dead_code, clippy::all)]"); + } uwriteln!(me, "pub mod {name} {{"); - emit(me, submodule); + emit(me, submodule, opts, false); uwriteln!(me, "}}"); } for submodule in module.contents {