Credence.Pattern.NoCaseDestructureInPipe
(credence v0.7.1)
Copy Markdown
Readability rule: Detects a single-clause case used inside a pipeline
whose clause head is an irrefutable variable pattern (a bare variable
or _, with no guard). That shape is exactly equivalent to then/1 and
is non-idiomatic.
LLMs often use |> case do pattern -> body end as a workaround when
|> (fn ... end).() is caught by no_anon_fn_application_in_pipe.
Both are non-idiomatic; then/1 is the idiomatic alternative.
Bad
result
|> compute()
|> case do
value -> value + 1
endGood
result
|> compute()
|> then(fn value -> value + 1 end)Why only irrefutable patterns
A refutable pattern (e.g. {x, acc}, {:ok, v}, a pin, or any clause
with a guard) is not safe to rewrite. When the piped value does not
match, a single-clause case raises CaseClauseError while
then(fn pattern -> ... end) raises FunctionClauseError. Those are
different exceptions, so a caller that rescues CaseClauseError would
behave differently after the rewrite. An irrefutable variable pattern
always matches, so there is no divergent error path and the rewrite is
answer-preserving for every input.