cargo-shear

Crates.iocargo-shear
lib.rscargo-shear
version1.1.3
sourcesrc
created_at2024-03-15 09:33:30.855157
updated_at2024-09-23 02:17:45.022063
descriptionDetect and remove unused dependencies from Cargo.toml
homepage
repositoryhttps://github.com/Boshen/cargo-shear
max_upload_size
id1174532
size53,786
Boshen (Boshen)

documentation

README

Cargo Shear ✂️ 🐑

Detect and remove unused dependencies from Cargo.toml in Rust projects.

Installation

cargo binstall cargo-shear
# OR
cargo install cargo-shear

Usage

cargo shear --fix

Ignore false positives

[!IMPORTANT] cargo shear cannot detect dependency usages from macros because it uses syn to parse the files

False positives can be ignored by adding them to the package's Cargo.toml:

[package.metadata.cargo-shear]
ignored = ["crate"]

or in workspace's Cargo.toml:

[workspace.metadata.cargo-shear]
ignored = ["crate"]

Otherwise please report the issue as a bug.

CI

- name: Install cargo-binstall
  uses: cargo-bins/cargo-binstall@main

- name: Install cargo-shear
  run: cargo binstall --no-confirm cargo-shear

- run: cargo shear

Exit Code (for CI)

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

  • 0 if 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).

Technique

  1. use the cargo_metadata crate to list all dependencies specified in [workspace.dependencies] and [dependencies]
  2. iterate through all package targets (lib, bin, example, test and bench) to locate all Rust files
  3. use syn to parse these Rust files and extract imports
  4. find the difference between the imports and the package dependencies

Prior Arts

Trophy Cases

Sponsored By

My sponsors

Commit count: 171

cargo fmt