Skip to content

Commit

Permalink
prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Mar 15, 2024
1 parent 21664da commit f079076
Show file tree
Hide file tree
Showing 5 changed files with 398 additions and 16 deletions.
234 changes: 233 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 6 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
[package]
name = "cargo-shear"
version = "0.6.1"
version = "0.0.1"
edition = "2021"
description = "Detect and remove unused dependencies from Cargo.toml"
authors = ["Boshen <boshenc@gmail.com>"]
repository = "https://github.com/Boshen/cargo-shear"
keywords = []
categories = []
keywords = ["cargo", "udeps", "machete", "unused", "dependencies"]
categories = ["development-tools", "development-tools::cargo-plugins"]
license = "MIT"
readme = "README.md"

[dependencies]
walkdir = "2.5.0"

[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
strip = "symbols"
debug = false
panic = "abort"
cargo_metadata = "0.18.1"
cargo_toml = "0.19.2"
syn = { version = "2", features = ["full", "visit"] }
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,28 @@

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

## Technique

1. use the `cargo_metadata` crate to list all dependencies specified in `[workspace.dependencies]` and `[dependencies]`
2. iterate through all package targets to locate all Rust files
3. use `syn` to parse these Rust files and extract imports
4. 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)`, `foo` is 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?

## Improvements

* add a CLI, preferably [bpaf](https://docs.rs/bpaf/latest/bpaf/index.html)
* make the reporting more granular for `[dependencies]`, `[dev-dependencies]` and `[build-dependencies]`

## Prior Arts

* https://github.com/est31/cargo-udeps
* https://github.com/bnjbvr/cargo-machete

## Trophy Cases
6 changes: 6 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ init:
watch command:
cargo watch -x '{{command}}'

build-release:
cargo build --release

run-release command:
./target/release/cargo-shear {{command}}

fmt:
cargo fmt
taplo format
Loading

0 comments on commit f079076

Please sign in to comment.