Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#85852·roslyn

Unions: spurious CS8655 on a switch over a union variable reassigned from a method call

Author: bernardnormierCreated Sep 30, 2026Updated Oct 1, 2026
LabelsArea-CompilersFeature - Unions

Version Used: .NET SDK 11.0.100-rc.1.26425.128, compiler 5.11.0-1.26425.128 (3551975be08744f0418857c5bed8ab1545c5dd47), <LangVersion>15.0</LangVersion> (also with preview),<Nullable>enable</Nullable>.

Steps to Reproduce:

csharp
Result<string, int> r = Make();
Console.WriteLine(r switch { string s => s, int f => $"{f}" }); // no warning

r = Make();
Console.WriteLine(r switch { string s => s, int f => $"{f}" }); // warning CS8655

static Result<string, int> Make() => "x";

public readonly union Result<TSuccess, TFailure>(TSuccess, TFailure);

Expected Behavior:

No warning: both switch expressions cover every case type of the union, and the second one is identical to the first.

Actual Behavior:

Program.cs(5,21): warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive).
For example, the pattern 'null' is not covered.

Notes

  • Two distinct variables, each assigned from a call and switched once, don't warn. Reassigning r from a value instead of a call (r = "y") doesn't warn either.
  • A single switch inside a loop warns too, when the call omits an optional CancellationToken parameter:
csharp
foreach (int i in new[] { 1, 2 })
{
    Result<string, int> r = Make(); // warning CS8655 on the switch below
    Console.WriteLine(r switch { string s => s, int f => $"{f}" });
}

static Result<string, int> Make(CancellationToken cancellationToken = default) => "x";

There is no warning with Make() declared without parameters, with Make(CancellationToken.None), or with an optional int or Guid parameter instead.

Source: dotnet/roslyn

View original on GitHubView discussion on GitHub