-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
lint for unused crates #4341
Comments
Note that this is different from #3513, about remove |
This might be quite hard to get right because some imported crates might only be used with some features enabled or on some platforms that do not compile for the host machine that runs |
@matthiaskrgr yeah it may not be possible to do perfectly, but if not I would see that as a cargo bug. Cargo can build crates conditionally based on features, but maybe not target platform (not sure), maybe not in other cases I'm not thinking of. Ideally, cargo would not be building any crates that are unused for any particular configuration. If cargo could handle all the cases then this lint would be saying e.g. "you are not using this crate, or you are not writing your manifest correctly to disable this crate in this config". |
fwiw I'm poking at a temporary solution to just add the appropriate "used / unused" logic to rustc and log it as |
Per @ehuss
|
See also rust-lang/rust#57274 |
FYI, while I'd like to have it implemented in upstream tools as well, I have released an independent tool that does such an unused crates lint. |
Thanks @est31 ! |
This already exists in rustc, in the form of the unused_crate_dependencies lint: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#unused-crate-dependencies This issue should probably be closed? |
@roblabla that lint is allow by default because it doesn't work well with how cargo offers crates to rustc. That is, each cargo package has multiple crates, and each crate can use different dependencies. One example is main.rs and lib.rs in the same package. Another one is crates used by dev dependencies, as a dev dependency can e.g. only be used by doctests. Giving good error messages in all these instances basically requires this to be handled in cargo. I had a PR open but it's blocked by a cargo mode that at least compiles everything that can use dev dependencies in one run, so that cargo can then turn on the unused dependencies check for dev dependencies too: rust-lang/cargo#8437 |
It's pretty common for crates listed in
Cargo.toml
to become unused over time. I don't know of a tool that can figure this out, but it definitely needs the help of rustc. Clippy is in a position to figure this out. rustc should be able to track whether any names from a crate explicitly loaded by rustc are actually used and report when none are.cc @siddontang
The text was updated successfully, but these errors were encountered: