Search documentation of Metastatic

# Metastatic.Analysis.DeadCode (Metastatic v0.2.0)

View Source

Dead code detection at the MetaAST level.

Identifies unreachable code, unused functions, and other patterns that result in dead code. Works across all supported languages by operating on the unified MetaAST representation.

## Dead Code Types

- Unreachable after return - Code following early_return nodes
- Constant conditionals - Branches that can never execute (if true/false)
- Unused functions - Function definitions never called (module context required)
- Other unreachable - Code that can never be reached

## Usage

    alias Metastatic.{Document, Analysis.DeadCode}

    # Analyze for dead code
    ast = {:block, [
      {:early_return, {:literal, :integer, 42}},
      {:function_call, "print", [{:literal, :string, "unreachable"}]}
    ]}
    doc = Document.new(ast, :python)
    {:ok, result} = DeadCode.analyze(doc)

    result.has_dead_code?       # => true
    result.total_dead_statements # => 1
    result.dead_locations        # => [%{type: :unreachable_after_return, ...}]

## Examples

    # No dead code
    iex> ast = {:binary_op, :arithmetic, :+, {:literal, :integer, 1}, {:literal, :integer, 2}}
    iex> doc = Metastatic.Document.new(ast, :python)
    iex> {:ok, result} = Metastatic.Analysis.DeadCode.analyze(doc)
    iex> result.has_dead_code?
    false

    # Unreachable after return
    iex> ast = {:block, [
    ...>   {:early_return, {:literal, :integer, 1}},
    ...>   {:literal, :integer, 2}
    ...> ]}
    iex> doc = Metastatic.Document.new(ast, :python)
    iex> {:ok, result} = Metastatic.Analysis.DeadCode.analyze(doc)
    iex> result.has_dead_code?
    true
    iex> [location | _] = result.dead_locations
    iex> location.type
    :unreachable_after_return

# Summary

## Functions

analyze(doc, opts \\ [])

Analyzes a document for dead code.

analyze!(doc, opts \\ [])

Analyzes a document for dead code, raising on error.

# Functions

# analyze(doc, opts \\ [])

    @spec analyze(
      Metastatic.Document.t(),
      keyword()
    ) :: {:ok, Metastatic.Analysis.DeadCode.Result.t()}

Analyzes a document for dead code.

Returns `{:ok, result}` where result is a `Metastatic.Analysis.DeadCode.Result` struct.

## Options

- `:detect_unused_functions` - Enable unused function detection (default: false, requires module context)
- `:min_confidence` - Minimum confidence level to report (default: :low)

## Examples

    iex> ast = {:literal, :integer, 42}
    iex> doc = Metastatic.Document.new(ast, :elixir)
    iex> {:ok, result} = Metastatic.Analysis.DeadCode.analyze(doc)
    iex> result.has_dead_code?
    false

# analyze!(doc, opts \\ [])

    @spec analyze!(
      Metastatic.Document.t(),
      keyword()
    ) :: Metastatic.Analysis.DeadCode.Result.t()

Analyzes a document for dead code, raising on error.

## Examples

    iex> ast = {:literal, :integer, 42}
    iex> doc = Metastatic.Document.new(ast, :elixir)
    iex> result = Metastatic.Analysis.DeadCode.analyze!(doc)
    iex> result.has_dead_code?
    false