Skip to content

Commit 0d43da7

Browse files
author
msftbot[bot]
authored
Shameful rename of [IsNullOr]WhiteSpace Guard APIs (#3515)
## Overview Some `Guard` APIs were incorrectly using the wrong capitalization for the `WhiteSpace` part, not following the same structure of the actual APIs in the BCL (eg. `string.IsNullOrWhiteSpace`). This PR deprecates them and adds new APIs with the right name. ...I'm sorry 😅 <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - API rename - API deprecation ## PR Checklist Please check if your PR fulfills the following requirements: - [X] Tested code with current [supported SDKs](../readme.md#supported) - [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~ - [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~ - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)~~ - [ ] ~~Tests for the changes have been added (for bug fixes / features) (if applicable)~~ - [ ] ~~Header has been added to all new source files (run *build/UpdateHeaders.bat*)~~ - [X] Contains **NO** breaking changes
2 parents 2b610c4 + e296861 commit 0d43da7

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

Microsoft.Toolkit/Diagnostics/Guard.String.cs

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public static void IsNotNullOrEmpty([NotNull] string? text, string name)
4343
{
4444
if (!string.IsNullOrEmpty(text))
4545
{
46+
#pragma warning disable CS8777 // Does not return when text is null
4647
return;
48+
#pragma warning restore CS8777
4749
}
4850

4951
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrEmpty(text, name);
50-
#pragma warning disable CS8777 // Does not return when text is null (.NET Standard 2.0 string.IsNullOrEmpty lacks flow attribute)
5152
}
52-
#pragma warning restore CS8777
5353

5454
/// <summary>
5555
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/> or whitespace.
@@ -58,14 +58,32 @@ public static void IsNotNullOrEmpty([NotNull] string? text, string name)
5858
/// <param name="name">The name of the input parameter being tested.</param>
5959
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
6060
[MethodImpl(MethodImplOptions.AggressiveInlining)]
61+
public static void IsNullOrWhiteSpace(string? text, string name)
62+
{
63+
if (string.IsNullOrWhiteSpace(text))
64+
{
65+
return;
66+
}
67+
68+
ThrowHelper.ThrowArgumentExceptionForIsNullOrWhiteSpace(text, name);
69+
}
70+
71+
/// <summary>
72+
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/> or whitespace.
73+
/// </summary>
74+
/// <param name="text">The input <see cref="string"/> instance to test.</param>
75+
/// <param name="name">The name of the input parameter being tested.</param>
76+
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
77+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
78+
[Obsolete("Use " + nameof(IsNullOrWhiteSpace))]
6179
public static void IsNullOrWhitespace(string? text, string name)
6280
{
6381
if (string.IsNullOrWhiteSpace(text))
6482
{
6583
return;
6684
}
6785

68-
ThrowHelper.ThrowArgumentExceptionForIsNullOrWhitespace(text, name);
86+
ThrowHelper.ThrowArgumentExceptionForIsNullOrWhiteSpace(text, name);
6987
}
7088

7189
/// <summary>
@@ -75,17 +93,37 @@ public static void IsNullOrWhitespace(string? text, string name)
7593
/// <param name="name">The name of the input parameter being tested.</param>
7694
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
7795
[MethodImpl(MethodImplOptions.AggressiveInlining)]
78-
public static void IsNotNullOrWhitespace([NotNull] string? text, string name)
96+
public static void IsNotNullOrWhiteSpace([NotNull] string? text, string name)
7997
{
8098
if (!string.IsNullOrWhiteSpace(text))
8199
{
100+
#pragma warning disable CS8777 // Does not return when text is null
82101
return;
102+
#pragma warning restore CS8777
83103
}
84104

85-
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrWhitespace(text, name);
86-
#pragma warning disable CS8777 // Does not return when text is null
105+
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrWhiteSpace(text, name);
87106
}
107+
108+
/// <summary>
109+
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
110+
/// </summary>
111+
/// <param name="text">The input <see cref="string"/> instance to test.</param>
112+
/// <param name="name">The name of the input parameter being tested.</param>
113+
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
114+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
115+
[Obsolete("Use " + nameof(IsNotNullOrWhiteSpace))]
116+
public static void IsNotNullOrWhitespace([NotNull] string? text, string name)
117+
{
118+
if (!string.IsNullOrWhiteSpace(text))
119+
{
120+
#pragma warning disable CS8777 // Does not return when text is null
121+
return;
88122
#pragma warning restore CS8777
123+
}
124+
125+
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrWhiteSpace(text, name);
126+
}
89127

90128
/// <summary>
91129
/// Asserts that the input <see cref="string"/> instance must be empty.
@@ -128,14 +166,49 @@ public static void IsNotEmpty(string text, string name)
128166
/// <param name="name">The name of the input parameter being tested.</param>
129167
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
130168
[MethodImpl(MethodImplOptions.AggressiveInlining)]
169+
public static void IsWhiteSpace(string text, string name)
170+
{
171+
if (string.IsNullOrWhiteSpace(text))
172+
{
173+
return;
174+
}
175+
176+
ThrowHelper.ThrowArgumentExceptionForIsWhiteSpace(text, name);
177+
}
178+
179+
/// <summary>
180+
/// Asserts that the input <see cref="string"/> instance must be whitespace.
181+
/// </summary>
182+
/// <param name="text">The input <see cref="string"/> instance to test.</param>
183+
/// <param name="name">The name of the input parameter being tested.</param>
184+
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
185+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
186+
[Obsolete("Use " + nameof(IsWhiteSpace))]
131187
public static void IsWhitespace(string text, string name)
132188
{
133189
if (string.IsNullOrWhiteSpace(text))
134190
{
135191
return;
136192
}
137193

138-
ThrowHelper.ThrowArgumentExceptionForIsWhitespace(text, name);
194+
ThrowHelper.ThrowArgumentExceptionForIsWhiteSpace(text, name);
195+
}
196+
197+
/// <summary>
198+
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
199+
/// </summary>
200+
/// <param name="text">The input <see cref="string"/> instance to test.</param>
201+
/// <param name="name">The name of the input parameter being tested.</param>
202+
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
203+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
204+
public static void IsNotWhiteSpace(string text, string name)
205+
{
206+
if (!string.IsNullOrWhiteSpace(text))
207+
{
208+
return;
209+
}
210+
211+
ThrowHelper.ThrowArgumentExceptionForIsNotWhiteSpace(text, name);
139212
}
140213

141214
/// <summary>
@@ -145,14 +218,15 @@ public static void IsWhitespace(string text, string name)
145218
/// <param name="name">The name of the input parameter being tested.</param>
146219
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
147220
[MethodImpl(MethodImplOptions.AggressiveInlining)]
221+
[Obsolete("Use " + nameof(IsNotWhiteSpace))]
148222
public static void IsNotWhitespace(string text, string name)
149223
{
150224
if (!string.IsNullOrWhiteSpace(text))
151225
{
152226
return;
153227
}
154228

155-
ThrowHelper.ThrowArgumentExceptionForIsNotWhitespace(text, name);
229+
ThrowHelper.ThrowArgumentExceptionForIsNotWhiteSpace(text, name);
156230
}
157231

158232
/// <summary>

Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.String.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, str
4040
/// </summary>
4141
[MethodImpl(MethodImplOptions.NoInlining)]
4242
[DoesNotReturn]
43-
internal static void ThrowArgumentExceptionForIsNullOrWhitespace(string? text, string name)
43+
internal static void ThrowArgumentExceptionForIsNullOrWhiteSpace(string? text, string name)
4444
{
4545
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be null or whitespace, was {text.ToAssertString()}");
4646
}
@@ -50,7 +50,7 @@ internal static void ThrowArgumentExceptionForIsNullOrWhitespace(string? text, s
5050
/// </summary>
5151
[MethodImpl(MethodImplOptions.NoInlining)]
5252
[DoesNotReturn]
53-
internal static void ThrowArgumentExceptionForIsNotNullOrWhitespace(string? text, string name)
53+
internal static void ThrowArgumentExceptionForIsNotNullOrWhiteSpace(string? text, string name)
5454
{
5555
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}");
5656
}
@@ -80,7 +80,7 @@ internal static void ThrowArgumentExceptionForIsNotEmpty(string text, string nam
8080
/// </summary>
8181
[MethodImpl(MethodImplOptions.NoInlining)]
8282
[DoesNotReturn]
83-
internal static void ThrowArgumentExceptionForIsWhitespace(string text, string name)
83+
internal static void ThrowArgumentExceptionForIsWhiteSpace(string text, string name)
8484
{
8585
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be whitespace, was {text.ToAssertString()}");
8686
}
@@ -90,7 +90,7 @@ internal static void ThrowArgumentExceptionForIsWhitespace(string text, string n
9090
/// </summary>
9191
[MethodImpl(MethodImplOptions.NoInlining)]
9292
[DoesNotReturn]
93-
internal static void ThrowArgumentExceptionForIsNotWhitespace(string text, string name)
93+
internal static void ThrowArgumentExceptionForIsNotWhiteSpace(string text, string name)
9494
{
9595
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be whitespace, was {text.ToAssertString()}");
9696
}

0 commit comments

Comments
 (0)