Credence.Pattern.NoMapAsSet
(credence v0.4.2)
Copy Markdown
Style rule: Detects using a Map with boolean literal values (true/false)
purely for membership tracking, when MapSet is more appropriate.
Map.put(seen, item, true) paired with Map.has_key?(seen, item) is a
manual reimplementation of MapSet.put/2 and MapSet.member?/2. Using
MapSet makes the intent clearer and avoids storing meaningless values.
This rule is not auto-fixable because a correct transformation requires
companion changes beyond the flagged call site — initialising the variable
with MapSet.new() instead of %{}, and replacing the corresponding
Map.has_key?/2 check with MapSet.member?/2. Those changes involve
data-flow analysis that cannot be done safely with a local AST rewrite.
Bad
# Boolean value used purely for membership tracking
Map.put(seen, item, true)
Map.put(seen, item, false)
# Typical pattern this catches
if Map.has_key?(seen, item) do
{seen, acc}
else
{Map.put(seen, item, true), [item | acc]}
endGood
if MapSet.member?(seen, item) do
{seen, acc}
else
{MapSet.put(seen, item), [item | acc]}
end