LexCredo.Check.Warning.NoTaggedWithClauses (LexCredo v0.1.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Do not use tagged-tuple workarounds in with clauses to identify which clause failed in the else block.

If you need to distinguish between different failure sources, use case instead. If you do not need to distinguish, normalise error return types in small private helper functions.

# BAD — tags exist only to tell apart which step failed
with {:user, {:ok, user}} <- {:user, fetch_user(id)},
     {:post, {:ok, post}} <- {:post, fetch_post(user)} do
  {:ok, post}
else
  {:user, {:error, reason}} -> {:error, {:user, reason}}
  {:post, {:error, reason}} -> {:error, {:post, reason}}
end

# GOOD — each function returns a distinct error atom,
# so the step that failed is clear without wrapping tuples
with {:ok, user} <- fetch_user(id),
     {:ok, post} <- fetch_post(user) do
  {:ok, post}
else
  {:error, :user_not_found} = err -> err
  {:error, :post_not_found} = err -> err
end

Check-Specific Parameters

Use the following parameters to configure this check:

:exclude_test_files

When true, skips test files. Default: false.

This parameter defaults to false.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.