|
| 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 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +namespace System.Diagnostics.CodeAnalysis |
| 6 | +{ |
| 7 | + /// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary> |
| 8 | + [AttributeUsage (AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] |
| 9 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 10 | + internal |
| 11 | +#else |
| 12 | + public |
| 13 | +#endif |
| 14 | + sealed class AllowNullAttribute : Attribute |
| 15 | + { } |
| 16 | + |
| 17 | + /// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary> |
| 18 | + [AttributeUsage (AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] |
| 19 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 20 | + internal |
| 21 | +#else |
| 22 | + public |
| 23 | +#endif |
| 24 | + sealed class DisallowNullAttribute : Attribute |
| 25 | + { } |
| 26 | + |
| 27 | + /// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary> |
| 28 | + [AttributeUsage (AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] |
| 29 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 30 | + internal |
| 31 | +#else |
| 32 | + public |
| 33 | +#endif |
| 34 | + sealed class MaybeNullAttribute : Attribute |
| 35 | + { } |
| 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 | + |
| 47 | + /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary> |
| 48 | + [AttributeUsage (AttributeTargets.Parameter, Inherited = false)] |
| 49 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 50 | + internal |
| 51 | +#else |
| 52 | + public |
| 53 | +#endif |
| 54 | + sealed class MaybeNullWhenAttribute : Attribute |
| 55 | + { |
| 56 | + /// <summary>Initializes the attribute with the specified return value condition.</summary> |
| 57 | + /// <param name="returnValue"> |
| 58 | + /// The return value condition. If the method returns this value, the associated parameter may be null. |
| 59 | + /// </param> |
| 60 | + public MaybeNullWhenAttribute (bool returnValue) => ReturnValue = returnValue; |
| 61 | + |
| 62 | + /// <summary>Gets the return value condition.</summary> |
| 63 | + public bool ReturnValue { get; } |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary> |
| 67 | + [AttributeUsage (AttributeTargets.Parameter, Inherited = false)] |
| 68 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 69 | + internal |
| 70 | +#else |
| 71 | + public |
| 72 | +#endif |
| 73 | + sealed class NotNullWhenAttribute : Attribute |
| 74 | + { |
| 75 | + /// <summary>Initializes the attribute with the specified return value condition.</summary> |
| 76 | + /// <param name="returnValue"> |
| 77 | + /// The return value condition. If the method returns this value, the associated parameter will not be null. |
| 78 | + /// </param> |
| 79 | + public NotNullWhenAttribute (bool returnValue) => ReturnValue = returnValue; |
| 80 | + |
| 81 | + /// <summary>Gets the return value condition.</summary> |
| 82 | + public bool ReturnValue { get; } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary> |
| 86 | + [AttributeUsage (AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] |
| 87 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 88 | + internal |
| 89 | +#else |
| 90 | + public |
| 91 | +#endif |
| 92 | + sealed class NotNullIfNotNullAttribute : Attribute |
| 93 | + { |
| 94 | + /// <summary>Initializes the attribute with the associated parameter name.</summary> |
| 95 | + /// <param name="parameterName"> |
| 96 | + /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null. |
| 97 | + /// </param> |
| 98 | + public NotNullIfNotNullAttribute (string parameterName) => ParameterName = parameterName; |
| 99 | + |
| 100 | + /// <summary>Gets the associated parameter name.</summary> |
| 101 | + public string ParameterName { get; } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary>Applied to a method that will never return under any circumstance.</summary> |
| 105 | + [AttributeUsage (AttributeTargets.Method, Inherited = false)] |
| 106 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 107 | + internal |
| 108 | +#else |
| 109 | + public |
| 110 | +#endif |
| 111 | + sealed class DoesNotReturnAttribute : Attribute |
| 112 | + { } |
| 113 | + |
| 114 | + /// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary> |
| 115 | + [AttributeUsage (AttributeTargets.Parameter, Inherited = false)] |
| 116 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 117 | + internal |
| 118 | +#else |
| 119 | + public |
| 120 | +#endif |
| 121 | + sealed class DoesNotReturnIfAttribute : Attribute |
| 122 | + { |
| 123 | + /// <summary>Initializes the attribute with the specified parameter value.</summary> |
| 124 | + /// <param name="parameterValue"> |
| 125 | + /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to |
| 126 | + /// the associated parameter matches this value. |
| 127 | + /// </param> |
| 128 | + public DoesNotReturnIfAttribute (bool parameterValue) => ParameterValue = parameterValue; |
| 129 | + |
| 130 | + /// <summary>Gets the condition parameter value.</summary> |
| 131 | + public bool ParameterValue { get; } |
| 132 | + } |
| 133 | +} |
0 commit comments