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