Credence.Pattern.NoRedundantListTraversal (credence v0.4.4)

Copy Markdown

Detects multiple traversals of the same list that could be merged into a single pass.

LLMs routinely produce code like count = length(numbers) followed by sum = Enum.sum(numbers) — two O(n) passes where one Enum.reduce/3 would do. Similarly, separate Enum.min/1 and Enum.max/1 calls can be replaced by the built-in Enum.min_max/1.

Only flags calls that are:

  • bare assignments (var = func(list)) in the same block
  • arity-1 calls on a plain variable (not an expression or field access)
  • on the same variable with no rebinding between them

Detected functions

length/1            :count
Enum.count/1        :count
Enum.sum/1          :sum
Enum.min/1          :min
Enum.max/1          :max

Auto-fixable pairs

length/Enum.count + Enum.sum    single Enum.reduce/3
Enum.min + Enum.max             Enum.min_max/1

Other combinations are flagged but not auto-fixed.

Bad

count = length(numbers)
sum = Enum.sum(numbers)

Good

{count, sum} = Enum.reduce(numbers, {0, 0}, fn x, {c, s} -> {c + 1, s + x} end)