Skip to content

Commit

Permalink
Address feedback from Neal
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Mar 25, 2020
1 parent 2d0246d commit b7885a3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20293,6 +20293,8 @@ public C() // 1, 2
}

[MemberNotNull(null!, null!)]
[MemberNotNull(members: null!)]
[MemberNotNull(member: null!)]
void Init() => throw null!;
}
", MemberNotNullAttributeDefinition }, parseOptions: TestOptions.RegularPreview);
Expand Down Expand Up @@ -21894,6 +21896,51 @@ public C()
);
}

[Fact]
public void MemberNotNull_Multiple_ParamsConstructor()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C
{
public string field1;
public string field2;
public string field3;
public string field4;

public C()
{
Init();
field1.ToString();
field2.ToString();
field3.ToString();
field4.ToString();
}

[MemberNotNull(nameof(field1), nameof(field2))]
[MemberNotNull(nameof(field3), nameof(field4))]
void Init()
{
}
}
", MemberNotNullAttributeDefinition }, parseOptions: TestOptions.RegularPreview);

c.VerifyDiagnostics(
// (23,5): warning CS8774: Member 'field1' may not have a null value when exiting.
// }
Diagnostic(ErrorCode.WRN_MemberNotNull, "}").WithArguments("field1").WithLocation(23, 5),
// (23,5): warning CS8774: Member 'field2' may not have a null value when exiting.
// }
Diagnostic(ErrorCode.WRN_MemberNotNull, "}").WithArguments("field2").WithLocation(23, 5),
// (23,5): warning CS8774: Member 'field3' may not have a null value when exiting.
// }
Diagnostic(ErrorCode.WRN_MemberNotNull, "}").WithArguments("field3").WithLocation(23, 5),
// (23,5): warning CS8774: Member 'field4' may not have a null value when exiting.
// }
Diagnostic(ErrorCode.WRN_MemberNotNull, "}").WithArguments("field4").WithLocation(23, 5)
);
}

[Fact]
public void MemberNotNull_Multiple_Property()
{
Expand Down Expand Up @@ -22396,6 +22443,8 @@ public C() // 1, 2
}

[MemberNotNullWhen(true, null!, null!)]
[MemberNotNullWhen(true, members: null!)]
[MemberNotNullWhen(true, member: null!)]
bool Init() => throw null!;
}
", MemberNotNullWhenAttributeDefinition }, parseOptions: TestOptions.RegularPreview);
Expand Down Expand Up @@ -23442,6 +23491,58 @@ public C()
);
}

[Fact]
public void MemberNotNullWhenTrue_Multiple_ParamsConstructor()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C
{
public string field1;
public string? field2;
public string field3;
public string? field4;

public C()
{
if (Init())
{
field1.ToString();
field2.ToString();
field3.ToString();
field4.ToString();
}
else
{
throw null!;
}
}

[MemberNotNullWhen(true, nameof(field1), nameof(field2))]
[MemberNotNullWhen(true, nameof(field3), nameof(field4))]
bool Init()
{
return true;
}
}
", MemberNotNullWhenAttributeDefinition }, parseOptions: TestOptions.RegularPreview);

c.VerifyDiagnostics(
// (29,9): warning CS8775: Member 'field1' may not have a null value when exiting with `True`.
// return true;
Diagnostic(ErrorCode.WRN_MemberNotNullWhen, "return true;").WithArguments("field1", "True").WithLocation(29, 9),
// (29,9): warning CS8775: Member 'field2' may not have a null value when exiting with `True`.
// return true;
Diagnostic(ErrorCode.WRN_MemberNotNullWhen, "return true;").WithArguments("field2", "True").WithLocation(29, 9),
// (29,9): warning CS8775: Member 'field3' may not have a null value when exiting with `True`.
// return true;
Diagnostic(ErrorCode.WRN_MemberNotNullWhen, "return true;").WithArguments("field3", "True").WithLocation(29, 9),
// (29,9): warning CS8775: Member 'field4' may not have a null value when exiting with `True`.
// return true;
Diagnostic(ErrorCode.WRN_MemberNotNullWhen, "return true;").WithArguments("field4", "True").WithLocation(29, 9)
);
}

[Fact]
public void MemberNotNullWhenTrue_Multiple_Property()
{
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/Core/Portable/MetadataReader/PEModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,15 +1244,15 @@ internal ImmutableArray<string> GetMemberNotNullAttributeValues(EntityHandle tok
{
if (ai.SignatureIndex == 0)
{
if (TryExtractStringValueFromAttribute(ai.Handle, out var extracted))
if (TryExtractStringValueFromAttribute(ai.Handle, out string extracted))
{
if (extracted is object)
{
result.Add(extracted);
}
}
}
else if (TryExtractStringArrayValueFromAttribute(ai.Handle, out var extracted2))
else if (TryExtractStringArrayValueFromAttribute(ai.Handle, out ImmutableArray<string> extracted2))
{
foreach (var value in extracted2)
{
Expand Down Expand Up @@ -1285,7 +1285,7 @@ internal ImmutableArray<string> GetMemberNotNullAttributeValues(EntityHandle tok
{
if (ai.SignatureIndex == 0)
{
if (TryExtractValueFromAttribute(ai.Handle, out var extracted, s_attributeBoolAndStringValueExtractor))
if (TryExtractValueFromAttribute(ai.Handle, out BoolAndStringData extracted, s_attributeBoolAndStringValueExtractor))
{
if (extracted.String is object)
{
Expand All @@ -1294,7 +1294,7 @@ internal ImmutableArray<string> GetMemberNotNullAttributeValues(EntityHandle tok
}
}
}
else if (TryExtractValueFromAttribute(ai.Handle, out var extracted2, s_attributeBoolAndStringArrayValueExtractor))
else if (TryExtractValueFromAttribute(ai.Handle, out BoolAndStringArrayData extracted2, s_attributeBoolAndStringArrayValueExtractor))
{
var whenResult = extracted2.Sense ? whenTrue : whenFalse;
foreach (var value in extracted2.Strings)
Expand Down
2 changes: 1 addition & 1 deletion src/Workspaces/Core/Portable/Differencing/TreeComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ internal TNode GetParent(TNode node)
{
var hasParent = TryGetParent(node, out var parent);
Debug.Assert(hasParent);
return parent;
return parent!;
}

internal TNode GetAncestor(TNode node, int level)
Expand Down

0 comments on commit b7885a3

Please sign in to comment.