Add an @unthrown/oxlint rule: no-unused-matcher (…Cases callbacks that ignore the injected matcher)
Summary
@unthrown/[email protected] ships six rules (no-ambiguous-error-type, prefer-async-result, no-unhandled-result, no-catch-all-pattern, no-throw, prefer-ensure). I'd like to propose a seventh: unthrown/no-unused-matcher, flagging a …Cases callback that never uses the matcher it was handed.
no-catch-all-pattern already guards the exhaustiveness contract against P._. This is the other way through the same door — and unlike the catch-all, it is invisible to both the type checker and the runtime.
Why the type checker doesn't catch it
recoverErrCases<M extends ExhaustiveMatch<unknown>>(
f: (matcher: ErrMatcher<E>, defect: (cause: unknown) => Defect) => M,
): AsyncResult<T | MatchErrOut<M>, never>
The constraint is on the shape of the returned builder, not on its provenance. Any exhaustive matcher satisfies it — including one built over a completely unrelated value. Meanwhile:
function runMatch(f, error) {
return f(match(error), defect).run();
}
The injected matcher is the only one bound to the actual error. A builder returned from elsewhere gets .run() called against whatever value it closed over.
noUnusedParameters does not cover this: the parameter is not unused, it is simply never declared.
Repro (unthrown 5.0.0)
class AError extends TaggedError("A") {}
class BError extends TaggedError("B") {}
const source = ErrAsync(new BError({ message: "boom" }));
const decoy = new AError({ message: "decoy" });
// compiles clean under --strict
const recovered = await source.recoverErrCases(() =>
match(decoy)
.with(P.tag("A"), () => "recovered as A")
.with(P.tag("B"), () => "recovered as B"),
);
1. borrowed matcher -> "Ok" recovered as A
2. foreign matcher -> "Defect"
defect cause -> NonExhaustiveError: unthrown: no pattern matched the value {"_tag":"X"}
Two outcomes, neither diagnosable from the call site:
- Wrong branch, silently. A
BErroris recovered as if it were anAError, because the branch was chosen bydecoy. The declared type is stillAsyncResult<string, never>— the Err channel reads as fully handled. - Err → Defect. When nothing in the borrowed matcher matches,
.run()throwsNonExhaustiveError,recoverErrCasescatches it and returns a Defect. In the Temporal bridge that turns a deliberately non-retryable business error into a retryable failure — the same failure class as #142, arrived at from a different direction.
What is already rejected
Worth noting, because it bounds the rule's job: the obvious 4.x muscle-memory version is caught by the type system today.
// correctly rejected
.recoverErrCases((error) => match(error).with(P.tag("A"), …).with(P.tag("B"), …))
// Type 'Matcher<Matcher<AError | BError, …>, …>' is not assignable to 'ExhaustiveMatch<unknown>'
Passing the matcher to match() produces a matcher-over-a-matcher and fails. So the gap is specifically the callback that ignores its parameter and sources exhaustiveness from somewhere else.
Suggested detection
Purely syntactic, no type information required — same JS-plugin model as the other rules. For each .recoverErrCases(cb) / .mapErrCases(cb) / .flatMapErrCases(cb) / .tapErrCases(cb) / .flatTapErrCases(cb):
- Report when
cb's first parameter is absent (() => …), or present but never referenced in the body. - Optionally also report any call to unthrown's
match(...)inside such a callback: within a…Casesbody there is no legitimate reason to build a second matcher, and this catches the case where someone references the matcher parameter trivially (void matcher) while still returning a foreign builder.
I don't think an escape hatch is needed — a …Cases callback that does not use its matcher is never what you meant.
Prevalence
Being straight about this one: it is preventive, not a cleanup rule.
I scanned the same ~264-project TypeScript monorepo as #166 (18 packages on unthrown, now on 5.0.0): 210 …Cases call sites across 72 files, 0 of them ignoring the matcher. Nobody has fallen into it yet.
What makes it worth a rule anyway is the failure profile rather than the frequency: it type-checks, it does not throw at the call site, and outcome (1) produces a plausible wrong value rather than a crash. It is the kind of thing that gets found in production, once, by someone reading a log line that says the wrong error was recovered.