Credence.Pattern.NoEnumDropNegative (credence v0.4.4)

Copy Markdown

Performance rule: Detects Enum.drop(list, -n) where n is a positive integer literal.

For linked lists, Enum.drop(list, -n) must traverse to the end of the list to figure out where to cut, making it O(n). This often indicates the algorithm should be restructured to avoid needing to trim from the tail of a linked list.

The auto-fix replaces Enum.drop(list, -n) with Enum.slice(list, 0..-(n+1)//1), which has equivalent semantics. If performance is critical, consider restructuring to avoid tail-trimming entirely.

Bad

list |> Enum.drop(-1)

Enum.drop(list, -3)

Good

# If building the list yourself, drop the head before reversing:
[_ | rest] = reversed_list
Enum.reverse(rest)

# Or use Enum.slice/2 if you know the desired length:
Enum.slice(list, 0..-2//1)