OldJii
dead-code-pruner
Pythonโœจ New

Conservative multi-language dead-code elimination with tree-sitter: feature flag cleanup, constant folding, branch cleanup, method pruning, and dynamic-entry protection.

Last updated Jul 8, 2026
51
Stars
0
Forks
0
Issues
+51
Stars/day
Attention Score
43
Language breakdown
No language data available.
โ–ธ Files click to expand
README

dead-code-pruner

Conservative multi-language dead-code elimination powered by tree-sitter.

dead-code-pruner is a static analysis and source cleanup tool for removing dead code after feature flag cleanup, boolean constant folding, and compile-time configuration changes. Give it a constant mapping and it folds configured flags, simplifies boolean/control-flow expressions, eliminates dead branches, inlines constant-return methods, removes safe dead methods, and protects dynamic framework entry points through iterative project-level analysis.

Built on tree-sitter for format-agnostic, comment-safe, multi-language analysis.

Keywords: dead code elimination, dead code cleanup, static analysis, tree-sitter, feature flag cleanup, boolean simplification, constant folding, Java dead code, Kotlin dead code, Android dead code cleanup, Swift dead code, iOS dead code, TypeScript dead code, refactoring automation.

The Problem

Projects accumulate boolean feature flags (AppConfig.ISDEBUG, FeatureFlags.LEGACYMODE, compile-time constants, etc.). When a flag becomes permanently true or false, the guarded code is dead โ€” but cleaning it manually across thousands of files is tedious and error-prone.

What It Does

# pruner.yaml
replacements:
  - pattern: "AppConfig.IS_DEBUG"
    value: false

The tool runs a 3-phase pipeline that loops until no more changes are found:

| Phase | Steps | Purpose | |-------|-------|---------| | 1 | Constant fold โ†’ bool simplify โ†’ compound bool โ†’ if-block eliminate | Replace flags and simplify control flow | | 2 | Inline constant-return methods โ†’ cascade simplify | Remove return true/false helpers | | 3 | Dead method cleanup โ†’ cascade simplify | Remove empty void methods and orphaned definitions |

Each phase can trigger new simplification opportunities in earlier steps โ€” the pipeline converges automatically.

Quick Start

git clone https://github.com/OldJii/dead-code-pruner.git
cd dead-code-pruner

pip install -r requirements.txt

cp pruner.example.yaml /path/to/your/project/pruner.yaml

Edit pruner.yaml with your project's constants

Preview changes

python3 -m pruner /path/to/your/project --dry-run

Run full pipeline

python3 -m pruner /path/to/your/project

Usage

# Full pipeline (auto-discovers pruner.yaml)
python3 -m pruner .

Explicit config

python3 -m pruner src/ --config pruner.yaml

Dry run โ€” scan and report only

python3 -m pruner . --dry-run

Run specific phases

python3 -m pruner . --phases 1 # constant folding only python3 -m pruner . --phases 1,2 # phases 1 + 2

Shortcut entry point (equivalent to python3 -m pruner):

python3 run_pruner.py . --config pruner.yaml

Configuration

YAML (recommended)

replacements:
  - pattern: "AppConfig.IS_DEBUG"
    value: false
  - pattern: "FeatureFlags.LEGACY_MODE"
    value: false

Flat key-value format

AppConfig.IS_DEBUG: false
FeatureFlags.LEGACY_MODE: false

JSON

{
  "replacements": [
    { "pattern": "AppConfig.IS_DEBUG", "value": false }
  ]
}

Supported Languages

Java, Kotlin, Go, C, C++, JavaScript, TypeScript, Rust, Swift, C# โ€” and any language with a tree-sitter grammar (add one line in pruner/lang.py).

Architecture

pruner/
โ”œโ”€โ”€ cli.py              Command-line interface
โ”œโ”€โ”€ pipeline.py         3-phase orchestrator with progress output
โ”œโ”€โ”€ transform.py        Single-file pipeline (steps 1โ€“4)
โ”œโ”€โ”€ lang.py             Language registry (tree-sitter parsers)
โ”œโ”€โ”€ ast_utils.py        Core AST manipulation
โ”œโ”€โ”€ steps/
โ”‚   โ”œโ”€โ”€ constant_fold.py    Step 1: constant replacement
โ”‚   โ”œโ”€โ”€ bool_simplify.py    Step 2: simple boolean algebra
โ”‚   โ”œโ”€โ”€ compound_bool.py    Step 3: compound boolean + ternary
โ”‚   โ”œโ”€โ”€ if_blocks.py        Step 4: dead branch elimination
โ”‚   โ”œโ”€โ”€ kotlin_expr.py      Kotlin if-expression pre-pass
โ”‚   โ”œโ”€โ”€ method_inline.py    Step 5: constant method inlining
โ”‚   โ””โ”€โ”€ dead_methods.py     Step 6: dead method cleanup
โ””โ”€โ”€ analysis/
    โ”œโ”€โ”€ method_scanner.py     AST method detection
    โ”œโ”€โ”€ project_scan.py       Unified single-pass project scan
    โ”œโ”€โ”€ ref_index.py          Cross-file reference index
    โ”œโ”€โ”€ class_hierarchy.py    Inheritance analysis
    โ””โ”€โ”€ code_edit.py          Call-site replacement & deletion

Safety Mechanisms

  • AST-precise: tree-sitter precisely distinguishes code from comments and strings
  • Annotation-safe: any method with @Annotation is never deleted (framework/DI/AOP managed)
  • Chain-call aware: instance methods called via obj.field.method() are detected via cross-file reference analysis
  • Inheritance-aware: skips abstract, interface, and framework base-class methods
  • Overload-safe: parameter count matching prevents confusing overloaded methods
  • Conservative by design: when in doubt, the method is kept

Requirements

  • Python 3.10+
  • Dependencies: pip install -r requirements.txt

Testing

python3 tests/run_tests.py           # 10-language step 1โ€“4 tests
python3 tests/runprojecttests.py   # step 5 & 6 project-level tests

Limitations

  • Cannot fold through intermediate variables (bool x = FLAG; if (x) ...)
  • Some languages share a C-family parser; edge-case syntax may use dedicated pre-passes
  • Dead method detection is conservative โ€” annotated, public, or potentially-referenced methods are preserved

License

MIT

๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท OldJii/dead-code-pruner ยท Updated daily from GitHub