|
| 1 | +// Original code: https://github.com/dotnet/runtime/blob/419e949d258ecee4c40a460fb09c66d974229623/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs |
| 2 | + |
| 3 | +#define INTERNAL_NULLABLE_ATTRIBUTES |
| 4 | +#if NETSTANDARD2_0 |
| 5 | + |
| 6 | +// Licensed to the .NET Foundation under one or more agreements. |
| 7 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 8 | +// See the LICENSE file in the project root for more information. |
| 9 | + |
| 10 | +namespace System.Diagnostics.CodeAnalysis |
| 11 | +{ |
| 12 | + /// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary> |
| 13 | + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] |
| 14 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 15 | + internal |
| 16 | +#else |
| 17 | + public |
| 18 | +#endif |
| 19 | + sealed class AllowNullAttribute : Attribute { } |
| 20 | + |
| 21 | + /// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary> |
| 22 | + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] |
| 23 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 24 | + internal |
| 25 | +#else |
| 26 | + public |
| 27 | +#endif |
| 28 | + sealed class DisallowNullAttribute : Attribute { } |
| 29 | + |
| 30 | + /// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary> |
| 31 | + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] |
| 32 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 33 | + internal |
| 34 | +#else |
| 35 | + public |
| 36 | +#endif |
| 37 | + sealed class MaybeNullAttribute : Attribute { } |
| 38 | + |
| 39 | + /// <summary>Specifies that an output will not be null even if the corresponding type allows it. Specifies that an input argument was not null when the call returns.</summary> |
| 40 | + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] |
| 41 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 42 | + internal |
| 43 | +#else |
| 44 | + public |
| 45 | +#endif |
| 46 | + sealed class NotNullAttribute : Attribute { } |
| 47 | + |
| 48 | + /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary> |
| 49 | + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] |
| 50 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 51 | + internal |
| 52 | +#else |
| 53 | + public |
| 54 | +#endif |
| 55 | + sealed class MaybeNullWhenAttribute : Attribute |
| 56 | + { |
| 57 | + /// <summary>Initializes the attribute with the specified return value condition.</summary> |
| 58 | + /// <param name="returnValue"> |
| 59 | + /// The return value condition. If the method returns this value, the associated parameter may be null. |
| 60 | + /// </param> |
| 61 | + public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; |
| 62 | + |
| 63 | + /// <summary>Gets the return value condition.</summary> |
| 64 | + public bool ReturnValue { get; } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary> |
| 68 | + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] |
| 69 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 70 | + internal |
| 71 | +#else |
| 72 | + public |
| 73 | +#endif |
| 74 | + sealed class NotNullWhenAttribute : Attribute |
| 75 | + { |
| 76 | + /// <summary>Initializes the attribute with the specified return value condition.</summary> |
| 77 | + /// <param name="returnValue"> |
| 78 | + /// The return value condition. If the method returns this value, the associated parameter will not be null. |
| 79 | + /// </param> |
| 80 | + public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; |
| 81 | + |
| 82 | + /// <summary>Gets the return value condition.</summary> |
| 83 | + public bool ReturnValue { get; } |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary> |
| 87 | + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] |
| 88 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 89 | + internal |
| 90 | +#else |
| 91 | + public |
| 92 | +#endif |
| 93 | + sealed class NotNullIfNotNullAttribute : Attribute |
| 94 | + { |
| 95 | + /// <summary>Initializes the attribute with the associated parameter name.</summary> |
| 96 | + /// <param name="parameterName"> |
| 97 | + /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null. |
| 98 | + /// </param> |
| 99 | + public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName; |
| 100 | + |
| 101 | + /// <summary>Gets the associated parameter name.</summary> |
| 102 | + public string ParameterName { get; } |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary>Applied to a method that will never return under any circumstance.</summary> |
| 106 | + [AttributeUsage(AttributeTargets.Method, Inherited = false)] |
| 107 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 108 | + internal |
| 109 | +#else |
| 110 | + public |
| 111 | +#endif |
| 112 | + sealed class DoesNotReturnAttribute : Attribute { } |
| 113 | + |
| 114 | + /// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary> |
| 115 | + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] |
| 116 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 117 | + internal |
| 118 | +#else |
| 119 | + public |
| 120 | +#endif |
| 121 | + sealed class DoesNotReturnIfAttribute : Attribute |
| 122 | + { |
| 123 | + /// <summary>Initializes the attribute with the specified parameter value.</summary> |
| 124 | + /// <param name="parameterValue"> |
| 125 | + /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to |
| 126 | + /// the associated parameter matches this value. |
| 127 | + /// </param> |
| 128 | + public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue; |
| 129 | + |
| 130 | + /// <summary>Gets the condition parameter value.</summary> |
| 131 | + public bool ParameterValue { get; } |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary> |
| 135 | + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] |
| 136 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 137 | + internal |
| 138 | +#else |
| 139 | + public |
| 140 | +#endif |
| 141 | + sealed class MemberNotNullAttribute : Attribute |
| 142 | + { |
| 143 | + /// <summary>Initializes the attribute with a field or property member.</summary> |
| 144 | + /// <param name="member"> |
| 145 | + /// The field or property member that is promised to be not-null. |
| 146 | + /// </param> |
| 147 | + public MemberNotNullAttribute(string member) => Members = new[] { member }; |
| 148 | + |
| 149 | + /// <summary>Initializes the attribute with the list of field and property members.</summary> |
| 150 | + /// <param name="members"> |
| 151 | + /// The list of field and property members that are promised to be not-null. |
| 152 | + /// </param> |
| 153 | + public MemberNotNullAttribute(params string[] members) => Members = members; |
| 154 | + |
| 155 | + /// <summary>Gets field or property member names.</summary> |
| 156 | + public string[] Members { get; } |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary>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.</summary> |
| 160 | + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] |
| 161 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 162 | + internal |
| 163 | +#else |
| 164 | + public |
| 165 | +#endif |
| 166 | + sealed class MemberNotNullWhenAttribute : Attribute |
| 167 | + { |
| 168 | + /// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary> |
| 169 | + /// <param name="returnValue"> |
| 170 | + /// The return value condition. If the method returns this value, the associated parameter will not be null. |
| 171 | + /// </param> |
| 172 | + /// <param name="member"> |
| 173 | + /// The field or property member that is promised to be not-null. |
| 174 | + /// </param> |
| 175 | + public MemberNotNullWhenAttribute(bool returnValue, string member) |
| 176 | + { |
| 177 | + ReturnValue = returnValue; |
| 178 | + Members = new[] { member }; |
| 179 | + } |
| 180 | + |
| 181 | + /// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary> |
| 182 | + /// <param name="returnValue"> |
| 183 | + /// The return value condition. If the method returns this value, the associated parameter will not be null. |
| 184 | + /// </param> |
| 185 | + /// <param name="members"> |
| 186 | + /// The list of field and property members that are promised to be not-null. |
| 187 | + /// </param> |
| 188 | + public MemberNotNullWhenAttribute(bool returnValue, params string[] members) |
| 189 | + { |
| 190 | + ReturnValue = returnValue; |
| 191 | + Members = members; |
| 192 | + } |
| 193 | + |
| 194 | + /// <summary>Gets the return value condition.</summary> |
| 195 | + public bool ReturnValue { get; } |
| 196 | + |
| 197 | + /// <summary>Gets field or property member names.</summary> |
| 198 | + public string[] Members { get; } |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +#endif |
0 commit comments