Rust tool find 'dead' dependencies?

Is there a Rust tool that can automatically find 'dead' dependencies in Cargo.toml? I.e. things Cargo.toml adds as a dependency, possibly imported / use crate_name::* in src/*.rs, but never actually used by any code ?

The unused_crate_dependencies lint can do this, to a limited extent.

Caveat: it only takes rustc's perspective, not cargo's, so it has a one-crate view of things. This means that a dependency only used by your examples/tests/benches will be a false positive. But, if you enable it temporarily, then you will at least discover candidates for removal.

2 Likes
4 Likes

I've not used this before -- so the newb question. This technique involves adding

#![deny(unused_crate_dependencies)]

to the top of src/lib.rs , followed by 'cargo check' ?

Generally yes. I wouldn't recommend using deny instead of warn, though, because that means it's aborting the compilation which in principle might get you a less complete answer (though I don't think it will in this situation).

It's also not actually necessary to modify the file. You can run rustc with warning options:

cargo rustc -- -W unused_crate_dependencies

This is useful if there are any false positives, so you can run it as a special case but not get the warnings usually, without modifying the file and undoing.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.