Credence.Pattern.NoManualStringReverse (credence v0.7.0)

Copy Markdown

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

This rule covers only the grapheme decompose. Reversing a list of graphemes and gluing it back is identical to String.reverse/1 for every input — graphemes are exactly what String.reverse/1 reverses — so it needs no assumption and runs even in :strict mode. The String.codepoints/1 variants are handled by Credence.Pattern.NoCodepointStringReverse, which needs the single_codepoint_graphemes promise (see decision 4: the split key is the decompose function, not the reassemble function).

Bad

reversed = str |> String.graphemes() |> Enum.reverse() |> Enum.join()
reversed = str |> String.graphemes() |> Enum.reverse() |> IO.iodata_to_binary()
reversed = Enum.join(Enum.reverse(String.graphemes(str)))

Good

reversed = String.reverse(str)