未验证 提交 51917b53 编写于 作者: R Rikki Gibson 提交者: GitHub

Fix MemberNotNull attributes on substituted methods (#47818)

上级 7dad16ec
......@@ -323,6 +323,12 @@ internal override bool HasRuntimeSpecialName
public sealed override FlowAnalysisAnnotations FlowAnalysisAnnotations => UnderlyingMethod.FlowAnalysisAnnotations;
internal sealed override ImmutableArray<string> NotNullMembers => UnderlyingMethod.NotNullMembers;
internal sealed override ImmutableArray<string> NotNullWhenTrueMembers => UnderlyingMethod.NotNullWhenTrueMembers;
internal sealed override ImmutableArray<string> NotNullWhenFalseMembers => UnderlyingMethod.NotNullWhenFalseMembers;
internal override bool ReturnValueIsMarshalledExplicitly
{
get
......
......@@ -22500,6 +22500,274 @@ public C()
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNullWhenTrue_Generic_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C<T>
{
public T field1;
public T? field2;
public T field3;
public T? field4;
public C()
{
if (Init())
{
field1.ToString();
field2.ToString();
field3.ToString(); // 1
field4.ToString(); // 2
}
else
{
throw null!;
}
}
[MemberNotNullWhen(true, nameof(field1), nameof(field2))]
bool Init() => throw null!;
}
", MemberNotNullWhenAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (16,13): warning CS8602: Dereference of a possibly null reference.
// field3.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field3").WithLocation(16, 13),
// (17,13): warning CS8602: Dereference of a possibly null reference.
// field4.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field4").WithLocation(17, 13)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNullWhenTrue_Generic_02()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
class GetResult<T>
{
[MemberNotNullWhen(true, ""Value"")]
public bool OK { get; set; }
public T? Value { get; init; }
}
record Manager(int Age);
class Archive
{
readonly Dictionary<string, Manager> Dict = new Dictionary<string, Manager>();
public GetResult<Manager> this[string key]
=> Dict.TryGetValue(key, out var value)
? new GetResult<Manager> { OK = true, Value = value }
: new GetResult<Manager> { OK = false, Value = null };
}
public class C
{
public void M()
{
Archive archive = new Archive();
var result = archive[""John""];
int age1 = result.OK
? result.Value.Age
: result.Value.Age; // 1
}
}
", MemberNotNullWhenAttributeDefinition, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (33,15): warning CS8602: Dereference of a possibly null reference.
// : result.Value.Age; // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "result.Value", isSuppressed: false).WithLocation(33, 15)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNullWhenFalse_Generic_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C<T> where T : new()
{
[MemberNotNullWhen(false, ""Value"")]
public bool IsBad { get; set; }
public T? Value { get; set; }
}
public class Program
{
public void M(C<object> c)
{
_ = c.IsBad
? c.Value.ToString() // 1
: c.Value.ToString();
}
}
", MemberNotNullWhenAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (16,15): warning CS8602: Dereference of a possibly null reference.
// ? c.Value.ToString() // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c.Value", isSuppressed: false).WithLocation(16, 15)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNull_Generic_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C<T> where T : new()
{
[MemberNotNull(""Value"")]
public void Init() { Value = new T(); }
public T? Value { get; set; }
}
public class Program
{
public void M(bool b, C<object> c)
{
if (b) c.Value.ToString(); // 1
c.Init();
c.Value.ToString();
}
}
", MemberNotNullAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (15,16): warning CS8602: Dereference of a possibly null reference.
// if (b) c.Value.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c.Value", isSuppressed: false).WithLocation(15, 16)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNull_Extension_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C
{
public string? _field;
}
public static class Ext
{
public static string? _field;
[MemberNotNull(""_field"")]
public static void AssertFieldNotNull(this C c) { if (_field == null) throw null!; }
}
class Program
{
void M1(C c)
{
c.AssertFieldNotNull();
Ext._field.ToString();
c._field.ToString(); // 1
}
void M2(C c)
{
Ext.AssertFieldNotNull(c);
Ext._field.ToString();
c._field.ToString(); // 2
}
}
", MemberNotNullAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (23,9): warning CS8602: Dereference of a possibly null reference.
// c._field.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(23, 9),
// (30,9): warning CS8602: Dereference of a possibly null reference.
// c._field.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(30, 9)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNullWhen_Extension_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C
{
public string? _field;
}
public static class Ext
{
public static string? _field;
[MemberNotNullWhen(true, ""_field"")]
public static bool IsFieldPresent(this C c) { return _field != null; }
[MemberNotNullWhen(false, ""_field"")]
public static bool IsFieldAbsent(this C c) { return _field == null; }
}
class Program
{
void M1(C c)
{
_ = c.IsFieldPresent()
? Ext._field.ToString()
: Ext._field.ToString(); // 1
_ = c.IsFieldPresent()
? c._field.ToString() // 2
: c._field.ToString(); // 3
}
void M2(C c)
{
_ = c.IsFieldAbsent()
? Ext._field.ToString() // 4
: Ext._field.ToString();
_ = c.IsFieldAbsent()
? c._field.ToString() // 5
: c._field.ToString(); // 6
}
}
", MemberNotNullWhenAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (26,15): warning CS8602: Dereference of a possibly null reference.
// : Ext._field.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Ext._field", isSuppressed: false).WithLocation(26, 15),
// (29,15): warning CS8602: Dereference of a possibly null reference.
// ? c._field.ToString() // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(29, 15),
// (30,15): warning CS8602: Dereference of a possibly null reference.
// : c._field.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(30, 15),
// (36,15): warning CS8602: Dereference of a possibly null reference.
// ? Ext._field.ToString() // 4
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Ext._field", isSuppressed: false).WithLocation(36, 15),
// (40,15): warning CS8602: Dereference of a possibly null reference.
// ? c._field.ToString() // 5
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(40, 15),
// (41,15): warning CS8602: Dereference of a possibly null reference.
// : c._field.ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(41, 15)
);
}
[Fact]
public void MemberNotNullWhenTrue_NonConstantBool()
{
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册