Cargo Sheer
Detect and remove unused dependencies from Cargo.toml in Rust projects.
Technique
- use the
cargo_metadatacrate to list all dependencies specified in[workspace.dependencies]and[dependencies] - iterate through all package targets to locate all Rust files
- use
synto parse these Rust files and extract imports - identify the difference between the imports and the package dependencies
False positives (to be solved):
- macros
- how can imports be collected from macros? e.g. in
println!({}, foo::bar),foois a macro token instead of an identifier - can we run some other command and get the macro expanded source to parse?
- is there an API for getting imports instead of parsing?
- how can imports be collected from macros? e.g. in
TODO
- make the reporting more granular for
[dependencies],[dev-dependencies]and[build-dependencies] -
--fix - add tests
- exit codes
- error recovery
- print things more nicely