Skip to content

Commit

Permalink
Add MemberNotNull/When attributes (#33567)
Browse files Browse the repository at this point in the history
* Add MemberNotNull/When attributes

* Apply [CLSCompliant(false)]

* Fix typos and indent

* Move [CLSCompliant(false)] and remove AllowMultiple

* Declare attributes in ref file

* Store parameters into readonly properties

* Don't use deconstruction

* Qualify, move properties, remove this
  • Loading branch information
jcouv authored Mar 19, 2020
1 parent f80a514 commit 90ab57c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,55 @@ sealed class DoesNotReturnIfAttribute : Attribute
/// <summary>Gets the condition parameter value.</summary>
public bool ParameterValue { get; }
}

/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
[CLSCompliant(false)]
public
#endif
sealed class MemberNotNullAttribute : Attribute
{
/// <summary>Initializes the attribute with the list of field and property members.</summary>
/// <param name="members">
/// The list of field and property members that are promised to be not-null.
/// </param>
public MemberNotNullAttribute(params string[] members)
=> Members = members;

/// <summary>Gets field or property member names.</summary>
public string[] Members { get; }
}

/// <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>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
[CLSCompliant(false)]
public
#endif
sealed class MemberNotNullWhenAttribute : Attribute
{
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
/// <param name="returnValue">
/// The return value condition. If the method returns this value, the associated parameter will not be null.
/// </param>
/// <param name="members">
/// The list of field and property members that are promised to be not-null.
/// </param>
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
{
ReturnValue = returnValue;
Members = members;
}

/// <summary>Gets the return value condition.</summary>
public bool ReturnValue { get; }

/// <summary>Gets field or property member names.</summary>
public string[] Members { get; }
}
}
15 changes: 15 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5618,6 +5618,21 @@ public sealed partial class NotNullWhenAttribute : System.Attribute
public NotNullWhenAttribute(bool returnValue) { }
public bool ReturnValue { get { throw null; } }
}
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)]
[System.CLSCompliant(false)]
public sealed class MemberNotNullAttribute : System.Attribute
{
public MemberNotNullAttribute(params string[] members) { }
public string[] Members { get { throw null; } }
}
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)]
[System.CLSCompliant(false)]
public sealed class MemberNotNullWhenAttribute : System.Attribute
{
public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { }
public bool ReturnValue { get { throw null; } }
public string[] Members { get { throw null; } }
}
[System.AttributeUsageAttribute(System.AttributeTargets.All, Inherited=false, AllowMultiple=true)]
[System.Diagnostics.ConditionalAttribute("CODE_ANALYSIS")]
public sealed partial class SuppressMessageAttribute : System.Attribute
Expand Down

0 comments on commit 90ab57c

Please sign in to comment.