Credence.Pattern.NoSortThenAt
(credence v0.4.2)
Copy Markdown
Performance rule (fixable subset): Detects Enum.sort |> Enum.at(index) where
the index is a literal 0 or -1. These can be safely replaced with
Enum.min/1 or Enum.max/1, avoiding the O(n log n) sort entirely.
Bad (fixable)
Enum.sort(nums, :desc) |> Enum.at(0)
Enum.at(Enum.sort(nums), 0)
Enum.sort(nums, :asc) |> Enum.at(-1)Good
Enum.max(nums)
Enum.min(nums)
Enum.max(nums)Not flagged
Other literal indices like Enum.sort(nums) |> Enum.at(3) are not flagged
because there is no standard-library O(n) replacement for kth-element access.
Variable indices such as Enum.sort(nums) |> Enum.at(k - 1) are not flagged
because they represent valid kth-element access that genuinely needs a sort.