| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | #define INTERNAL_NULLABLE_ATTRIBUTES |
| | | 5 | | |
| | | 6 | | #pragma warning disable SA1402 // File may only contain a single type |
| | | 7 | | #pragma warning disable SA1649 // File name should match first type name |
| | | 8 | | |
| | | 9 | | namespace System.Diagnostics.CodeAnalysis |
| | | 10 | | { |
| | | 11 | | /// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary> |
| | | 12 | | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] |
| | | 13 | | #if INTERNAL_NULLABLE_ATTRIBUTES |
| | | 14 | | internal |
| | | 15 | | #else |
| | | 16 | | public |
| | | 17 | | #endif |
| | | 18 | | sealed class AllowNullAttribute : Attribute { } |
| | | 19 | | |
| | | 20 | | /// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary> |
| | | 21 | | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] |
| | | 22 | | #if INTERNAL_NULLABLE_ATTRIBUTES |
| | | 23 | | internal |
| | | 24 | | #else |
| | | 25 | | public |
| | | 26 | | #endif |
| | | 27 | | sealed class DisallowNullAttribute : Attribute { } |
| | | 28 | | |
| | | 29 | | /// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary> |
| | | 30 | | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.R |
| | | 31 | | #if INTERNAL_NULLABLE_ATTRIBUTES |
| | | 32 | | internal |
| | | 33 | | #else |
| | | 34 | | public |
| | | 35 | | #endif |
| | | 36 | | sealed class MaybeNullAttribute : Attribute { } |
| | | 37 | | |
| | | 38 | | /// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary> |
| | | 39 | | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.R |
| | | 40 | | #if INTERNAL_NULLABLE_ATTRIBUTES |
| | | 41 | | internal |
| | | 42 | | #else |
| | | 43 | | public |
| | | 44 | | #endif |
| | | 45 | | sealed class NotNullAttribute : Attribute { } |
| | | 46 | | |
| | | 47 | | /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the c |
| | | 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 |
| | | 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 |
| | | 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 no |
| | | 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 | | /// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified v |
| | | 114 | | [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] |
| | | 115 | | #if INTERNAL_NULLABLE_ATTRIBUTES |
| | | 116 | | internal |
| | | 117 | | #else |
| | | 118 | | public |
| | | 119 | | #endif |
| | | 120 | | sealed class DoesNotReturnIfAttribute : Attribute |
| | | 121 | | { |
| | | 122 | | /// <summary>Initializes the attribute with the specified parameter value.</summary> |
| | | 123 | | /// <param name="parameterValue"> |
| | | 124 | | /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the ar |
| | | 125 | | /// the associated parameter matches this value. |
| | | 126 | | /// </param> |
| | 0 | 127 | | public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue; |
| | | 128 | | |
| | | 129 | | /// <summary>Gets the condition parameter value.</summary> |
| | 0 | 130 | | public bool ParameterValue { get; } |
| | | 131 | | } |
| | | 132 | | } |