Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Oct 14, 2024
1 parent f49a448 commit 3237e0e
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21178,6 +21178,51 @@ public C()
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field4").WithLocation(22, 9));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/56256")]
public void MemberNotNull_LocalFunction_WithinExtensionMethod()
{
var c = CreateNullableCompilation(["""
using System.Diagnostics.CodeAnalysis;
public class C
{
public string field1;
public string? field2;
public string field3;
public string? field4;

public C()
{
System.Action a = delegate()
{
init();

[MemberNotNull(nameof(field1), nameof(field2))]
void init() => throw null!;
};

field1.ToString(); // 1
field2.ToString(); // 2
field3.ToString(); // 3
field4.ToString(); // 4
}
}
""", MemberNotNullAttributeDefinition]);

c.VerifyDiagnostics(
// (19,9): warning CS8602: Dereference of a possibly null reference.
// field1.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field1").WithLocation(19, 9),
// (20,9): warning CS8602: Dereference of a possibly null reference.
// field2.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field2").WithLocation(20, 9),
// (21,9): warning CS8602: Dereference of a possibly null reference.
// field3.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field3").WithLocation(21, 9),
// (22,9): warning CS8602: Dereference of a possibly null reference.
// field4.ToString(); // 4
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field4").WithLocation(22, 9));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/56256")]
public void MemberNotNull_LocalFunction_TopLevel()
{
Expand Down

0 comments on commit 3237e0e

Please sign in to comment.