Rbtz.CredoChecks.Refactor.RedundantThen (rbtz_credo_checks v0.3.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 normal and works with any version of Elixir.

Explanation

Flags unnecessary uses of Kernel.then/2.

then(val, fun) is semantically fun.(val). When fun is a simple pass-through or partial application where val naturally lands at the first argument position, then/2 adds a layer of indirection that can be removed — just call the function directly.

Bad

"hello" |> then(&String.upcase/1)
"hello" |> then(fn x -> String.upcase(x) end)
map |> then(&Map.get(&1, :key))
then(x, &String.upcase/1)

Good

"hello" |> String.upcase()
map |> Map.get(:key)
String.upcase(x)

then/2 is the right tool when the piped value can't be the first argument. These uses are not flagged:

  • val |> then(&foo(closure, &1)) — val is the second arg.
  • val |> then(fn v -> foo(closure, v) end) — same, via fn.
  • val |> then(&(&1 + &1)) — val used more than once.
  • val |> then(&(&1 * 2)) — operator body; can't be unpiped directly.
  • Multi-clause fns, guards, pattern-matched args.
  • Bodies containing case / if / |> / nested fn / nested &.

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.