Skip to content

Commit

Permalink
Add nullability annotations to System.Private.Xml.Linq project (dotne…
Browse files Browse the repository at this point in the history
…t#40744)

* Add nullability annotations to Xml.Linq project

* Fix misplaced assertion

* Address feedback

* Add missing NotNullIfNotNull attributes to operators
  • Loading branch information
jozkee authored and carlossanlop committed Aug 28, 2020
1 parent 10068f4 commit c131a1a
Show file tree
Hide file tree
Showing 27 changed files with 809 additions and 738 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<RootNamespace>System.Xml</RootNamespace>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)System\Collections\Generic\EnumerableHelpers.cs"
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;

using CultureInfo = System.Globalization.CultureInfo;
Expand Down Expand Up @@ -29,7 +31,7 @@ public static IEnumerable<XAttribute> EmptySequence
}
}

internal XAttribute next;
internal XAttribute? next;
internal XName name;
internal string value;

Expand Down Expand Up @@ -105,7 +107,7 @@ public XName Name
/// If this attribute does not have a parent, or if there is no next attribute,
/// then this property returns null.
/// </remarks>
public XAttribute NextAttribute
public XAttribute? NextAttribute
{
get { return parent != null && ((XElement)parent).lastAttr != this ? next : null; }
}
Expand All @@ -131,15 +133,15 @@ public override XmlNodeType NodeType
/// If this attribute does not have a parent, or if there is no previous attribute,
/// then this property returns null.
/// </remarks>
public XAttribute PreviousAttribute
public XAttribute? PreviousAttribute
{
get
{
if (parent == null) return null;
XAttribute a = ((XElement)parent).lastAttr;
XAttribute a = ((XElement)parent).lastAttr!;
while (a.next != this)
{
a = a.next;
a = a.next!;
}
return a != ((XElement)parent).lastAttr ? a : null;
}
Expand Down Expand Up @@ -226,7 +228,8 @@ public override string ToString()
/// The content of this <see cref="XAttribute"/> as a <see cref="string"/>.
/// </returns>
[CLSCompliant(false)]
public static explicit operator string(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator string?(XAttribute? attribute)
{
if (attribute == null) return null;
return attribute.value;
Expand Down Expand Up @@ -261,7 +264,8 @@ public static explicit operator bool(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as a <see cref="bool"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator bool?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator bool?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToBoolean(attribute.value.ToLowerInvariant());
Expand Down Expand Up @@ -296,6 +300,7 @@ public static explicit operator int(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as an <see cref="int"/>?.
/// </returns>
[CLSCompliant(false)]
[return: NotNullIfNotNull("attribute")]
public static explicit operator int?(XAttribute attribute)
{
if (attribute == null) return null;
Expand Down Expand Up @@ -331,7 +336,8 @@ public static explicit operator uint(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as an <see cref="uint"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator uint?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator uint?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToUInt32(attribute.value);
Expand Down Expand Up @@ -366,7 +372,8 @@ public static explicit operator long(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as a <see cref="long"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator long?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator long?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToInt64(attribute.value);
Expand Down Expand Up @@ -401,7 +408,8 @@ public static explicit operator ulong(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as an <see cref="ulong"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator ulong?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator ulong?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToUInt64(attribute.value);
Expand Down Expand Up @@ -436,7 +444,8 @@ public static explicit operator float(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as a <see cref="float"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator float?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator float?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToSingle(attribute.value);
Expand Down Expand Up @@ -471,7 +480,8 @@ public static explicit operator double(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as a <see cref="double"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator double?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator double?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToDouble(attribute.value);
Expand Down Expand Up @@ -506,7 +516,8 @@ public static explicit operator decimal(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as a <see cref="decimal"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator decimal?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator decimal?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToDecimal(attribute.value);
Expand Down Expand Up @@ -541,7 +552,8 @@ public static explicit operator DateTime(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as a <see cref="DateTime"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator DateTime?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator DateTime?(XAttribute? attribute)
{
if (attribute == null) return null;
return DateTime.Parse(attribute.value, CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind);
Expand Down Expand Up @@ -576,7 +588,8 @@ public static explicit operator DateTimeOffset(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as a <see cref="DateTimeOffset"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator DateTimeOffset?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator DateTimeOffset?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToDateTimeOffset(attribute.value);
Expand Down Expand Up @@ -611,7 +624,8 @@ public static explicit operator TimeSpan(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as a <see cref="TimeSpan"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator TimeSpan?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator TimeSpan?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToTimeSpan(attribute.value);
Expand Down Expand Up @@ -646,7 +660,8 @@ public static explicit operator Guid(XAttribute attribute)
/// The content of this <see cref="XAttribute"/> as a <see cref="Guid"/>?.
/// </returns>
[CLSCompliant(false)]
public static explicit operator Guid?(XAttribute attribute)
[return: NotNullIfNotNull("attribute")]
public static explicit operator Guid?(XAttribute? attribute)
{
if (attribute == null) return null;
return XmlConvert.ToGuid(attribute.value);
Expand All @@ -657,7 +672,7 @@ internal int GetDeepHashCode()
return name.GetHashCode() ^ value.GetHashCode();
}

internal string GetPrefixOfNamespace(XNamespace ns)
internal string? GetPrefixOfNamespace(XNamespace ns)
{
string namespaceName = ns.NamespaceName;
if (namespaceName.Length == 0) return string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal override XNode CloneNode()

internal override bool DeepEquals(XNode node)
{
XComment other = node as XComment;
XComment? other = node as XComment;
return other != null && value == other.value;
}

Expand Down
Loading

0 comments on commit c131a1a

Please sign in to comment.