dead_code_analyzer 1.0.0
dead_code_analyzer: ^1.0.0 copied to clipboard
A tool to analyze and identify dead/unused code in Dart and Flutter projects.
Dead Code Analyzer #
Dead Code Analyzer is a command-line tool for Dart and Flutter projects that identifies unused code elements (classes, functions, variables) and unreachable code segments to streamline code cleanup and refactoring. Optimize your codebase, improve maintainability, and reduce technical debt with comprehensive analysis and actionable reports.
Features #
- Detects unused classes, functions, and variables.
- Identifies unreachable code segments.
- Tracks usage frequency of code elements across the codebase.
- Analyzes internal and external references for accurate results.
- Generates detailed reports with recommendations for code removal.
- Displays interactive progress indicators during analysis.
- Supports custom exclusion patterns to skip specific files or directories.
How to Use #
Take these steps to enable Dead Code Analyzer:
-
Install the package as a dev dependency:
dart pub add --dev dead_code_analyzeror, for Flutter projects:
flutter pub add --dev dead_code_analyzer -
Run the analyzer:
# If installed globally dart pub global activate dead_code_analyzer dead_code_analyzer # Or run directly from source dart run bin/dead_code_analyzer.dart # Analyze a specific project dead_code_analyzer -p /path/to/your/project
Command Line Options #
Usage: dead_code_analyzer [options]
Options:
-p, --project-path Path to the project to analyze (default: current directory)
-o, --output-dir Directory to save the report file (default: Desktop)
-v, --verbose Show detailed output including all usage locations
-e, --exclude Comma-separated patterns to exclude (e.g., "test,example")
--no-progress Disable progress indicators
--only-unused Show only unused elements in the report
-h, --help Show this help message
Example Output #
Note: Timestamps and file paths are illustrative and will vary based on your system and analysis time.
Dead Code Analysis - [Generated Timestamp]
==================================================
Unused Classes
------------------------------
- MyUnusedClass (in lib/my_unused_class.dart)
- OldService (in lib/services/old_service.dart)
Unused Functions
------------------------------
- calculateLegacyTotal (in lib/utils/calculations.dart)
- _validateOldFormat (in lib/validators.dart)
Unreachable Code
------------------------------
- lib/screens/home_screen.dart:47 (code after return statement)
- lib/utils/formatter.dart:102 (code in always-false conditional)
Summary
------------------------------
Total analyzed files: 78
Total classes: 45
Total functions: 126
Total variables: 384
Unused elements: 19 (3.4% of all code elements)
- Unused classes: 7 (15.6%)
- Unused functions: 9 (7.1%)
- Unused variables: 3 (0.8%)
Unreachable code blocks: 5
Full analysis saved to: [User Desktop]/dead_code_analysis_[timestamp].txt
Recommendations:
- Consider removing the unused classes and functions listed above
- Review unreachable code segments
- Run with --verbose flag to see detailed usage information
Would you like a chart to visualize the summary statistics (e.g., unused elements breakdown)? If so, I can generate a pie chart showing the distribution of unused classes, functions, and variables.
Integration with CI/CD #
Integrate Dead Code Analyzer into your CI/CD pipeline to automatically detect dead code:
# Example GitHub Actions workflow
name: Dead Code Check
on:
pull_request:
branches: [ main ]
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1
- name: Install dead_code_analyzer
run: dart pub global activate dead_code_analyzer
- name: Run dead code analysis
run: dead_code_analyzer --no-progress --only-unused
How It Works #
The analyzer:
- Scans all Dart files in your project.
- Builds an Abstract Syntax Tree (AST) for precise analysis.
- Identifies declarations of classes, functions, and variables.
- Tracks references to each element across the codebase.
- Detects unreachable code segments.
- Generates a comprehensive report with actionable findings.
Best Practices #
- Run the tool regularly as part of your code cleanup process.
- Review all results before removing code to avoid unintended deletions.
- Use the
--excludeoption to ignore test files or generated code (e.g.,*.g.dart). - For large projects, consider analyzing specific directories to improve performance.
Limitations #
- May miss edge cases in complex code structures, such as macros or dynamic widget trees in Flutter.
- Dynamic code invocation or reflection may lead to false positives.
- Always manually verify results before modifying your codebase.
Contributing #
Contributions are welcome! To contribute:
- Fork the repository.
- Create your feature branch (
git checkout -b feature/amazing-feature). - Commit your changes (
git commit -m 'Add some amazing feature'). - Push to the branch (
git push origin feature/amazing-feature). - Open a Pull Request.
See our contributing guidelines for more details.
License #
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.