-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
unused_crate_dependencies false-positives in Cargo packages with muliple Cargo Targets (lib, bins, examples, tests, benches) #95513
Copy link
Copy link
Open
Labels
A-cratesArea: Crates and their interactions (like crate loading)Area: Crates and their interactions (like crate loading)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.Category: This is a bug.L-false-positiveLint: False positive (should not have fired).Lint: False positive (should not have fired).L-unused_crate_dependenciesLint: unused_crate_dependenciesLint: unused_crate_dependenciesT-cargoRelevant to the cargo team, which will review and decide on the PR/issue.Relevant to the cargo team, which will review and decide on the PR/issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-cratesArea: Crates and their interactions (like crate loading)Area: Crates and their interactions (like crate loading)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.Category: This is a bug.L-false-positiveLint: False positive (should not have fired).Lint: False positive (should not have fired).L-unused_crate_dependenciesLint: unused_crate_dependenciesLint: unused_crate_dependenciesT-cargoRelevant to the cargo team, which will review and decide on the PR/issue.Relevant to the cargo team, which will review and decide on the PR/issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
I tried this code:
Consider a crate setup with the
clapdependency, whereclapis used only by thelibtarget:Cargo.toml:src/lib.rs:This is fine when checking with this lint enabled:
$ RUSTFLAGS=-Wunused-crate-dependencies cargo c --all --all-targetsNow, consider adding multiple targets to this crate. For the sake of demonstration, we use a
testandbintarget that both utilize the library crate defined above:src/main.rs:tests/test.rs:We run the same
checkcommand again:$ RUSTFLAGS=-Wunused-crate-dependencies cargo c --all --all-targetsI expected to see this happen:
No
external crate `clap` unusedwarnings show up.Instead, this happened:
Every target seems to be checked individually against the set of defined crate dependencies, instead of identifying dependencies that are not used by all targets combined (or at the very least lib+bins, as test-only dependencies should reside under
dev-dependencies). As such, both the test and binary targets above are accused of not usingclap:For the most part, especially larger library crates, this makes it impossible to write clean test binaries without having the top half filled with
use {dep_a as _, dep_b as _, ...};.Note that our project currently comprises 92 crates in a single
gitrepository, and we use a single.cargo/config.tomlin the workspace root to configure lints all at once. It is completely infeasible for us to duplicate these to every existing and up and coming crate, nor keep them in sync after the fact. As such, copy-pasting#[warn(unused-crate-dependencies)]across just thelib.rs/main.rsbits of our code base (where only a single target is used) is pretty much impossible.EDIT: I can understand this warning to show up for the binary (test) targets if they do not reference the library target, but it is particularly confusing when these binary targets at least transitively use the dependency through the library target.
Meta
rustc --version --verbose: