Credence.Pattern.NoUnusedComputation
(credence v0.8.0)
Copy Markdown
Removes a dead _-prefixed assignment of a total, provably-typed call.
An assignment like _n = length(chars) computes a value that is discarded
(the _ prefix signals intent). Removing it looks safe — but a "pure"
function is not necessarily total: length/1 raises on a non-list,
String.length/1 on a non-binary, abs/1 on a non-number. Deleting a
discarded call that would have raised on some input changes behaviour
(the original crashes; the rewrite does not). So we only remove the call
when both:
- the function is total given its argument type (
lengthon any list,String.graphemeson any binary,abson any number, …) — partial functions likediv/rem(÷0),hd/tl([]),elem(index),String.to_integer(non-numeric) are never removed; and - the argument is provably that type — a literal, a
++/<>/%{}expression, a type-returning call (String.graphemes,Enum.map, …), or a variable bound earlier in the same block to such an expression.
A bare variable of unknown type, or a partial function, is left untouched. The last expression of a block is never removed (it is the block's value).
Bad
chars = String.graphemes(s)
_n = length(chars)
Enum.with_index(chars)Good
chars = String.graphemes(s)
Enum.with_index(chars)