Search code examples
rustrust-cargo

Is it possible to permanently / system wide disable non snake case warning emitted by rust compiler?


I am aware of #![allow(non_snake_case)] but this only disables the warning for that specific file/project.

How to disable this warning system wide?

I'm a beginner learning rust and warnings like this on every single recompilation is unhelpful and annoying. I mean variable names being non snake case is the least of my worries when I am struggling to do basic things, precious screen space being used up by useless warnings is just not right.


Solution

  • Personally speaking, I think you should not to it. This stated, to disable non-snake case warnings system-wide in Rust, you can use a more general approach that involves setting a default configuration for the Rust compiler (rustc) through environment variables or a global configuration file. Unfortunately, Rust does not support a simple configuration file like some other languages do, but you can work around this by setting up your environment or build scripts to consistently apply this setting across all your projects.

    Here are a few methods to consider:

    1. Using RUSTFLAGS Environment Variable

    You can set the RUSTFLAGS environment variable in your shell to include the directive to allow non-snake case globally. This can be done by adding the following line to your shell configuration file (like .bashrc):

    export RUSTFLAGS="-A non_snake_case"
    

    This will append the flag to disable the non_snake_case lint every time Rust code is compiled in your shell session. Note that this will affect all Rust projects compiled in this environment. Of course, not ideal when you have to change it in your CI or on each machine.

    2. Cargo Configuration

    If you prefer a Cargo-based solution that doesn't affect the global environment, you can configure Cargo to globally ignore certain lints by creating a .cargo/config.toml file in your home directory (or the appropriate configuration directory on your system) with the following content:

    [build]
    rustflags = ["-A non_snake_case"]
    

    This will instruct Cargo to pass the -A non_snake_case argument to rustc every time a Cargo project is built on your system. This method is specific to Cargo and will not affect Rust code compiled without Cargo.