Jump. CredoChecks. AvoidFunctionLevelElse
(Jump.CredoChecks v0.4.0)
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
Ensures that else is not used at the top-level of def/defp function bodies.
Elixir allows else at the function level because the function body acts
as an implicit try. However, this is almost always a mistake from a
botched refactor—e.g., removing a with block but leaving its else
clause behind. The code will compile without warnings, but behaves unexpectedly
(raising TryClauseError if no clause matches).
# ❌ Bad — function-level else
def foo(bar) do
something(bar)
else
{:error, reason} -> handle_error(reason)
end
# Good — use with/else or case instead
def foo(bar) do
with {:ok, result} <- something(bar) do
result
else
{:error, reason} -> handle_error(reason)
end
endCheck-Specific Parameters
There are no specific parameters for this check.
General Parameters
Like with all checks, general params can be applied.
Parameters can be configured via the .credo.exs config file.