0

I have a Rust project folder structure that contains an executable and a shared C-compatible library that are both build using the same sources. The Cargo.toml manifest file looks like:

[package]
name = "foo-bar"
version = "0.1.0"
authors = ...

[lib]
name = "foo_bar"
crate-type = ["rlib", "cdylib"]

[[bin]]
name = "foo-bar"
test = false
doc = false

[dependencies]
...

As the executable is not using all of the code I get some "unused code" warnings when building the project with cargo build. I could add #[allow(dead_code)] lints all over my source code where necessary but that would disable them also when building the library target.

Is there a way to globally disable the "dead_code" lint only when compiling the (feature-wise smaller) bin executable target but having it enabled for the lib target?

blerontin
  • 2,892
  • 5
  • 34
  • 60
  • What if you add `#![allow(dead_code)]` at the begining of your bin project? – Boiethios Mar 11 '19 at 09:27
  • That just does does the trick! I had assumed that it would only disable the lint for the current file (main.rs), but apparently it also applies to all included files. @FrenchBoiethios Please add an answer with the solution so I can mark it as accepted. – blerontin Mar 11 '19 at 09:34
  • How are you using the library from your executable? You _shouldn't_ be getting dead code warnings for items in your library if you reference it via `extern crate`. – Francis Gagné Mar 11 '19 at 10:12
  • As both targets live within the same directory I just include the needed modules and traits via `mod foo; use crate::foo::FooBar` into `main.rs`. The `lib.rs` file contains only publishing functions for the C-style library. – blerontin Mar 11 '19 at 10:39

1 Answers1

1

You can modify a lint for a whole crate by putting an attribute with #! at the beginning of the crate:

main.rs:

#![allow(dead_code)]

// etc.
Boiethios
  • 38,438
  • 19
  • 134
  • 183