12 releases (6 breaking)

0.7.0 Sep 25, 2024
0.6.2 Mar 24, 2024
0.6.1 Feb 21, 2024
0.6.0 Oct 10, 2023
0.1.0 Nov 13, 2021

#50 in Cargo plugins

Download history 10603/week @ 2024-08-19 11253/week @ 2024-08-26 12344/week @ 2024-09-02 10178/week @ 2024-09-09 10830/week @ 2024-09-16 13943/week @ 2024-09-23 12458/week @ 2024-09-30 12213/week @ 2024-10-07 14042/week @ 2024-10-14 11793/week @ 2024-10-21 9434/week @ 2024-10-28 11774/week @ 2024-11-04 11849/week @ 2024-11-11 12191/week @ 2024-11-18 10142/week @ 2024-11-25 12130/week @ 2024-12-02

47,188 downloads per month
Used in nexus7

MIT license

46KB
963 lines

cargo-machete

Remove unused Rust dependencies with this one weird trick!

build status matrix chat supported rustc stable dependency status

Thanks to Diego F. Goberna for the lovely logo 🥰

Introduction

cargo-machete is a Cargo tool that detects unused dependencies in Rust projects, in a fast (yet imprecise) way.

See also the blog post for a detailed writeup.

Installation

Install cargo-machete with cargo:

cargo install cargo-machete

Usage

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

cd my-directory && cargo machete

# alternatively

cargo machete /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.

Docker Image

A docker image for cargo machete.

For instance, run cargo-machete in the $(pwd) directory using:

docker run -v $(pwd):/src ghcr.io/bnjbvr/cargo-machete:latest

Cargo Machete Action

A github action for cargo machete.

Example usage

The step given by,

      - uses: bnjbvr/cargo-machete@main

can be added to any workflow.

An example workflow is shown below:

name: Cargo Machete
on:
  pull_request: { branches: "*" }

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Machete
        uses: bnjbvr/cargo-machete@main

Contributing

Contributor Covenant

We welcome community contributions to this project.

License

MIT license.

Dependencies

~11–20MB
~350K SLoC