Metastatic.Analysis.DeadCode (Metastatic v0.10.4)

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, [subtype: :integer], 42}]},
  {:function_call, [name: "print"], [{:literal, [subtype: :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, [category: :arithmetic, operator: :+], [{:literal, [subtype: :integer], 1}, {:literal, [subtype: :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, [subtype: :integer], 1}]},
...>   {:literal, [subtype: :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(language_or_doc, source_or_ast_or_opts \\ [], opts \\ [])

@spec analyze(Metastatic.language(), term(), keyword()) ::
  {:ok, map()} | {:error, term()}

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, [subtype: :integer], 42}
iex> doc = Metastatic.Document.new(ast, :elixir)
iex> {:ok, result} = Metastatic.Analysis.DeadCode.analyze(doc)
iex> result.has_dead_code?
false

analyze!(language_or_doc, source_or_ast_or_opts \\ [], opts \\ [])

@spec analyze!(Metastatic.language(), term(), keyword()) :: map()

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, [subtype: :integer], 42}
iex> doc = Metastatic.Document.new(ast, :elixir)
iex> {:ok, result} = Metastatic.Analysis.DeadCode.analyze(doc)
iex> result.has_dead_code?
false

Unlike not-banged version, this one either returns a result or raises