Skip to content

Commit 380f6ae

Browse files
committed
Improved Guard APIs codegen
1 parent 3b5eb17 commit 380f6ae

9 files changed

+176
-361
lines changed

Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.g.cs

Lines changed: 88 additions & 177 deletions
Large diffs are not rendered by default.

Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics.CodeAnalysis;
8-
using System.Runtime.CompilerServices;
98
using Microsoft.Toolkit.Extensions;
109

1110
namespace Microsoft.Toolkit.Diagnostics
@@ -22,111 +21,100 @@ GenerateTextForItems(EnumerableTypes, item =>
2221
/// <summary>
2322
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
2423
/// </summary>
25-
[MethodImpl(MethodImplOptions.NoInlining)]
2624
[DoesNotReturn]
2725
internal static void ThrowArgumentExceptionForIsEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
2826
{
29-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
27+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}", name);
3028
}
3129

3230
/// <summary>
3331
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
3432
/// </summary>
35-
[MethodImpl(MethodImplOptions.NoInlining)]
3633
[DoesNotReturn]
3734
internal static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
3835
{
39-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
36+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}", name);
4037
}
4138

4239
/// <summary>
4340
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
4441
/// </summary>
45-
[MethodImpl(MethodImplOptions.NoInlining)]
4642
[DoesNotReturn]
4743
internal static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
4844
{
49-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size not equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
45+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size not equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}", name);
5046
}
5147

5248
/// <summary>
5349
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
5450
/// </summary>
55-
[MethodImpl(MethodImplOptions.NoInlining)]
5651
[DoesNotReturn]
5752
internal static void ThrowArgumentExceptionForHasSizeGreaterThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
5853
{
59-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size over {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
54+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size over {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}", name);
6055
}
6156

6257
/// <summary>
6358
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
6459
/// </summary>
65-
[MethodImpl(MethodImplOptions.NoInlining)]
6660
[DoesNotReturn]
6761
internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
6862
{
69-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size of at least {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
63+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size of at least {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}", name);
7064
}
7165

7266
/// <summary>
7367
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
7468
/// </summary>
75-
[MethodImpl(MethodImplOptions.NoInlining)]
7669
[DoesNotReturn]
7770
internal static void ThrowArgumentExceptionForHasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
7871
{
79-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
72+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}", name);
8073
}
8174

8275
/// <summary>
8376
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
8477
/// </summary>
85-
[MethodImpl(MethodImplOptions.NoInlining)]
8678
[DoesNotReturn]
8779
internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
8880
{
89-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
81+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}", name);
9082
}
9183

9284
/// <summary>
9385
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
9486
/// </summary>
95-
[MethodImpl(MethodImplOptions.NoInlining)]
9687
[DoesNotReturn]
9788
internal static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
9889
{
99-
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}");
90+
throw new ArgumentException($"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}", name);
10091
}
10192

10293
/// <summary>
10394
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
10495
/// </summary>
105-
[MethodImpl(MethodImplOptions.NoInlining)]
10696
[DoesNotReturn]
10797
internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
10898
{
109-
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}");
99+
throw new ArgumentException($"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}", name);
110100
}
111101

112102
/// <summary>
113103
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
114104
/// </summary>
115-
[MethodImpl(MethodImplOptions.NoInlining)]
116105
[DoesNotReturn]
117106
internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
118107
{
119-
ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
108+
throw new ArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
120109
}
121110

122111
/// <summary>
123112
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
124113
/// </summary>
125-
[MethodImpl(MethodImplOptions.NoInlining)]
126114
[DoesNotReturn]
127115
internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
128116
{
129-
ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be an invalid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
117+
throw new ArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be an invalid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
130118
}
131119
<#
132120
});

Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Collection.Generic.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Diagnostics.CodeAnalysis;
7-
using System.Runtime.CompilerServices;
87
using Microsoft.Toolkit.Extensions;
98

109
namespace Microsoft.Toolkit.Diagnostics
@@ -19,34 +18,31 @@ public static partial class ThrowHelper
1918
/// </summary>
2019
/// <typeparam name="T">The item of items in the input <see cref="Span{T}"/> instance.</typeparam>
2120
/// <remarks>This method is needed because <see cref="Span{T}"/> can't be used as a generic type parameter.</remarks>
22-
[MethodImpl(MethodImplOptions.NoInlining)]
2321
[DoesNotReturn]
2422
internal static void ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(string name)
2523
{
26-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must not be empty");
24+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must not be empty", name);
2725
}
2826

2927
/// <summary>
3028
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEmpty{T}(ReadOnlySpan{T},string)"/> fails.
3129
/// </summary>
3230
/// <typeparam name="T">The item of items in the input <see cref="ReadOnlySpan{T}"/> instance.</typeparam>
3331
/// <remarks>This method is needed because <see cref="ReadOnlySpan{T}"/> can't be used as a generic type parameter.</remarks>
34-
[MethodImpl(MethodImplOptions.NoInlining)]
3532
[DoesNotReturn]
3633
internal static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(string name)
3734
{
38-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must not be empty");
35+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must not be empty", name);
3936
}
4037

4138
/// <summary>
4239
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEmpty{T}(T[],string)"/> (or an overload) fails.
4340
/// </summary>
4441
/// <typeparam name="T">The item of items in the input collection.</typeparam>
45-
[MethodImpl(MethodImplOptions.NoInlining)]
4642
[DoesNotReturn]
4743
internal static void ThrowArgumentExceptionForIsNotEmpty<T>(string name)
4844
{
49-
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be empty");
45+
throw new ArgumentException($"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be empty", name);
5046
}
5147
}
5248
}

0 commit comments

Comments
 (0)