Skip to content
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 positive for dev-dependencies on main crate #129637

Open
joshtriplett opened this issue Aug 27, 2024 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-unused_crate_dependencies Lint: unused_crate_dependencies T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@joshtriplett
Copy link
Member

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.

@joshtriplett joshtriplett added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. labels Aug 27, 2024
@fmease fmease added the L-unused_crate_dependencies Lint: unused_crate_dependencies label Aug 27, 2024
@fmease
Copy link
Member

fmease commented Aug 27, 2024

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

@joshtriplett
Copy link
Member Author

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.

@github-staff github-staff deleted a comment from mayank785 Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-unused_crate_dependencies Lint: unused_crate_dependencies T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants