Credence.Pattern.NoSortThenReverse (credence v0.4.4)

Copy Markdown

Performance & readability rule: Detects the pattern of calling Enum.sort/1,2 followed by Enum.reverse/1 on the result, where the sort direction can be statically determined.

Sorting ascending then reversing is equivalent to Enum.sort(list, :desc) but wastes a full O(n) pass for the reversal.

Recognised direction forms

Enum.sort(nums)                        # default :asc
Enum.sort(nums, :asc)                  # explicit atom
Enum.sort(nums, :desc)                 # explicit atom
Enum.sort(nums, &>=/2)                 # capture → :desc
Enum.sort(nums, &<=/2)                 # capture → :asc
Enum.sort(nums, fn a, b -> a > b end)  # anonymous comparator → :desc
Enum.sort(nums, fn a, b -> b > a end)  # flipped comparator  → :asc

Not flagged

Unresolvable directions such as Enum.sort(nums, dir) |> Enum.reverse() or opaque comparators like Enum.sort(nums, &MyModule.compare/2) |> Enum.reverse() are not flagged because we cannot determine the flipped direction.