Skip to content

Commit 1926a3c

Browse files
authored
Add string ctor to MemberNotNull{When} (#33864)
With only the params string[] ctor, every usage of MemberNotNull{When} in a CLSCompliant(true) assembly needs to have its warning suppressed.
1 parent 9656059 commit 1926a3c

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,36 +127,52 @@ sealed class DoesNotReturnIfAttribute : Attribute
127127
}
128128

129129
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
130-
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)]
130+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
131131
#if INTERNAL_NULLABLE_ATTRIBUTES
132132
internal
133133
#else
134-
[CLSCompliant(false)]
135134
public
136135
#endif
137136
sealed class MemberNotNullAttribute : Attribute
138137
{
138+
/// <summary>Initializes the attribute with a field or property member.</summary>
139+
/// <param name="member">
140+
/// The field or property member that is promised to be not-null.
141+
/// </param>
142+
public MemberNotNullAttribute(string member) => Members = new[] { member };
143+
139144
/// <summary>Initializes the attribute with the list of field and property members.</summary>
140145
/// <param name="members">
141146
/// The list of field and property members that are promised to be not-null.
142147
/// </param>
143-
public MemberNotNullAttribute(params string[] members)
144-
=> Members = members;
148+
public MemberNotNullAttribute(params string[] members) => Members = members;
145149

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

150154
/// <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>
151-
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)]
155+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
152156
#if INTERNAL_NULLABLE_ATTRIBUTES
153157
internal
154158
#else
155-
[CLSCompliant(false)]
156159
public
157160
#endif
158161
sealed class MemberNotNullWhenAttribute : Attribute
159162
{
163+
/// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
164+
/// <param name="returnValue">
165+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
166+
/// </param>
167+
/// <param name="member">
168+
/// The field or property member that is promised to be not-null.
169+
/// </param>
170+
public MemberNotNullWhenAttribute(bool returnValue, string member)
171+
{
172+
ReturnValue = returnValue;
173+
Members = new[] { member };
174+
}
175+
160176
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
161177
/// <param name="returnValue">
162178
/// The return value condition. If the method returns this value, the associated parameter will not be null.

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5618,17 +5618,17 @@ public sealed partial class NotNullWhenAttribute : System.Attribute
56185618
public NotNullWhenAttribute(bool returnValue) { }
56195619
public bool ReturnValue { get { throw null; } }
56205620
}
5621-
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)]
5622-
[System.CLSCompliant(false)]
5621+
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
56235622
public sealed class MemberNotNullAttribute : System.Attribute
56245623
{
5624+
public MemberNotNullAttribute(string member) { }
56255625
public MemberNotNullAttribute(params string[] members) { }
56265626
public string[] Members { get { throw null; } }
56275627
}
5628-
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)]
5629-
[System.CLSCompliant(false)]
5628+
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
56305629
public sealed class MemberNotNullWhenAttribute : System.Attribute
56315630
{
5631+
public MemberNotNullWhenAttribute(bool returnValue, string member) { }
56325632
public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { }
56335633
public bool ReturnValue { get { throw null; } }
56345634
public string[] Members { get { throw null; } }

0 commit comments

Comments
 (0)