ForgeCredoChecks.ReverseListFirst (forge_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 high and works with any version of Elixir.

Explanation

Replace xs |> Enum.reverse() |> List.first() with List.last(xs).

Why

List.last/1 returns the same value directly, without allocating the reversed list. Common LLM artifact: building an accumulator with [v | acc], reversing at the end to restore order, then taking the first element. The reverse round-trip is wasted work.

How to fix

# BEFORE
xs |> Enum.reverse() |> List.first()

# AFTER
List.last(xs)

Both nested and piped forms are flagged:

List.first(Enum.reverse(xs))   # also flagged
List.first(xs |> Enum.reverse())  # also flagged

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.