Credence.Rule.NoIdentityFunctionInEnum (credence v0.3.0)

Copy Markdown

Detects Enum._by functions called with an identity function callback, which can be simplified to the non-_by variant.

LLMs sometimes generate Enum.uniq_by(fn x -> x end) instead of the simpler Enum.uniq(). This pattern appears across all _by variants.

Bad

list |> Enum.uniq_by(fn x -> x end)
list |> Enum.sort_by(& &1)
Enum.min_by(list, fn item -> item end)
Enum.max_by(list, &Function.identity/1)

Good

list |> Enum.uniq()
list |> Enum.sort()
Enum.min(list)
Enum.max(list)

Auto-fix

Rewrites Enum.uniq_by(list, identity) to Enum.uniq(list) and the piped form |> Enum.uniq_by(identity) to |> Enum.uniq(). Handles fn x -> x end, & &1, and &(&1) as identity functions.