Credence.Pattern.NoListToTupleForAccess (credence v0.6.0)

Copy Markdown

Performance & style rule: Detects converting a list to a tuple via List.to_tuple/1 and then accessing elements with elem/2. Tuples are meant for small, fixed-size collections. Copying a dynamically-sized list into a tuple just for one-shot indexed access defeats the purpose and allocates a full copy of the data. Use pattern matching ([a, b | _] = list) or Enum.at/2 on the list directly instead. For string processing, use binary_part/3 or binary pattern matching.

Bad

t = List.to_tuple(graphemes)
first = elem(t, 0)
last = elem(t, tuple_size(t) - 1)

Good

[first | _] = graphemes
last = List.last(graphemes)
# Or for indexed access on strings:
<<first::utf8, _rest::binary>> = string

Loop-scope exemption

The pattern t = List.to_tuple(list) outside a loop, followed by elem(t, i) inside Enum.reduce/Enum.map/fn/for/... is the canonical Elixir idiom for O(1) random access during iteration. Rewriting that elem to Enum.at would turn O(m + n) into O(m × n). The rule recognises this shape and refuses to flag (or auto-fix) it.