How to disable unused code warnings in Rust?

WarningsCompiler WarningsRustDead Code

Warnings Problem Overview


struct SemanticDirection;

fn main() {}

warning: struct is never used: `SemanticDirection`
 --> src/main.rs:1:1
  |
1 | struct SemanticDirection;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

I will turn these warnings back on for anything serious, but I am just tinkering with the language and this is driving me bats.

I tried adding #[allow(dead_code)] to my code, but that did not work.

Warnings Solutions


Solution 1 - Warnings

You can either:

  • Add an allow attribute on a struct, module, function, etc.:

      #[allow(dead_code)]
      struct SemanticDirection;
    
  • Add a crate-level allow attribute; notice the !:

      #![allow(dead_code)]
    
  • Pass it to rustc:

      rustc -A dead_code main.rs
    
  • Pass it using cargo via the RUSTFLAGS environment variable:

      RUSTFLAGS="$RUSTFLAGS -A dead_code" cargo build
    

Solution 2 - Warnings

Another way to disable this warning is to prefix the identifier by _:

struct _UnusedStruct {
    _unused_field: i32,
}

fn main() {
    let _unused_variable = 10;
}

This can be useful, for instance, with an SDL window:

let _window = video_subsystem.window("Rust SDL2 demo", 800, 600);

Prefixing with an underscore is different from using a lone underscore as the name. Doing the following will immediately destroy the window, which is unlikely to be the intended behavior.

let _ = video_subsystem.window("Rust SDL2 demo", 800, 600);

Solution 3 - Warnings

Making the code public also stops the warnings; you'll need to make the enclosing mod's public too.

This makes sense when you're writing a library: your code is "unused" internally because it's intended to be used by client code.

Solution 4 - Warnings

Put these two lines on the top of the file.

#![allow(dead_code)]
#![allow(unused_variables)]

Solution 5 - Warnings

also as an addition: rust provides four levels of lints (allow, warn, deny, forbid).

https://doc.rust-lang.org/rustc/lints/levels.html#lint-levels

Solution 6 - Warnings

You can always disable unused variables/functions by adding an (_) to the variable name, like so:

let _variable = vec![0; 10];

Solution 7 - Warnings

For unused functions, you should make the function public, but watch out. If the struct isn't public, then you'll still get the error as in here:

//this should be public also
struct A{
   A{}
}

impl A {
    pub fn new() -> A {

    }
}

Or if you don't want it to be public, you should put #[allow(unused)]

Solution 8 - Warnings

using features

#[cfg(feature = "dead_code")]

note: "dead_code" can be replaced by any word.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew WagnerView Question on Stackoverflow
Solution 1 - WarningsArjanView Answer on Stackoverflow
Solution 2 - WarningsantoyoView Answer on Stackoverflow
Solution 3 - WarningsVictor BassoView Answer on Stackoverflow
Solution 4 - WarningsM. Hamza RajputView Answer on Stackoverflow
Solution 5 - WarningsMuhammed MoussaView Answer on Stackoverflow
Solution 6 - WarningscrimsondamaskView Answer on Stackoverflow
Solution 7 - WarningsPPPView Answer on Stackoverflow
Solution 8 - WarningsjmzhouView Answer on Stackoverflow