A CLI tool for detecting unused code in Rust projects. This tool helps you maintain clean and efficient codebases by identifying:
- Unused dependencies in
Cargo.toml - Unused functions and methods
- Unused modules
- 🔍 Analyzes Rust source code using
synfor accurate parsing - 📦 Detects unused dependencies in
Cargo.toml - 🚀 Fast and efficient file traversal
- 💡 Smart detection of unused code items
- 🎨 Beautiful colored output
- 📊 Optional JSON output for CI integration
coming soon
# Clone the repository
git clone https://github.com/yourusername/cargo-rust-unused
cd cargo-rust-unused
# Build and install locally
cargo install --path .The tool can be used in two ways:
cargo rust-unused [OPTIONS]cargo-rust-unused [OPTIONS]--path <PATH>: Path to the Rust project (default: current directory)--format <FORMAT>: Output format [text|json] (default: text)--include-private: Include private items in the analysis--help: Print help information--version: Print version information
- Analyze Current Project
# Navigate to your Rust project
cd my-rust-project
# Run the analysis
cargo rust-unused- Analyze with JSON Output
cargo rust-unused --format json- Analyze Specific Project Path
cargo rust-unused --path ~/projects/my-rust-project- Save Analysis Results
# Save as text
cargo rust-unused > analysis_report.txt
# Save as JSON
cargo rust-unused --format json > analysis_report.json🔍 Analyzing project...
Unused Dependencies:
- regex
- serde_yaml
Unused Functions:
- fn unused_utility()
- fn deprecated_function()
Unused Modules:
- mod old_features{
"unused_dependencies": ["regex", "serde_yaml"],
"unused_functions": ["fn unused_utility", "fn deprecated_function"],
"unused_modules": ["mod old_features"]
}You can integrate the tool into your CI/CD pipeline. Here's an example GitHub Actions workflow:
name: Code Analysis
on: [push, pull_request]
jobs:
unused-code-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install cargo-rust-unused
run: cargo install --path .
- name: Run Analysis
run: |
cargo rust-unused --format json > analysis.json
- name: Check Results
run: |
if [ "$(jq '.unused_dependencies | length' analysis.json)" -gt 0 ]; then
echo "Warning: Found unused dependencies!"
exit 1
fi-
Regular Usage:
- Run before committing changes
- Include in pre-commit hooks
- Run as part of code review process
-
Handling Results:
- Review unused dependencies first (they increase build time)
- Check unused functions for potential dead code
- Verify unused modules aren't needed for future use
-
False Positives:
- Some items might be used indirectly (through macros or reflection)
- Dependencies might be needed for dev/test environments
- Consider using
#[allow(dead_code)]for intentionally unused items
-
Tool Not Finding Files:
- Ensure you're in a Rust project directory
- Check if
Cargo.tomlexists - Verify
srcdirectory exists
-
Incorrect Results:
- Update to the latest version
- Try with
--include-privateflag - Check if code uses macros or dynamic dispatch
Error: Not a Cargo project: no Cargo.toml found
Solution: Run from a Rust project directory
Error: No src directory found
Solution: Create src directory or check path
Contributions are welcome! Please feel free to submit a Pull Request. Here's how you can contribute:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.