Credence.Rule.NoListToTupleForAccess
(credence v0.2.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 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