MutagenEx.Mutators.GuardDrop (mutagen_ex v0.2.0)

Copy Markdown View Source

Drops the guard from a function clause head.

Per mutagen.mutators catalog entry 10.

AST shape

def f(x) when x > 0, do: x parses (inside the def macro) with a clause head wrapped in :when:

{:when, meta, [{:f, _, [x]}, {:>, _, [x, 0]}]}

Anonymous functions and case clauses can also carry guards in the form {:->, _, [[{:when, _, [pattern, guard]}], body]}. We match the bare :when AST shape; the enumerator can find these wherever they appear.

Drop

Remove the :when wrapper, leaving only the head/pattern. Nested guards (x when a when b) parse as one :when inside another, so each level is its own site.

A multi-parameter anonymous function clause keeps every parameter in one node: fn a, b when a > b -> a end has {:when, _, [a, b, guard]}. Replacing that node cannot drop the guard without dropping parameters, so mutate/1 returns it unchanged.

Validation

validate/1 receives the result of mutate/1, like every other mutator. Any head or pattern is :ok. A multi-parameter :when node is :structurally_invalid, because mutate/1 could not drop its guard.