10

I'm using a library that generates a bunch of code for me, and it's often quite eager in generating methods that I'm not using yet. This results in a bunch of noisy warnings when building my project.

The script generates plain old .rs files throughout my codebase, which I then import and call, much like normal code:

mod autogen_code; 
pub use self::autogen_code::*;

I can't use #![allow(unused_whatever)] on the generated code, because when I rebuild my project, the generation script runs again and any changes would be gone. These files are .gitignore'd, and have big comments at the top saying "This is all auto-generated. Do not touch."

I don't want to allow unused stuff across my whole project, so placing #![allow(unused_whatever)] at the top of my crate is also a non-starter.

The good thing is that the generated files all have a predictable name, so what I'm hoping is that there's a way to tell cargo/rustc not to emit warnings for files matching a particular file name. Is this possible?

Cam Jackson
  • 11,860
  • 8
  • 45
  • 78
  • @Shepmaster The script that generates it places plain old `.rs` files throughout your codebase, which you then import and call, much like normal code. – Cam Jackson Oct 16 '17 at 13:57
  • 1
    I import it like `mod autogen_code; pub use self::autogen_code::*;`. And I mean, technically yes I can open the generated file and edit it, but when I rebuild my project, the generation script runs again and my changes are gone. These files are `.gitignore`'d, and have big comments at the top saying `This is all auto-generated. Do not touch.` – Cam Jackson Oct 16 '17 at 14:02
  • 1
    offload generated code to a new crate? you can have more than one crate in a workspace which may be useful – user25064 Oct 16 '17 at 14:18
  • 2
    If you import it like `mod autogen_code` - simply use `allow` on it? – VP. Oct 16 '17 at 14:29
  • Thanks @VictorPolevoy, I didn't realise I could use `allow` on a `mod`! – Cam Jackson Oct 17 '17 at 03:40

1 Answers1

16

No, you cannot apply lints using a filename pattern.

Instead, you can...

  • Instead of using !#[allow(...)] inside the file, use #[allow(...)] on the mod declaration:

      #[allow(dead_code)]
      mod autogen;
    
  • Modify the generation step to change the files. For example, instead of running library-generator foo.input, run library-generator foo.input && sed -i '' '1s/^/#![allow(whatever)]/' myfile.rs. You can also do this in pure Rust. Since "the generated files all have a predictable name" you could also find all the files this way and perform this transformation. Now the transform will be applied every time you regenerate the files.

  • Modify the generator itself to add an option to either restrict what functions are output or to add the allow setting. If you experience the problem, it's likely others do as well. Might as well fix it for everyone.

  • In certain cases, you can create a "shim" module that does whatever you need to do and then include! the real code. This is kind of nasty though:

      #![allow(...)]
    
      include!("autogen_real.rs");
    
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366