-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unused_crate_dependencies
false-positives in Cargo packages with muliple Cargo Targets (lib, bins, examples, tests, benches)
#95513
Comments
Thanks for the report! This is a known issue, as rustc does not know about cargo projects or their structure. #57274 contains more discussion of the issue and its problems. As an alternative, you may want to consider using cargo udeps. |
@ehuss Thanks for linking that! I wouldn't have thought to look at that issue as it asks for implementing this feature in the first place. It seems non-trivial anyway given how the user can dictate what targets to build, and this issue only seems to be solvable if the usage results from all targets can be aggregated and combined. For future lurkers, to save you some scrolling and searching, the exact comment that describes this is #57274 (comment) @ehuss Is it worth keeping this issue open as a more explicit tracking place for this particular problem or do you think the mention in #57274 is enough? I'll check out For now though, as a workaround, I opted to run this lint exclusively on |
I think it would be fine to close this. I think the same solution for #57274 would cover this, and I don't see a way to address this particular situation separately. |
This lint doesn't work with multiple targets (in the case of prepare-worker, the bench-only dependencies were messing it up). See: - rust-lang/rust#95513 - rust-lang/rust#57274 (comment)
Closing in favor of #57274 and rust-lang/cargo#1982. If you disagree with my assessment I can of course reopen this issue. |
@fmease neither of those issues seem to describe this problem. #57274 describes implementing this lint in the first place (which already happened now in the form of rust-lang/cargo#1982 describes a way to configure per-target dependencies. While such a feature likely impacts how this lint is implemented, and could implicitly solve this issue, it is of far wider scope than this issue. To repeat, this issue describes false positives from the EDIT: You could however close this as a "WONTFIX", anticipating rust-lang/cargo#1982 to be a likable alternative instead. |
With rust-lang/rfcs#2887 closed, let's reopen this one. |
This also triggers on examples, benches and integration tests from what I can tell. And bin+lib. In fact the lint seems buggy on anything that isn't a single target crate of a single type. |
…spective crates (#597) * Disable `unused_crate_dependencies` lint as it is utterly broken See rust-lang/rust#95513 * Move driver tests into the `driver` crate * Move vote keeper tests into the `vote` crate * Move full proposal keeper tests into the `consensus` crate --------- Co-authored-by: Anca Zamfir <[email protected]>
This is especially noisy when applied to examples. I think it would make sense to exclude example crates from this lint completely. |
The problems of the lint are obvious. Indeed, it would be more than amazing if unused-crate-dependencies didn't misfire in all of the aforementioned cases. However, the underlying issue is technical in nature (as opposed to social) and non-trivial at that: Simply said,
From the perspective of rustc (which Cargo invokes) " A very similar situation arises for unit tests and unit benches (which are powered by conditional compilation). From the perspective of rustc, the "same" crate "instantiated" by / under different |
The concept of "example crates" is nonexistent in Rust / rustc! It's a Cargo concept. If we wanted to go forward with this short-term solution you proposed, it would be Cargo's job to pass Or expose something like: [target.examples.lints.rust]
unused_crate_dependencies = "allow" # ofc, this wouldn't work if you also had `[lints.rust]↵unused_crate_dependencies = "forbid"` in your manifest |
unused_crate_dependencies
false-positives in crates with muliple (lib/bin) targetsunused_crate_dependencies
false-positives in Cargo packages with muliple Cargo Targets (lib, bins, examples, tests, benches)
I tried this code:
Consider a crate setup with the
clap
dependency, whereclap
is used only by thelib
target:Cargo.toml
:src/lib.rs
:This is fine when checking with this lint enabled:
$ RUSTFLAGS=-Wunused-crate-dependencies cargo c --all --all-targets
Now, consider adding multiple targets to this crate. For the sake of demonstration, we use a
test
andbin
target that both utilize the library crate defined above:src/main.rs
:tests/test.rs
:We run the same
check
command again:$ RUSTFLAGS=-Wunused-crate-dependencies cargo c --all --all-targets
I expected to see this happen:
No
external crate `clap` unused
warnings 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
git
repository, and we use a single.cargo/config.toml
in 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.rs
bits 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
:The text was updated successfully, but these errors were encountered: