ADR-0030: A discarded Result fails the build (RSLT001)
- Status: accepted
- Date: 2026-10-11
- Prior art: Trellis by Xavier John (MIT; no affiliation, the shared name is a coincidence): a build-time analyzer that reports a result nobody handles. The idea only; this analyzer is written from scratch. A generated project would rename that repository's name, so the line stays in the template's repository, beside the README's Credits.
Context
Section titled “Context”ADR-0004 makes failure part of the type, and nothing enforced it. invoice.Issue(now); return Result.Success();
compiles, commits a transition the aggregate refused, and answers 2xx. An awaited
await handler.Handle(command, ct); in a test drops the outcome the test was meant to check, and
ASP.NET Core Identity reports a failed user or role write as an IdentityResult value rather than an
exception: a seeding step that dropped one once logged success and left no administrator.
The first guard was CA1806 with additional_use_results_methods listing every Domain member and
Result combinator by documentation-ID prefix. It matched by name, not by type, so a handler, an
Application port, a Persistence method or an IdentityResult was never covered, and it never saw
through await, the most common way a Result is dropped. When the analyzer below was first wired in,
it found 33 statements that CA1806 had passed, all in tests: 14 NSubstitute call checks and 19 real
drops (identity seeding in the ApiTests and IntegrationTests fixtures that never checked an
IdentityResult, a handler test, and the use-case telemetry tests that never looked at the outcome
they caused); the all-options template build then found 3 more in multitenancy-only fixtures.
Decision
Section titled “Decision”- An in-repository analyzer,
tools/Trellis.Analyzers: one netstandard2.0 Roslyn component (IsRoslynComponent,EnforceExtendedAnalyzerRules, not packable), one analyzer class, one diagnostic id. RSLT001, "The result of this call is discarded", is a Warning by default; the Release and CI builds treat warnings as errors, so it fails them. The id is neutral, so the template rename leaves it alone. - What it reports, as two messages under the one id:
- A statement that drops a result: a direct call, an awaited
Task<T>orValueTask<T>of one (throughConfigureAwaittoo), a conditional access ending in such a call, an object creation (new X(dynamicArgument)andnew T()included),Wait()on a task of one, and the implicit statement of an expression-bodied lambda, local function or member that returns void (Action a = () => invoice.Issue(now);). An unawaited task of a result is reported wherever the compiler's CS4014 is silent (a sync method, a non-async lambda,maybe?.CommitAsync(ct)); where CS4014 fires, RSLT001 stays quiet, so each is reported once. A collection initializer whoseAddreturns a result is reported per element. - A task of a result turned into a plain
Task:Task<Result>converts toTaskimplicitly, sopublic Task Handle(…) => crm.PushPaymentAsync(…);(an outbox handler on an ADR-0020 port) compiles and lets the outbox checkpoint a failed push as done. A computedTask<Result>, or a sequence of them, converted toTaskin a return, a lambda body, a variable, an argument or a cast is reported, and so is the method-group form (Func<Task> work = port.PushAsync;).
- A statement that drops a result: a direct call, an awaited
- What it does not report: any assignment, so
_ = …is the way to drop a result on purpose; a return; an argument; a lambda whose return type is the result (Func<Result>); an awaited non-genericTask; a task of a result already held in a variable (it keeps its result there); NSubstitute's call check (await uow.Received(1).CommitAsync(ct)), which asserts that a call happened and returns a placeholder; an assertion that hands back the value it checked (result.Error.ShouldBeOfType<ValidationError>(): a method of Shouldly or xUnit, or of a class marked[ShouldlyMethods], unless what it checked is a delegate, as inShould.NotThrow(() => …)); the expression body of a delegate handed to an exception assertion (Should.ThrowAsync(() => handler.Handle(…)), any method whose name starts withThroworShouldThrow), which is expected to throw; a call that hands back a result something else holds (Tapon a held result,Stack.Pop,Queue.Dequeue,ConcurrentDictionary.GetOrAdd/AddOrUpdate,Interlocked.Exchange,Task.WhenAllorWhenAnyover tasks held in variables); a lambda converted to an expression tree, which never runs; and generated code, except Razor's: a component's@codeblock is hand-written C# that reaches the compiler as the Razor generator's output (*_razor.g.cs), so it is read and its diagnostics point at the.razorline. The EF migrations are generated code by the.editorconfigsection that namessrc/Infrastructure/*.Persistence/Migrations/, never any folder calledMigrations. - The result types: the SharedKernel
Result,Result<T>andError(a droppedErroris a forgottenreturn:if (invoice is null) { InvoiceErrors.NotFound(id); }falls through to success), matched by type name plus a containing namespace that ends inSharedKernel.Results(so it still matches after the template renames the root namespace); the UI'sApiResult<T>, in a namespace ending inUI.Common.Services(a droppedawait InvoiceApi.PayAsync(id)shows a refused payment as done); andMicrosoft.AspNetCore.Identity.IdentityResult, by its full name. A type derived from one counts (FieldError,ValidationError), as does a type parameter constrained to one, and a value that carries one where only it can reach it: an array, a sequence (IEnumerable<T>,IAsyncEnumerable<T>, soawait Task.WhenAll(a.CommitAsync(ct), b.CommitAsync(ct));and an iterator that never runs are reported), a tuple element, and the value of aTask<T>orValueTask<T>, four levels deep. - Wiring: the root
Directory.Build.propsgives every project undersrc/,tests/andtools/aProjectReferenceto the analyzer withOutputItemType="Analyzer",ReferenceOutputAssembly="false",PrivateAssets="all"andIsAspireProjectResource="false". The compiler loads it, no project references or copies it, it flows to no consumer, and the AppHost does not take it for a service. The item is conditioned on the project name, so the analyzer does not load itself and the condition renames with the path.eng/has its own empty props and never sees it.ArchitectureTestsalso references the assembly, withMicrosoft.CodeAnalysis.CSharp, to run it over snippets inResultDiscardAnalyzerTests. - Packages:
Microsoft.CodeAnalysis.CSharp5.0.0 andMicrosoft.CodeAnalysis.Analyzers3.11.0, both MIT,PrivateAssets="all", pinned inDirectory.Packages.props(ADR-0029). There is no code-fix provider. A fix lives in its own assembly and needsMicrosoft.CodeAnalysis.CSharp.Workspaces, a third package, and the edit it would make is typing_ =or handling the value. - The version is a ceiling on the compiler. A compiler older than the Roslyn an analyzer
references cannot load it (CS9057, an error in Release), and an IDE that hosts an older Roslyn shows
no squiggle. A newer compiler loads it, so the pin is the lowest version that restores and has the
APIs the analyzer uses. The IOperation APIs have been stable for years, and 4.14.0 was the
intended choice. But transitive pinning applies the pin to
Microsoft.EntityFrameworkCore.Design10.0.12 too, which needsMicrosoft.CodeAnalysis.CSharp5.0.0 or later, so 4.14.0 fails restore with NU1109 in Persistence. 5.0.0 is the floor. The ceiling is the compiler of the SDK inglobal.json. SDK 10.0.401 ships compiler 5.9.0-1.26423.113 (itsRoslyn/bincore/csc.dll -version; SDK 10.0.302's prints 5.6.0-2.26329.109). Dependabot proposes only patch updates ofMicrosoft.CodeAnalysis.*, and the SDK-bump checklist indocs/development.mdsays how far the pin may move. - Coverage: the analyzer is build-time tooling, never deployed.
eng/coverage.runsettingsleaves it out andcoverage-summary.pynever counts it, so no ring floor depends on it. Its facts inResultDiscardAnalyzerTestsare its specification.
Consequences
Section titled “Consequences”- A dropped result is a red Release build in every project, tests included, and a warning in the
Debug loop. A deliberate drop reads
_ = …with a one-line reason; a test's arrange step ends inShouldBeSuccess(). - The generated project owns about 500 lines of analyzer (a third of them doc comments) and a test
class, and one coupling: the Roslyn pin moves with the SDK, by hand, never above its compiler, and
never below the floor EF Core Design sets. A microsoft-group Dependabot PR that fails restore with NU1109 on
Microsoft.CodeAnalysis.CSharpraises the pin in the same PR. - Gaps, each named so nobody assumes otherwise. The analyzer judges a value by its static type where
it is computed and does no flow analysis, so it does not report:
- a result stored and never read, a deconstructed tuple element never read (Sonar's S1481 is the
nearest check), or one read without its outcome: a null pattern (
invoice.Void() is not null), aswitchwith onlydefault, alock,ToString(),Count()orAny()over results, aforeachorawait foreachwhose loop variable nobody reads, or aContinueWithcontinuation that ignores the task it receives; - a result seen as
objector through a covariant delegate (a method returningobject,Func<object> f = () => invoice.Void();), or as an unconstrained type parameter (Drop<T>(Func<T> make) { make(); }, even whenTisResultat the call site); - a result inside a type of your own (
record struct Outcome(Result Result, int Count)): only tuples, arrays, sequences and tasks are looked through; - a
dynamiccall: a dynamic receiver, or one dynamic argument to a knownResultmethod, gives the call the typedynamic; - the
Addcalls of a C# 12 collection expression (LineBook book = [""];), which the operation tree does not show; - code marked generated by hand:
[GeneratedCode], a// <auto-generated/>header, or a.g.cs,.generated.cs,.g.i.csor.Designer.csname opts a hand-written file out, deliberately. The exemptions match by name: NSubstitute's by method name and namespace (a call insideReceived.InOrder(...)orWhen(...)is not exempt; write_ =there), the assertion and exception-assertion exemptions by library namespace and method-name prefix. Meziantou's MA0134 also reports an unawaited task in a sync method, so the two overlap there.
- a result stored and never read, a deconstructed tuple element never read (Sonar's S1481 is the
nearest check), or one read without its outcome: a null pattern (
Alternatives / Revisit when
Section titled “Alternatives / Revisit when”- CA1806 with
additional_use_results_methods(the stand-in, retired by this ADR): by name, never throughawait, and blind to every method outside the listed prefixes (handlers, ports, Persistence,IdentityResult). - The source project's own analyzer package: it matches only its own generic result type in its own
namespace, so it would be inert here and would miss the non-generic
Result. - JetBrains
[MustUseReturnValue]: an IDE hint, a package, and no failed build. - Revisit when the C# compiler itself reports a dropped value of a type marked must-use: the analyzer
then becomes an attribute on
ResultandResult<T>.
The documentation of Trellis.Templates 0.2.0 · release notes