From 90ab57c5626df29fbb58e53612defac02833eb8a Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Thu, 19 Mar 2020 11:31:17 -0700 Subject: [PATCH] Add MemberNotNull/When attributes (#33567) * Add MemberNotNull/When attributes * Apply [CLSCompliant(false)] * Fix typos and indent * Move [CLSCompliant(false)] and remove AllowMultiple * Declare attributes in ref file * Store parameters into readonly properties * Don't use deconstruction * Qualify, move properties, remove this --- .../Diagnostics/CodeAnalysis/NullableAttributes.cs | 51 ++++++++++++++++++++++ src/libraries/System.Runtime/ref/System.Runtime.cs | 15 +++++++ 2 files changed, 66 insertions(+) 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 0b24c01..35a5948 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 @@ -125,4 +125,55 @@ namespace System.Diagnostics.CodeAnalysis /// Gets the condition parameter value. public bool ParameterValue { get; } } + + /// 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)] +#if INTERNAL_NULLABLE_ATTRIBUTES + internal +#else + [CLSCompliant(false)] + public +#endif + sealed class MemberNotNullAttribute : Attribute + { + /// 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; + + /// 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)] +#if INTERNAL_NULLABLE_ATTRIBUTES + internal +#else + [CLSCompliant(false)] + public +#endif + sealed class MemberNotNullWhenAttribute : Attribute + { + /// 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. + /// + /// + /// The list of field and property members that are promised to be not-null. + /// + public MemberNotNullWhenAttribute(bool returnValue, params string[] members) + { + ReturnValue = returnValue; + Members = members; + } + + /// Gets the return value condition. + public bool ReturnValue { get; } + + /// Gets field or property member names. + public string[] Members { get; } + } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index d09b7e3..4771f24 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -5618,6 +5618,21 @@ namespace System.Diagnostics.CodeAnalysis public NotNullWhenAttribute(bool returnValue) { } public bool ReturnValue { get { throw null; } } } + [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)] + [System.CLSCompliant(false)] + public sealed class MemberNotNullAttribute : System.Attribute + { + public MemberNotNullAttribute(params string[] members) { } + public string[] Members { get { throw null; } } + } + [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)] + [System.CLSCompliant(false)] + public sealed class MemberNotNullWhenAttribute : System.Attribute + { + public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { } + public bool ReturnValue { get { throw null; } } + public string[] Members { get { throw null; } } + } [System.AttributeUsageAttribute(System.AttributeTargets.All, Inherited=false, AllowMultiple=true)] [System.Diagnostics.ConditionalAttribute("CODE_ANALYSIS")] public sealed partial class SuppressMessageAttribute : System.Attribute -- 2.7.4