dotnet / dotnet/roslyn

Unexpected Nullability warnings for properties

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

Description

```cs
#nullable enable

class C1
{
public string? Value {get;set;}
}

class C2 : C1;

class C3
{
public virtual string? Value {get;set;}
}

class C4 : C3
{
public override string? Value {get;set;}
}

class Program
{
static void Test1(C2 x)
{
if (x.Value is null) return;

C1 c1 = x;
c1.Value.ToString();
}

static void Test2(C1 x)
{
if (x.Value is null) return;

C2 c2 = (C2)x;
c2.Value.ToString();
}

static void Test1(C4 x)
{
if (x.Value is null) return;

C3 c3 = x;
c3.Value.ToString();
}

static void Test2(C3 x)
{
if (x.Value is null) return;

C4 c4 = (C4)x;
c4.Value.ToString();
}
}
```

Observed:
```cs
// (35,9): warning CS8602: Dereference of a possibly null reference.
// c2.Value.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c2.Value").WithLocation(35, 9),
// (43,9): warning CS8602: Dereference of a possibly null reference.
// c3.Value.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c3.Value").WithLocation(43, 9),
// (51,9): warning CS8602: Dereference of a possibly null reference.
// c4.Value.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c4.Value").WithLocation(51, 9)
```

Expected: No warnings, similar as for `c1.Value.ToString();`

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.