unused-pub
A Cargo subcommand/CLI tool to detect potentially unused pub items in your Rust crate.
It identifies public structs, enums, functions, consts, traits, and types that are defined but never used within the codebase. This is useful for cleaning up libraries or identifying dead code in large workspaces.
Features
- Deep Analysis: Scans definition sites and usage sites.
- Accurate: Handles complex paths (
foo::bar::Baz), imports (use), macro invocations (format!("{Foo}")), and doc comments (/// [Foo]). - Workspace Support: Runs on any target directory.
Installation
Usage
Run the tool on your project directory:
Example:
Filtering
You can filter by type (struct, enum, fn, const, trait, type) using the -f flag:
# Only find unused structs
# Find unused structs and enums
How it works
- Scans: Walks the target directory for
.rsfiles. - Parses: Uses
synto parse ASTs. - Collects:
- Definitions: Finds all
pubitems. - Usages: Finds all identifiers, paths, and macro tokens used in function bodies, struct fields, expressions, etc.
- Definitions: Finds all
- Reports: Any
pubitem that is not found in the "usages" set is reported.
License
MIT
PRs
Are welcome!