#unused #cargo #exclude #projects #trick #search #cargo-machete

app cargo-machete-nk

Find unused dependencies with this one weird trick!

6 releases

0.9.0 Apr 12, 2024
0.8.2 Mar 28, 2024
0.7.1 Nov 8, 2023

#334 in Filesystem

Download history 5/week @ 2024-02-18 7/week @ 2024-02-25 1/week @ 2024-03-03 251/week @ 2024-03-10 31/week @ 2024-03-17 117/week @ 2024-03-24 25/week @ 2024-03-31 126/week @ 2024-04-07 21/week @ 2024-04-14

293 downloads per month

MIT license

45KB
1K SLoC

cargo-machete-nk

Remove unused Rust dependencies with this one weird trick!

supported rustc stable

Introduction

This is a fork of cargo-machete, which adds a few additional features, such as --exclude flag to exclude provided subdirectories from the search. cargo-machete is a Cargo tool that detects unused dependencies in Rust projects, in a fast (yet imprecise) way.

See also the original author's blog post for a detailed writeup.

Installation

Install cargo-machete-nk with cargo:

cargo install cargo-machete-nk

Usage

Run cargo-machete-nk in a directory that contains one or more Rust projects (using Cargo for dependency management):

cd my-directory && cargo machete-nk

# alternatively

cargo machete-nk /absolute/path/to/my/directory

The return code gives an indication whether unused dependencies have been found:

  • 0 if machete found no unused dependencies,
  • 1 if it found at least one unused dependency,
  • 2 if there was an error during processing (in which case there's no indication whether any unused dependency was found or not).

This can be used in CI situations.

False positives

To ignore a certain set of dependencies in a crate, add package.metadata.cargo-machete to Cargo.toml (or workspace.metadata.cargo-machete to a workspace Cargo.toml), and specify an ignored array:

For example:

[dependencies]
prost = "0.10" # Used in code generated by build.rs output, which cargo-machete cannot check

# in an individual package Cargo.toml
[package.metadata.cargo-machete]
ignored = ["prost"]

# in a workspace Cargo.toml
[workspace.metadata.cargo-machete]
ignored = ["prost"]

If there are too many false positives, consider using the --with-metadata CLI flag, which will call cargo metadata --all-features to find final dependency names, more accurate dependencies per build type, etc. ⚠ This may modify the Cargo.lock files in your projects.

License

MIT license.

Dependencies

~13–24MB
~419K SLoC