CodeAnalysisAttributes.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // ReSharper disable once CheckNamespace
  2. namespace System.Diagnostics.CodeAnalysis
  3. {
  4. /// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
  5. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
  6. internal sealed class AllowNullAttribute : Attribute
  7. { }
  8. /// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
  9. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
  10. internal sealed class DisallowNullAttribute : Attribute
  11. { }
  12. /// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
  13. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
  14. internal sealed class MaybeNullAttribute : Attribute
  15. { }
  16. /// <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>
  17. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
  18. internal sealed class NotNullAttribute : Attribute
  19. { }
  20. /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
  21. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  22. internal sealed class MaybeNullWhenAttribute : Attribute
  23. {
  24. /// <summary>Initializes the attribute with the specified return value condition.</summary>
  25. /// <param name="returnValue">
  26. /// The return value condition. If the method returns this value, the associated parameter may be null.
  27. /// </param>
  28. public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
  29. /// <summary>Gets the return value condition.</summary>
  30. public bool ReturnValue { get; }
  31. }
  32. /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
  33. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  34. internal sealed class NotNullWhenAttribute : Attribute
  35. {
  36. /// <summary>Initializes the attribute with the specified return value condition.</summary>
  37. /// <param name="returnValue">
  38. /// The return value condition. If the method returns this value, the associated parameter will not be null.
  39. /// </param>
  40. public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
  41. /// <summary>Gets the return value condition.</summary>
  42. public bool ReturnValue { get; }
  43. }
  44. /// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
  45. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
  46. internal sealed class NotNullIfNotNullAttribute : Attribute
  47. {
  48. /// <summary>Initializes the attribute with the associated parameter name.</summary>
  49. /// <param name="parameterName">
  50. /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
  51. /// </param>
  52. public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
  53. /// <summary>Gets the associated parameter name.</summary>
  54. public string ParameterName { get; }
  55. }
  56. /// <summary>Applied to a method that will never return under any circumstance.</summary>
  57. [AttributeUsage(AttributeTargets.Method, Inherited = false)]
  58. internal sealed class DoesNotReturnAttribute : Attribute
  59. { }
  60. /// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
  61. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  62. internal sealed class DoesNotReturnIfAttribute : Attribute
  63. {
  64. /// <summary>Initializes the attribute with the specified parameter value.</summary>
  65. /// <param name="parameterValue">
  66. /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
  67. /// the associated parameter matches this value.
  68. /// </param>
  69. public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
  70. /// <summary>Gets the condition parameter value.</summary>
  71. public bool ParameterValue { get; }
  72. }
  73. /// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
  74. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
  75. internal sealed class MemberNotNullAttribute : Attribute
  76. {
  77. /// <summary>Initializes the attribute with a field or property member.</summary>
  78. /// <param name="member">
  79. /// The field or property member that is promised to be not-null.
  80. /// </param>
  81. public MemberNotNullAttribute(string member) => Members = new[] { member };
  82. /// <summary>Initializes the attribute with the list of field and property members.</summary>
  83. /// <param name="members">
  84. /// The list of field and property members that are promised to be not-null.
  85. /// </param>
  86. public MemberNotNullAttribute(params string[] members) => Members = members;
  87. /// <summary>Gets field or property member names.</summary>
  88. public string[] Members { get; }
  89. }
  90. /// <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>
  91. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
  92. internal sealed class MemberNotNullWhenAttribute : Attribute
  93. {
  94. /// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
  95. /// <param name="returnValue">
  96. /// The return value condition. If the method returns this value, the associated parameter will not be null.
  97. /// </param>
  98. /// <param name="member">
  99. /// The field or property member that is promised to be not-null.
  100. /// </param>
  101. public MemberNotNullWhenAttribute(bool returnValue, string member)
  102. {
  103. ReturnValue = returnValue;
  104. Members = new[] { member };
  105. }
  106. /// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
  107. /// <param name="returnValue">
  108. /// The return value condition. If the method returns this value, the associated parameter will not be null.
  109. /// </param>
  110. /// <param name="members">
  111. /// The list of field and property members that are promised to be not-null.
  112. /// </param>
  113. public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
  114. {
  115. ReturnValue = returnValue;
  116. Members = members;
  117. }
  118. /// <summary>Gets the return value condition.</summary>
  119. public bool ReturnValue { get; }
  120. /// <summary>Gets field or property member names.</summary>
  121. public string[] Members { get; }
  122. }
  123. }