MCPcopy Index your code
hub / github.com/bnjbvr/cargo-machete

github.com/bnjbvr/cargo-machete @v0.9.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.9.2 ↗ · + Follow
60 symbols 114 edges 19 files 6 documented · 10%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

cargo-machete

<strong>Remove unused Rust dependencies with this one weird trick!</strong>







<a href="https://github.com/bnjbvr/cargo-machete/actions?query=workflow%3ARust"><img src="https://github.com/bnjbvr/cargo-machete/workflows/Rust/badge.svg" alt="build status" /></a>
<a href="https://matrix.to/#/#cargo-machete:delire.party"><img src="https://img.shields.io/badge/matrix-join_chat-brightgreen.svg" alt="matrix chat" /></a>
<img src="https://img.shields.io/badge/rustc-stable+-green.svg" alt="supported rustc stable" />
<a href="https://deps.rs/repo/github/bnjbvr/cargo-machete"><img src="https://deps.rs/repo/github/bnjbvr/cargo-machete/status.svg" alt="dependency status"/></a>

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.

Renamed crates

Some crates have a different import name, than their dependency name (e.g. rustls-webpki). This triggers false positives in cargo-machete when used without the --with-metadata flag. To fix this you can manually rename a crate for cargo-machete:

For example:

[dependencies]
rustls-webpki = "0.102"

# in an individual package Cargo.toml
[package.metadata.cargo-machete.renamed]
rustls-webpki = "webpki"

# in a workspace Cargo.toml
[workspace.metadata.cargo-machete.renamed]
rustls-webpki = "webpki"

This ensures that if rustls-webpki is no longer used in the project, cargo-machete will be able to detect it (unlike with the ignored table).

Docker Image

A docker image for cargo machete.

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

docker run --rm -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:
  detect-unused-dependencies:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Machete
        uses: bnjbvr/cargo-machete@main

pre-commit Hooks

cargo machete can be used as a pre-commit hook to check for unused dependencies before committing changes.

Add the following to your .pre-commit-config.yaml:

- repo: https://github.com/bnjbvr/cargo-machete
  rev: main  # or a specific tag/commit
  hooks:
    - id: cargo-machete

Contributing

Contributor Covenant

We welcome community contributions to this project.

License

MIT license.

Core symbols most depended-on inside this repo

find_unused
called by 11
src/search_unused.rs
check_analysis
called by 9
src/search_unused.rs
collect_paths
called by 4
src/main.rs
func
called by 2
integration-tests/with-bench/bench/src/main.rs
try_singleline_then_multiline
called by 2
src/search_unused.rs
remove_dependencies
called by 2
src/main.rs
lib2_func
called by 1
integration-tests/unused-transitive/lib2/src/lib.rs
lib1_func
called by 1
integration-tests/unused-transitive/lib1/src/lib.rs

Shape

Function 45
Class 7
Method 6
Enum 2

Languages

Rust100%

Modules by API surface

src/search_unused.rs34 symbols
src/main.rs12 symbols
integration-tests/multi-key-dep/src/main.rs2 symbols
integration-tests/workspace-package/program/src/main.rs1 symbols
integration-tests/with-bench/lib/src/lib.rs1 symbols
integration-tests/with-bench/bench/src/main.rs1 symbols
integration-tests/unused-transitive/src/main.rs1 symbols
integration-tests/unused-transitive/lib2/src/lib.rs1 symbols
integration-tests/unused-transitive/lib1/src/lib.rs1 symbols
integration-tests/renamed-dep/src/main.rs1 symbols
integration-tests/renamed-dep-workspace/inner/src/main.rs1 symbols
integration-tests/just-unused/src/main.rs1 symbols

For agents

$ claude mcp add cargo-machete \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page