Credence.Pattern.NoRedundantListTraversal (credence v0.7.0)

Copy Markdown

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

Separate Enum.min/1 and Enum.max/1 calls can be replaced by the built-in Enum.min_max/1. Similarly, length/1 + Enum.sum/1 or other pairs on the same list are flagged as a hint, though the count + sum pair is not auto-fixed (see below).

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

Enum.min + Enum.max             Enum.min_max/1

The count + sum pair is flagged but NOT auto-fixed — merging Enum.sum/1

  • length/1 into a manual Enum.reduce/3 with a tuple accumulator is a readability downgrade. The built-in functions are the idiomatic pattern.

Other combinations are flagged but not auto-fixed.

Bad

minimum = Enum.min(numbers)
maximum = Enum.max(numbers)

Good

{minimum, maximum} = Enum.min_max(numbers)