Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable warnings in rust dependencies

Tags:

rust

When I compile my code with cargo, I get warnings from code in dependencies. This is not my code and I will not be fixing it. These warnings are noise which could hide problems in my own code.

How do I disable all of these warnings, in included crates and their dependencies, but not disable warnings in my own code?

The following does not work:

#[allow(warnings)]
extern crate foo;

Perhaps it works on foo itself, but not on its dependencies.

A minimal example follows:

$ find rust-test/
rust-test/
rust-test/b
rust-test/b/Cargo.toml
rust-test/b/src
rust-test/b/src/lib.rs
rust-test/mine
rust-test/mine/Cargo.toml
rust-test/mine/src
rust-test/mine/src/main.rs
rust-test/a
rust-test/a/Cargo.toml
rust-test/a/src
rust-test/a/src/lib.rs

"mine" is the code I am working on. a and b are libraries that generate warnings.

$ cat Cargo.toml 
[package]
name = "mine"

[dependencies]
a = {path = "../a"}

$ cat a/Cargo.toml 
[package]
name = "a"

[dependencies]
b = {path = "../b"}

$ cat b/Cargo.toml 
[package]
name = "b"

In rust-test/mine:

$ cargo b
warning: no edition set: defaulting to the 2015 edition while the latest is 2024
warning: function `test_b` is never used
 --> /home/anon/rust-test/b/src/lib.rs:1:4
  |
1 | fn test_b() -> () {}
  |    ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `b` (lib) generated 1 warning
warning: function `test_a` is never used
 --> /home/anon/rust-test/a/src/lib.rs:1:4
  |
1 | fn test_a() -> () {}
  |    ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `a` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s

The source files contain only the unused functions warned about.

like image 695
river Avatar asked Oct 31 '25 14:10

river


1 Answers

When I compile my code with cargo, I get warnings from code in dependencies. This is not my code and I will not be fixing it. These warnings are noise which could hide problems in my own code.

Cargo already has a rule to automatically hide such warnings for precisely these reasons.

The problem in your case is that the rule is to hide warnings if the dependency is from a remote sourcegit, crates.io, or other registry. When the dependency is specified by path, Cargo assumes that it is code you are maintaining.

Therefore, possible solutions are:

  • Patch the code that you have a path dependency on, adding #[allow(warnings)].
  • Use a git or crates.io dependency instead of path.
  • If the code is already available on crates.io or a Git repository, but you want a local copy of the code to make your build self-contained and not ever require network access, use cargo vendor.
like image 124
Kevin Reid Avatar answered Nov 03 '25 11:11

Kevin Reid