Credence.Pattern.PreferTupleDestructureAfterWithIndex (credence v0.8.0)

Copy Markdown

Detects Enum.map calls that follow Enum.with_index() in a pipe chain but use a multi-argument anonymous function instead of destructuring the {element, index} tuple that with_index/1 produces.

Bad

matrix
|> Enum.with_index()
|> Enum.map(fn row, index ->
  {index, Enum.count(row, &(&1 == 1))}
end)

Enum.map/2 expects a 1-arity function, but fn row, index -> has arity 2. At runtime this raises BadArityError on every input because Enum.with_index/1 emits {element, index} tuples — a single argument.

Good

matrix
|> Enum.with_index()
|> Enum.map(fn {row, index} ->
  {index, Enum.count(row, &(&1 == 1))}
end)

The tuple from with_index/1 is destructured directly in the function head, which is both idiomatic and arity-correct.