Credence.Rule.NoManualStringReverse (credence v0.3.0)

Copy Markdown

Readability & performance rule: Detects the pattern String.graphemes(s) |> Enum.reverse() |> Enum.join() (and the nested equivalent Enum.join(Enum.reverse(String.graphemes(s)))) which is a manual reimplementation of String.reverse/1.

String.reverse/1 handles Unicode grapheme clusters correctly and avoids creating an intermediate list, making it both clearer and faster.

Bad

# In a pipeline
reversed = str |> String.graphemes() |> Enum.reverse() |> Enum.join()

# As a nested call
reversed = Enum.join(Enum.reverse(String.graphemes(str)))

Good

reversed = String.reverse(str)