Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell the Rust compiler to ignore warnings only on files matching a specific name?

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?

like image 685
Cam Jackson Avatar asked Oct 16 '17 13:10

Cam Jackson


Video Answer


1 Answers

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");
    
like image 112
Shepmaster Avatar answered Oct 20 '22 01:10

Shepmaster