#unused #remove #cargo #generated #minify #generated-bindings #aimed

app cargo-minify

A cargo utility to automatically remove unused code from a Rust project

1 unstable release

0.5.0 Dec 1, 2023

#271 in Cargo plugins

Apache-2.0 OR MIT

48KB
1K SLoC

cargo-minify

Remove unused code from your Rust programs. Primarily aimed at generated code (prime example: bindings generated by bindgen), but also usable in more general context.

Limitations

Public functions and types in libraries (which makes sense) but also in binaries and examples are not considered unused, as well as code that is explicitly allowed to be unused (using #[allow(unused)]).

Installation

git clone git@github.com:tweedegolf/cargo-minify.git
cargo install --path cargo-minify

Usage

After installation, you can run this tool by simply typing cargo minify from your crate root. This runs it on your project and will print out any changes that will be made to your code.

To actually apply these changes, you have to run cargo minify --apply.

You can perform a more precise minifcation by using the --ignore option, followed by a wildcard specification. Unused code in the excluded files will not be touched. You can also you the --kinds flag to specify which types of unused code to remove. Supported are:

  • FUNCTION, which will remove unused function defintions
  • ASSOCIATED_FUNCTION, which will remove unused associated functions from impl blocks
  • STRUCT, ENUM, UNION, which will remove unused type definitions of said type
  • TYPE_ALIAS, which removes unused type aliases
  • CONST, which will remove unused constants
  • STATIC, which will remove unused static variables

Without any --kinds specification, all of the above will be removed.

cargo minify --apply expects your files to be under control of version control; if this is not the case a warning will be given and no changes will be made; this can be overridden using the --allow-no-vcs, --allow-dirty, and --allow-staged flags.

Of course you can also view this information (and other options) by running cargo minify --help.

Future work

Still to add to cargo minify:

  • Remove unused static variables.
  • Detected and remove unused derived traits.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~12–24MB
~372K SLoC