|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 6 | +// Licensed under the MIT License. |
| 7 | + |
| 8 | +#define INTERNAL_NULLABLE_ATTRIBUTES |
| 9 | + |
| 10 | +#pragma warning disable SA1402 // File may only contain a single type |
| 11 | +#pragma warning disable SA1649 // File name should match first type name |
| 12 | + |
| 13 | +#if !NETCOREAPP3_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER || NET461 |
| 14 | +namespace System.Diagnostics.CodeAnalysis |
| 15 | +{ |
| 16 | + /// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary> |
| 17 | + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] |
| 18 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 19 | + internal |
| 20 | +#else |
| 21 | + public |
| 22 | +#endif |
| 23 | + sealed class AllowNullAttribute : Attribute |
| 24 | + { } |
| 25 | + |
| 26 | + /// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary> |
| 27 | + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] |
| 28 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 29 | + internal |
| 30 | +#else |
| 31 | + public |
| 32 | +#endif |
| 33 | + sealed class DisallowNullAttribute : Attribute |
| 34 | + { } |
| 35 | + |
| 36 | + /// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary> |
| 37 | + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] |
| 38 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 39 | + internal |
| 40 | +#else |
| 41 | + public |
| 42 | +#endif |
| 43 | + sealed class MaybeNullAttribute : Attribute |
| 44 | + { } |
| 45 | + |
| 46 | + /// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary> |
| 47 | + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] |
| 48 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 49 | + internal |
| 50 | +#else |
| 51 | + public |
| 52 | +#endif |
| 53 | + sealed class NotNullAttribute : Attribute |
| 54 | + { } |
| 55 | + |
| 56 | + /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary> |
| 57 | + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] |
| 58 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 59 | + internal |
| 60 | +#else |
| 61 | + public |
| 62 | +#endif |
| 63 | + sealed class MaybeNullWhenAttribute : Attribute |
| 64 | + { |
| 65 | + /// <summary>Initializes the attribute with the specified return value condition.</summary> |
| 66 | + /// <param name="returnValue"> |
| 67 | + /// The return value condition. If the method returns this value, the associated parameter may be null. |
| 68 | + /// </param> |
| 69 | + public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; |
| 70 | + |
| 71 | + /// <summary>Gets the return value condition.</summary> |
| 72 | + public bool ReturnValue { get; } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary> |
| 76 | + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] |
| 77 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 78 | + internal |
| 79 | +#else |
| 80 | + public |
| 81 | +#endif |
| 82 | + sealed class NotNullWhenAttribute : Attribute |
| 83 | + { |
| 84 | + /// <summary>Initializes the attribute with the specified return value condition.</summary> |
| 85 | + /// <param name="returnValue"> |
| 86 | + /// The return value condition. If the method returns this value, the associated parameter will not be null. |
| 87 | + /// </param> |
| 88 | + public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; |
| 89 | + |
| 90 | + /// <summary>Gets the return value condition.</summary> |
| 91 | + public bool ReturnValue { get; } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary> |
| 95 | + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] |
| 96 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 97 | + internal |
| 98 | +#else |
| 99 | + public |
| 100 | +#endif |
| 101 | + sealed class NotNullIfNotNullAttribute : Attribute |
| 102 | + { |
| 103 | + /// <summary>Initializes the attribute with the associated parameter name.</summary> |
| 104 | + /// <param name="parameterName"> |
| 105 | + /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null. |
| 106 | + /// </param> |
| 107 | + public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName; |
| 108 | + |
| 109 | + /// <summary>Gets the associated parameter name.</summary> |
| 110 | + public string ParameterName { get; } |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary>Applied to a method that will never return under any circumstance.</summary> |
| 114 | + [AttributeUsage(AttributeTargets.Method, Inherited = false)] |
| 115 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 116 | + internal |
| 117 | +#else |
| 118 | + public |
| 119 | +#endif |
| 120 | + sealed class DoesNotReturnAttribute : Attribute |
| 121 | + { } |
| 122 | + |
| 123 | + /// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary> |
| 124 | + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] |
| 125 | +#if INTERNAL_NULLABLE_ATTRIBUTES |
| 126 | + internal |
| 127 | +#else |
| 128 | + public |
| 129 | +#endif |
| 130 | + sealed class DoesNotReturnIfAttribute : Attribute |
| 131 | + { |
| 132 | + /// <summary>Initializes the attribute with the specified parameter value.</summary> |
| 133 | + /// <param name="parameterValue"> |
| 134 | + /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to |
| 135 | + /// the associated parameter matches this value. |
| 136 | + /// </param> |
| 137 | + public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue; |
| 138 | + |
| 139 | + /// <summary>Gets the condition parameter value.</summary> |
| 140 | + public bool ParameterValue { get; } |
| 141 | + } |
| 142 | +} |
| 143 | +#endif |
0 commit comments