Skip to content

Commit 0e150d5

Browse files
authored
Merge pull request #78 from CommunityToolkit/dev/guard-unconstrained-generic
Remove class constraint from Guard.Is[Not]Null APIs
2 parents 87268d4 + c096438 commit 0e150d5

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

CommunityToolkit.Diagnostics/Guard.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public static partial class Guard
2424
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not <see langword="null"/>.</exception>
2525
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2626
public static void IsNull<T>(T? value, [CallerArgumentExpression("value")] string name = "")
27-
where T : class
2827
{
2928
if (value is null)
3029
{
@@ -41,7 +40,6 @@ public static void IsNull<T>(T? value, [CallerArgumentExpression("value")] strin
4140
/// <param name="value">The input value to test.</param>
4241
/// <param name="name">The name of the input parameter being tested.</param>
4342
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not <see langword="null"/>.</exception>
44-
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
4543
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4644
public static void IsNull<T>(T? value, [CallerArgumentExpression("value")] string name = "")
4745
where T : struct
@@ -63,7 +61,6 @@ public static void IsNull<T>(T? value, [CallerArgumentExpression("value")] strin
6361
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <see langword="null"/>.</exception>
6462
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6563
public static void IsNotNull<T>([NotNull] T? value, [CallerArgumentExpression("value")] string name = "")
66-
where T : class
6764
{
6865
if (value is not null)
6966
{
@@ -80,7 +77,6 @@ public static void IsNotNull<T>([NotNull] T? value, [CallerArgumentExpression("v
8077
/// <param name="value">The input value to test.</param>
8178
/// <param name="name">The name of the input parameter being tested.</param>
8279
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <see langword="null"/>.</exception>
83-
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
8480
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8581
public static void IsNotNull<T>([NotNull] T? value, [CallerArgumentExpression("value")] string name = "")
8682
where T : struct

CommunityToolkit.Diagnostics/Internals/Guard.ThrowHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ private static string AssertString(object? obj)
3838
/// <typeparam name="T">The type of the input value.</typeparam>
3939
[DoesNotReturn]
4040
public static void ThrowArgumentExceptionForIsNull<T>(T value, string name)
41-
where T : class
4241
{
43-
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be null, was {AssertString(value)} ({value.GetType().ToTypeString()}).", name);
42+
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be null, was {AssertString(value)} ({value!.GetType().ToTypeString()}).", name);
4443
}
4544

4645
/// <summary>

tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public void Test_Guard_IsNull_Ok()
1515
{
1616
Guard.IsNull<object>(null, nameof(Test_Guard_IsNull_Ok));
1717
Guard.IsNull<int>(null, nameof(Test_Guard_IsNull_Ok));
18+
19+
static void Test<T>(T? obj)
20+
{
21+
Guard.IsNull(obj, nameof(Test_Guard_IsNull_Ok));
22+
}
23+
24+
Test<string>(null);
25+
Test<int?>(null);
1826
}
1927

2028
[TestMethod]
@@ -28,14 +36,47 @@ public void Test_Guard_IsNull_ClassFail()
2836
[ExpectedException(typeof(ArgumentException))]
2937
public void Test_Guard_IsNull_StructFail()
3038
{
31-
Guard.IsNull<int>(7, nameof(Test_Guard_IsNull_StructFail));
39+
Guard.IsNull(7, nameof(Test_Guard_IsNull_StructFail));
40+
}
41+
42+
[TestMethod]
43+
[ExpectedException(typeof(ArgumentException))]
44+
public void Test_Guard_IsNull_GenericClassFail()
45+
{
46+
static void Test<T>(T? obj)
47+
{
48+
Guard.IsNull(obj, nameof(Test_Guard_IsNull_GenericClassFail));
49+
}
50+
51+
Test("Hi!");
52+
}
53+
54+
[TestMethod]
55+
[ExpectedException(typeof(ArgumentException))]
56+
public void Test_Guard_IsNull_GenericStructFail()
57+
{
58+
static void Test<T>(T? obj)
59+
{
60+
Guard.IsNull(obj, nameof(Test_Guard_IsNull_GenericStructFail));
61+
}
62+
63+
Test(42);
3264
}
3365

3466
[TestMethod]
3567
public void Test_Guard_IsNotNull_Ok()
3668
{
3769
Guard.IsNotNull(new object(), nameof(Test_Guard_IsNotNull_Ok));
38-
Guard.IsNotNull<int>(7, nameof(Test_Guard_IsNotNull_Ok));
70+
Guard.IsNotNull(7, nameof(Test_Guard_IsNotNull_Ok));
71+
72+
static void Test<T>(T? obj)
73+
{
74+
Guard.IsNotNull(obj, nameof(Test_Guard_IsNotNull_Ok));
75+
}
76+
77+
Test("Hi!");
78+
Test(42);
79+
Test<int?>(42);
3980
}
4081

4182
[TestMethod]
@@ -52,6 +93,30 @@ public void Test_Guard_IsNotNull_StructFail()
5293
Guard.IsNotNull<int>(null, nameof(Test_Guard_IsNotNull_StructFail));
5394
}
5495

96+
[TestMethod]
97+
[ExpectedException(typeof(ArgumentNullException))]
98+
public void Test_Guard_IsNotNull_GenericClassFail()
99+
{
100+
static void Test<T>(T? obj)
101+
{
102+
Guard.IsNotNull(obj, nameof(Test_Guard_IsNotNull_GenericClassFail));
103+
}
104+
105+
Test<string>(null);
106+
}
107+
108+
[TestMethod]
109+
[ExpectedException(typeof(ArgumentNullException))]
110+
public void Test_Guard_IsNotNull_GenericStructFail()
111+
{
112+
static void Test<T>(T? obj)
113+
{
114+
Guard.IsNotNull(obj, nameof(Test_Guard_IsNotNull_GenericClassFail));
115+
}
116+
117+
Test<int?>(null);
118+
}
119+
55120
[TestMethod]
56121
public void Test_Guard_IsOfT_Ok()
57122
{

0 commit comments

Comments
 (0)