Credence.Pattern.NoBareValueInMapNew
(credence v0.8.0)
Copy Markdown
Repairs Map.new/2 whose mapper returns a bare (non-{key, value}) value.
Map.new(enum, fun) requires fun to return a {key, value} tuple for every
element — the results are handed to :maps.from_list/1. LLMs frequently write
a mapper that returns a bare value instead, e.g. building an adjacency list:
Map.new(0..(n - 1), fn _k -> [] end)This compiles but raises ArgumentError (:maps.from_list/1) on every
non-empty input — there is no value of enum for which it produces a map. The
intended map is %{element => value}, so the repair pairs the element (the
mapper's parameter) with the value:
Bad
Map.new(0..(n - 1), fn _k -> [] end)
Map.new(items, fn item -> 0 end)Good
Map.new(0..(n - 1), fn k -> {k, []} end)
Map.new(items, fn item -> {item, 0} end)Repair, not a rewrite
This is a mark_equivalence_repair rule: it only matches a mapper whose body
is a literal that can never be a 2-tuple (a list, number, string, atom, map, or
a tuple of arity ≠ 2), so the original crashes on every input and there is no
behaviour to preserve. A mapper that already returns a tuple, or returns a
call/variable that might be a tuple, is never touched — so the rule cannot
fire on working code.