dotnet / dotnet/roslyn

Incorrect inferred nullability for slice pattern

Open
#82,802 1 comment 0 reactions 1 assignee Claimed by @jcouv View on GitHub
Area-Compilers Bug Feature - Nullable Reference Types
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

It looks like that was intentional (`SetState(ref this.State, outputSlot, NullableFlowState.NotNull); // Slice value is assumed to be never null`) but the assumption is not necessarily correct.

```cs
#nullable enable

object? o1 = null;
if (Infer(o1) is [var x])
{
x.ToString(); // warning CS8602: Dereference of a possibly null reference.
}

object? o2 = null;
if (Infer(o2) is [.. var y])
{
y.ToString(); // missing warning
}

object? o3 = new object();
if (Infer(o3) is [var z1])
{
z1.ToString();
}

if (Infer(o3) is [.. var z2])
{
z2.ToString();
}

C Infer(T t) => throw null!;

class C
{
public int Length => 0;
public T this[int i] => throw null!;
public T Slice(int i, int j) => throw null!;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.