unused-pub 0.1.3

A tool to detect unused public items (structs, enums, functions, etc.) in a Rust codebase.
Documentation

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

cargo install unused-pub

Usage

Run the tool on your project directory:

unused-pub <path-to-project>

Example:

unused-pub .

Filtering

You can filter by type (struct, enum, fn, const, trait, type) using the -f flag:

# Only find unused structs

unused-pub . -f struct


# Find unused structs and enums

unused-pub . -f struct -f enum

How it works

  1. Scans: Walks the target directory for .rs files.
  2. Parses: Uses syn to parse ASTs.
  3. Collects:
    • Definitions: Finds all pub items.
    • Usages: Finds all identifiers, paths, and macro tokens used in function bodies, struct fields, expressions, etc.
  4. Reports: Any pub item that is not found in the "usages" set is reported.

License

MIT

PRs

Are welcome!