From 8b971d6906db4338c82970fce92457611481da2d Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 24 Mar 2020 13:23:02 -0400 Subject: [PATCH] Add string ctor to MemberNotNull{When} With only the params string[] ctor, every usage of MemberNotNull{When} in a CLSCompliant(true) assembly needs to have its warning suppressed. --- .../CodeAnalysis/NullableAttributes.cs | 28 +++++++++++++++---- .../System.Runtime/ref/System.Runtime.cs | 8 +++--- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs index 35a59488cffed6..2c4e91c3e565ad 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs @@ -127,36 +127,52 @@ sealed class DoesNotReturnIfAttribute : Attribute } /// Specifies that the method or property will ensure that the listed field and property members have not-null values. - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)] + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] #if INTERNAL_NULLABLE_ATTRIBUTES internal #else - [CLSCompliant(false)] public #endif sealed class MemberNotNullAttribute : Attribute { + /// Initializes the attribute with a field or property member. + /// + /// The field or property member that is promised to be not-null. + /// + public MemberNotNullAttribute(string member) => Members = new[] { member }; + /// Initializes the attribute with the list of field and property members. /// /// The list of field and property members that are promised to be not-null. /// - public MemberNotNullAttribute(params string[] members) - => Members = members; + public MemberNotNullAttribute(params string[] members) => Members = members; /// Gets field or property member names. public string[] Members { get; } } /// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)] + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] #if INTERNAL_NULLABLE_ATTRIBUTES internal #else - [CLSCompliant(false)] public #endif sealed class MemberNotNullWhenAttribute : Attribute { + /// Initializes the attribute with the specified return value condition and a field or property member. + /// + /// The return value condition. If the method returns this value, the associated parameter will not be null. + /// + /// + /// The field or property member that is promised to be not-null. + /// + public MemberNotNullWhenAttribute(bool returnValue, string member) + { + ReturnValue = returnValue; + Members = new[] { member }; + } + /// Initializes the attribute with the specified return value condition and list of field and property members. /// /// The return value condition. If the method returns this value, the associated parameter will not be null. diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 86ded83c7ee94a..b70e7e5ca50037 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -5618,17 +5618,17 @@ public sealed partial class NotNullWhenAttribute : System.Attribute public NotNullWhenAttribute(bool returnValue) { } public bool ReturnValue { get { throw null; } } } - [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)] - [System.CLSCompliant(false)] + [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false, AllowMultiple = true)] public sealed class MemberNotNullAttribute : System.Attribute { + public MemberNotNullAttribute(string member) { } public MemberNotNullAttribute(params string[] members) { } public string[] Members { get { throw null; } } } - [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)] - [System.CLSCompliant(false)] + [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false, AllowMultiple = true)] public sealed class MemberNotNullWhenAttribute : System.Attribute { + public MemberNotNullWhenAttribute(bool returnValue, string member) { } public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { } public bool ReturnValue { get { throw null; } } public string[] Members { get { throw null; } }