CredoHotUpgrades.Check.ClosureInState (CredoHotUpgrades v1.0.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

An fn, or a capture of a local function, points at the version of the module that built it. Once that module is replaced by a hot upgrade, the value is dead and calling it raises BadFunctionError, in a process that was fine for however long it has been running.

def init(_), do: {:ok, %{on_done: fn r -> notify(r) end}}        # flagged
def handle_cast({:watch, f}, s), do: {:noreply, %{s | cb: &helper/1}}  # flagged
def a, do: :ets.insert(:jobs, {id, fn -> run() end})            # flagged

Flagged wherever the value is kept for later: the state a GenServer callback returns, an :ets.insert, a :persistent_term.put, a Process.put, or a message sent with send/2, GenServer.cast/2 or GenServer.call/2,3. A closure used on the spot (Enum.map(xs, fn ... end)) is not, it never outlives the call that created it.

{:ok, %{on_done: &MyApp.Notifier.notify/1}}      # external capture, always current
{:ok, %{on_done: {MyApp.Notifier, :notify}}}     # or {module, function} to apply

Check-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.