rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.2k stars 12.7k forks source link

`unused_crate_dependencies` false positive for dev-dependencies on main crate #129637

Open joshtriplett opened 2 months ago

joshtriplett commented 2 months ago

Steps to reproduce:

cargo new mycrate
cd mycrate
cargo add --dev termtree
printf '[lints.rust]\nunused_crate_dependencies = "deny"\n' >> Cargo.toml
cargo build --all-targets

Result:

error: external crate `termtree` unused in `mycrate`: remove the dependency or add `use termtree as _;`
  |
  = note: requested on the command line with `-D unused-crate-dependencies`

error: could not compile `mycrate` (bin "mycrate" test) due to 1 previous error

dev-dependencies have a variety of uses, and should not be considered as potentially unused. I'd love to see unused dev-dependencies detected as well, but right now they produce false positives.

The reverse is also true: rustc will warn about unused non-dev dependencies when compiling tests.

Short-term, perhaps we could provide a way to easily disable this warning entirely for dev-dependencies, and for non-dev dependencies in targets such as tests.

Longer-term, perhaps rustc could feed information about potentially unused dependencies back to Cargo in its output, and Cargo could then aggregate the intersection of those dependencies that are completely unused.

Meanwhile, however, these false positives make it difficult to use unused_crate_dependencies in any project that has dev-dependencies or tests.

fmease commented 2 months ago

Very likely dupe of e.g. (edit: now re-opened) #95513. Also CC tangentially related https://github.com/rust-lang/cargo/issues/1982, https://github.com/rust-lang/rfcs/pull/2887

joshtriplett commented 2 months ago

At the time I opened this, that issue was closed. I agree that it's a duplicate.

I don't think we should wait on the possibility of per-target dependencies. Rather, I think the right fix would be to have cargo figure out the intersection of unused dependencies based on information supplied by rustc.