Credence.Pattern.NoRedundantDedupBeforeMapset (credence v0.7.1)

Copy Markdown

Detects Enum.dedup/1 or Enum.uniq/1 used before MapSet.new/1, making the deduplication a redundant intermediate step.

MapSet.new/1 already stores only unique elements, so running Enum.dedup/1 or Enum.uniq/1 beforehand has no effect. Enum.dedup/1/Enum.uniq/1 and MapSet all use exact (===) equality, so removing the pre-pass never changes which elements survive (e.g. 1 and 1.0 stay distinct under both).

A Enum.sort/2/Enum.sort_by/2,3 (or any Enum.sort/1) intermediate is not matched: dropping it is not behaviour-preserving. The comparator or key function can have side effects or raise on some element, so removing the sort can change the answer (turn a raise into a MapSet). Those shapes are left alone — see the "no issue" tests.

Bad

Enum.dedup(items) |> MapSet.new()
items |> Enum.dedup() |> MapSet.new()
MapSet.new(Enum.dedup(items))
Enum.uniq(items) |> MapSet.new()

Good

MapSet.new(items)
MapSet.new(items)
MapSet.new(items)
MapSet.new(items)