Search documentation of Metastatic

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

View Source

Unused variable detection at the MetaAST level.

Identifies variables that are assigned but never read, including function parameters, loop iterators, and pattern matches. Works across all supported languages by operating on the unified MetaAST representation.

## Detection Categories

- Local variables - Assigned but never referenced
- Function parameters - Lambda parameters never used in body
- Loop iterators - Loop variables never accessed
- Pattern matches - Variables bound in patterns but not used

## Usage

    alias Metastatic.{Document, Analysis.UnusedVariables}

    # Analyze for unused variables
    ast = {:block, [
      {:assignment, {:variable, "x"}, {:literal, :integer, 42}},
      {:literal, :integer, 5}
    ]}
    doc = Document.new(ast, :python)
    {:ok, result} = UnusedVariables.analyze(doc)

    result.has_unused?       # => true
    result.total_unused      # => 1
    result.unused_variables  # => [%{name: "x", category: :local, ...}]

## Examples

    # No unused variables
    iex> ast = {:assignment, {:variable, "x"}, {:literal, :integer, 5}}
    iex> doc = Metastatic.Document.new(ast, :python)
    iex> {:ok, result} = Metastatic.Analysis.UnusedVariables.analyze(doc)
    iex> result.has_unused?
    false

    # Unused local variable
    iex> ast = {:block, [
    ...>   {:assignment, {:variable, "x"}, {:literal, :integer, 1}},
    ...>   {:literal, :integer, 2}
    ...> ]}
    iex> doc = Metastatic.Document.new(ast, :python)
    iex> {:ok, result} = Metastatic.Analysis.UnusedVariables.analyze(doc)
    iex> result.has_unused?
    true
    iex> [var | _] = result.unused_variables
    iex> var.name
    "x"
    iex> var.category
    :local

# Summary

## Functions

analyze(doc, opts \\ [])

Analyzes a document for unused variables.

analyze!(doc, opts \\ [])

Analyzes a document for unused variables, raising on error.

# Functions

# analyze(doc, opts \\ [])

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

Analyzes a document for unused variables.

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

## Options

- `:ignore_underscore` - Ignore variables starting with underscore (default: true)
- `:categories` - List of categories to check (default: all)

## Examples

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

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

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

Analyzes a document for unused variables, raising on error.

## Examples

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