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