Metastatic.Analysis.BusinessLogic.MissingErrorHandling (Metastatic v0.10.4)

View Source

Detects pattern matching on success cases without error handling.

This analyzer identifies code that pattern matches on a success value (typically a tuple or enum variant) without handling the potential error case, which can lead to runtime crashes.

Cross-Language Applicability

This pattern applies to languages with pattern matching and Result/Option types:

  • Elixir: Matching {:ok, value} without handling {:error, reason}
  • Rust: Unwrapping Result<T, E> with .unwrap() or matching only Ok(v)
  • OCaml/F#: Matching only Some(v) without handling None
  • Scala: Pattern matching on Success without Failure
  • Haskell: Pattern matching on Right without Left

Examples

Bad (Elixir)

{:ok, user} = Accounts.get_user(id)  # Will crash if error returned

Good (Elixir)

case Accounts.get_user(id) do
  {:ok, user} -> user
  {:error, reason} -> handle_error(reason)
end

# Or with pattern
with {:ok, user} <- Accounts.get_user(id) do
  user
end

Bad (Rust)

let user = get_user(id).unwrap();  // Panics on error

Good (Rust)

let user = match get_user(id) {
    Ok(u) => u,
    Err(e) => handle_error(e),
};

Detection Strategy

Detects pattern matching nodes where:

  1. The pattern is a success variant (e.g., tuple with :ok atom, or similar markers)
  2. No corresponding error handling pattern exists in the same scope