dotnet / dotnet/roslyn

Unexpected nullable warning on expression used in tuple assignment

Open
#77,245 2 comments 0 reactions 0 assignees View on GitHub
Area-Compilers Bug Feature - Nullable Reference Types Feature - Tuples
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**:
17.13.0

**Steps to Reproduce**:

```csharp
#nullable enable

Int32 i;
C? c = null;

if (c is not null)
{
c = c.NoTuple(); // No warning (as expected)
}

if (c is not null)
{
(c, i) = c.Tuple(); // Unexpected CS8602 warning on 'c'
}

class C
{
public C? NoTuple() => throw new NotImplementedException();

public (C?, Int32) Tuple() => throw new NotImplementedException();
}
```

**Diagnostic Id**:
CS8602: Dereference of a possibly null reference.

**Expected Behavior**:
No warnings.

**Actual Behavior**:
A warning on a variable known to be non-null.

**Notes**:
Swapping the order of the tuple elements doesn't produce the warning.

Contributor guide

Open the contributing guide

Research direction

No source file or test is named. Start by running the nullable-enabled reproduction in the issue and tracing the CS8602 diagnostic for the tuple assignment; the fix is complete when the shown assignment produces no warning while the existing behavior remains covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.