NullableAttributes.cs 9.2 KB

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