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

lint for unused crates #4341

Open
Tracked by #16
brson opened this issue Aug 6, 2019 · 10 comments
Open
Tracked by #16

lint for unused crates #4341

brson opened this issue Aug 6, 2019 · 10 comments
Labels
A-lint Area: New lints T-cargo Type: cargo related

Comments

@brson
Copy link

brson commented Aug 6, 2019

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

@brson
Copy link
Author

brson commented Aug 6, 2019

Note that this is different from #3513, about remove extern crate statements, but might also properly account for extern crate statements from which no names are resolved.

@flip1995 flip1995 added A-lint Area: New lints T-cargo Type: cargo related labels Aug 6, 2019
@matthiaskrgr
Copy link
Member

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 cargo clippy... 🤔

@brson
Copy link
Author

brson commented Aug 6, 2019

@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".

@brson
Copy link
Author

brson commented Aug 6, 2019

fwiw I'm poking at a temporary solution to just add the appropriate "used / unused" logic to rustc and log it as info!, so it can be scripted together with cargo tree as a rough heuristic. Not sure how far I'll get there.

@brson
Copy link
Author

brson commented Aug 6, 2019

Per @ehuss

@brson I'm not sure if it would help, but there is a new -Z binary-dep-depinfo flag which will record which dependencies were actually used in the dep-info file. You can then try to correlate the rlibs with the dependencies in Cargo.toml. It may not be easy, but at least rustc does keep track of which crates were actually used.

@est31
Copy link
Member

est31 commented Aug 24, 2019

See also rust-lang/rust#57274

@est31
Copy link
Member

est31 commented Aug 30, 2019

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.

@brson
Copy link
Author

brson commented Aug 31, 2019

Thanks @est31 !

@roblabla
Copy link
Contributor

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?

@est31
Copy link
Member

est31 commented Nov 30, 2021

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints T-cargo Type: cargo related
Projects
None yet
Development

No branches or pull requests

5 participants