NullableAttributes.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. //
  4. // This file is further modified from the original, for this project,
  5. // to comply with project style.
  6. // No changes are made which affect compatibility with the same types from
  7. // APIs later than netstandard2.0, nor will this file be included in compilations
  8. // targeted at later APIs.
  9. //
  10. // Originally rom https://github.com/dotnet/runtime/blob/ef72b95937703e485fdbbb75f3251fedfd1a0ef9/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs
  11. // ReSharper disable CheckNamespace
  12. // ReSharper disable UnusedAutoPropertyAccessor.Global
  13. // ReSharper disable UnusedType.Global
  14. namespace System.Diagnostics.CodeAnalysis;
  15. /// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
  16. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  17. [AttributeUsage (AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property)]
  18. [ExcludeFromCodeCoverage]
  19. [DebuggerNonUserCode]
  20. internal sealed class AllowNullAttribute : Attribute;
  21. /// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
  22. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  23. [AttributeUsage (AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property)]
  24. [ExcludeFromCodeCoverage]
  25. [DebuggerNonUserCode]
  26. internal sealed class DisallowNullAttribute : Attribute;
  27. /// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
  28. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  29. [AttributeUsage (AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue)]
  30. [ExcludeFromCodeCoverage]
  31. [DebuggerNonUserCode]
  32. internal sealed class MaybeNullAttribute : Attribute;
  33. /// <summary>
  34. /// Specifies that an output will not be null even if the corresponding type allows it. Specifies that an input
  35. /// argument was not null when the call returns.
  36. /// </summary>
  37. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  38. [AttributeUsage (AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue)]
  39. [ExcludeFromCodeCoverage]
  40. [DebuggerNonUserCode]
  41. internal sealed class NotNullAttribute : Attribute;
  42. /// <summary>
  43. /// Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding
  44. /// type disallows it.
  45. /// </summary>
  46. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  47. [AttributeUsage (AttributeTargets.Parameter)]
  48. [ExcludeFromCodeCoverage]
  49. [DebuggerNonUserCode]
  50. internal sealed class MaybeNullWhenAttribute : Attribute
  51. {
  52. /// <summary>Initializes the attribute with the specified return value condition.</summary>
  53. /// <param name="returnValue">
  54. /// The return value condition. If the method returns this value, the associated parameter may be null.
  55. /// </param>
  56. #pragma warning disable IDE0290 // Use primary constructor
  57. public MaybeNullWhenAttribute (bool returnValue) { ReturnValue = returnValue; }
  58. #pragma warning restore IDE0290 // Use primary constructor
  59. /// <summary>Gets the return value condition.</summary>
  60. public bool ReturnValue { get; }
  61. }
  62. /// <summary>
  63. /// Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the
  64. /// corresponding type allows it.
  65. /// </summary>
  66. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  67. [AttributeUsage (AttributeTargets.Parameter)]
  68. [ExcludeFromCodeCoverage]
  69. [DebuggerNonUserCode]
  70. internal 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. #pragma warning disable IDE0290 // Use primary constructor
  77. public NotNullWhenAttribute (bool returnValue) { ReturnValue = returnValue; }
  78. #pragma warning restore IDE0290 // Use primary constructor
  79. /// <summary>Gets the return value condition.</summary>
  80. public bool ReturnValue { get; }
  81. }
  82. /// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
  83. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  84. [AttributeUsage (AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true)]
  85. [ExcludeFromCodeCoverage]
  86. [DebuggerNonUserCode]
  87. internal 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. #pragma warning disable IDE0290 // Use primary constructor
  94. public NotNullIfNotNullAttribute (string parameterName) { ParameterName = parameterName; }
  95. #pragma warning restore IDE0290 // Use primary constructor
  96. /// <summary>Gets the associated parameter name.</summary>
  97. public string ParameterName { get; }
  98. }
  99. /// <summary>Applied to a method that will never return under any circumstance.</summary>
  100. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  101. [AttributeUsage (AttributeTargets.Method, Inherited = false)]
  102. [ExcludeFromCodeCoverage]
  103. [DebuggerNonUserCode]
  104. internal sealed class DoesNotReturnAttribute : Attribute;
  105. /// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
  106. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  107. [AttributeUsage (AttributeTargets.Parameter)]
  108. [ExcludeFromCodeCoverage]
  109. [DebuggerNonUserCode]
  110. internal sealed class DoesNotReturnIfAttribute : Attribute
  111. {
  112. /// <summary>Initializes the attribute with the specified parameter value.</summary>
  113. /// <param name="parameterValue">
  114. /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument
  115. /// to
  116. /// the associated parameter matches this value.
  117. /// </param>
  118. #pragma warning disable IDE0290 // Use primary constructor
  119. public DoesNotReturnIfAttribute (bool parameterValue) { ParameterValue = parameterValue; }
  120. #pragma warning restore IDE0290 // Use primary constructor
  121. /// <summary>Gets the condition parameter value.</summary>
  122. public bool ParameterValue { get; }
  123. }
  124. /// <summary>
  125. /// Specifies that the method or property will ensure that the listed field and property members have not-null
  126. /// values.
  127. /// </summary>
  128. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  129. [AttributeUsage (AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
  130. [ExcludeFromCodeCoverage]
  131. [DebuggerNonUserCode]
  132. internal sealed class MemberNotNullAttribute : Attribute
  133. {
  134. /// <summary>Initializes the attribute with a field or property member.</summary>
  135. /// <param name="member">
  136. /// The field or property member that is promised to be not-null.
  137. /// </param>
  138. public MemberNotNullAttribute (string member) { Members = [member]; }
  139. /// <summary>Initializes the attribute with the list of field and property members.</summary>
  140. /// <param name="members">
  141. /// The list of field and property members that are promised to be not-null.
  142. /// </param>
  143. public MemberNotNullAttribute (params string [] members) { Members = members; }
  144. /// <summary>Gets field or property member names.</summary>
  145. public string [] Members { get; }
  146. }
  147. /// <summary>
  148. /// Specifies that the method or property will ensure that the listed field and property members have not-null values
  149. /// when returning with the specified return value condition.
  150. /// </summary>
  151. /// <remarks>Excluded from output assembly via file specified in ApiCompatExcludeAttributesFile element in the project file.</remarks>
  152. [AttributeUsage (AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
  153. [ExcludeFromCodeCoverage]
  154. [DebuggerNonUserCode]
  155. internal sealed class MemberNotNullWhenAttribute : Attribute
  156. {
  157. /// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
  158. /// <param name="returnValue">
  159. /// The return value condition. If the method returns this value, the associated parameter will not be null.
  160. /// </param>
  161. /// <param name="member">
  162. /// The field or property member that is promised to be not-null.
  163. /// </param>
  164. public MemberNotNullWhenAttribute (bool returnValue, string member)
  165. {
  166. ReturnValue = returnValue;
  167. Members = [member];
  168. }
  169. /// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
  170. /// <param name="returnValue">
  171. /// The return value condition. If the method returns this value, the associated parameter will not be null.
  172. /// </param>
  173. /// <param name="members">
  174. /// The list of field and property members that are promised to be not-null.
  175. /// </param>
  176. public MemberNotNullWhenAttribute (bool returnValue, params string [] members)
  177. {
  178. ReturnValue = returnValue;
  179. Members = members;
  180. }
  181. /// <summary>Gets field or property member names.</summary>
  182. public string [] Members { get; }
  183. /// <summary>Gets the return value condition.</summary>
  184. public bool ReturnValue { get; }
  185. }