Credence.Rule.NoSortThenAtUnfixable
(credence v0.3.1)
Copy Markdown
Performance rule (flag-only companion): Detects Enum.sort |> Enum.at(index)
patterns where the index is not a compile-time literal 0 or -1, or the
sort direction cannot be statically determined.
These cases require human or LLM judgement to rewrite because the correct replacement depends on the runtime value of the index and/or sort direction.
Examples that trigger this rule
Enum.sort(nums, :desc) |> Enum.at(k - 1) # variable index
Enum.at(Enum.sort(nums), mid) # variable index
Enum.sort(nums, dir) |> Enum.at(0) # variable direction
Enum.sort(nums, fn a, b -> a > b end) |> Enum.at(0) # custom comparatorWhat to do
For index 0 → Enum.min/1 or Enum.max/1.
For index -1 → Enum.max/1 or Enum.min/1 (reversed).
For other indices → Enum.take/2 on the sorted list, or a partial-sort /
quickselect algorithm.