Skip to content

Commit e4ac690

Browse files
authored
Updated code to use the ArgumentNullException throw helper (#8319)
1 parent 3bf761a commit e4ac690

File tree

353 files changed

+1112
-4645
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

353 files changed

+1112
-4645
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,9 @@ dotnet_naming_symbols.static_fields.required_modifiers = static
149149
dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
150150
dotnet_naming_style.static_prefix_style.required_prefix = s_
151151
dotnet_naming_style.static_prefix_style.capitalization = camel_case
152+
153+
# Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance.
154+
dotnet_diagnostic.CA1510.severity = warning
155+
156+
[src/HotChocolate/Language/**/*.cs]
157+
dotnet_diagnostic.CA1510.severity = none

src/CookieCrumble/src/CookieCrumble.HotChocolate/Formatters/ExecutionResultSnapshotValueFormatter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ internal sealed class JsonResultPatcher
109109

110110
public void SetResponse(JsonDocument response)
111111
{
112-
if (response is null)
113-
{
114-
throw new ArgumentNullException(nameof(response));
115-
}
112+
ArgumentNullException.ThrowIfNull(response);
116113

117114
_json = JsonObject.Create(response.RootElement);
118115
}

src/CookieCrumble/src/CookieCrumble/Snapshot.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ public static void Match(
9696
public static void RegisterTestFramework(
9797
ITestFramework testFramework)
9898
{
99-
if (testFramework is null)
100-
{
101-
throw new ArgumentNullException(nameof(testFramework));
102-
}
99+
ArgumentNullException.ThrowIfNull(testFramework);
103100

104101
lock (_sync)
105102
{
@@ -110,10 +107,7 @@ public static void RegisterTestFramework(
110107
public static void RegisterFormatter(
111108
ISnapshotValueFormatter formatter)
112109
{
113-
if (formatter is null)
114-
{
115-
throw new ArgumentNullException(nameof(formatter));
116-
}
110+
ArgumentNullException.ThrowIfNull(formatter);
117111

118112
lock (_sync)
119113
{
@@ -125,10 +119,7 @@ public static void TryRegisterFormatter(
125119
ISnapshotValueFormatter formatter,
126120
bool typeCheck = true)
127121
{
128-
if (formatter is null)
129-
{
130-
throw new ArgumentNullException(nameof(formatter));
131-
}
122+
ArgumentNullException.ThrowIfNull(formatter);
132123

133124
lock (_sync)
134125
{
@@ -184,10 +175,7 @@ public Snapshot Add(
184175

185176
public Snapshot Add(SnapshotValue value)
186177
{
187-
if (value == null)
188-
{
189-
throw new ArgumentNullException(nameof(value));
190-
}
178+
ArgumentNullException.ThrowIfNull(value);
191179

192180
_segments.Add(value);
193181
return this;

src/GreenDonut/src/GreenDonut.Data.EntityFramework/Expressions/ExpressionHelpers.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,7 @@ static MethodInfo GetEnumerableMethod(string methodName, Type elementType, Lambd
306306
/// <exception cref="ArgumentNullException"></exception>
307307
public static OrderRewriterResult ExtractAndRemoveOrder(Expression expression)
308308
{
309-
if (expression is null)
310-
{
311-
throw new ArgumentNullException(nameof(expression));
312-
}
309+
ArgumentNullException.ThrowIfNull(expression);
313310

314311
var rewriter = new OrderByRemovalRewriter();
315312
var (result, orderExpressions, orderMethods) = rewriter.Rewrite(expression);

src/GreenDonut/src/GreenDonut.Data.Primitives/SortBy.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ public SortBy(Expression<Func<TEntity, TValue>> keySelector, bool ascending = tr
4848
/// </returns>
4949
public IOrderedQueryable<TEntity> ApplyOrderBy(IQueryable<TEntity> queryable)
5050
{
51-
if (queryable is null)
52-
{
53-
throw new ArgumentNullException(nameof(queryable));
54-
}
51+
ArgumentNullException.ThrowIfNull(queryable);
5552

5653
if (Ascending)
5754
{
@@ -107,10 +104,7 @@ public static class SortBy<TEntity>
107104
public static SortBy<TEntity, TValue> Ascending<TValue>(
108105
Expression<Func<TEntity, TValue>> keySelector)
109106
{
110-
if (keySelector is null)
111-
{
112-
throw new ArgumentNullException(nameof(keySelector));
113-
}
107+
ArgumentNullException.ThrowIfNull(keySelector);
114108

115109
return new SortBy<TEntity, TValue>(keySelector, true);
116110
}
@@ -133,10 +127,7 @@ public static SortBy<TEntity, TValue> Ascending<TValue>(
133127
public static SortBy<TEntity, TValue> Descending<TValue>(
134128
Expression<Func<TEntity, TValue>> keySelector)
135129
{
136-
if (keySelector is null)
137-
{
138-
throw new ArgumentNullException(nameof(keySelector));
139-
}
130+
ArgumentNullException.ThrowIfNull(keySelector);
140131

141132
return new SortBy<TEntity, TValue>(keySelector, false);
142133
}

src/GreenDonut/src/GreenDonut.Data.Primitives/SortDefinition.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ public void Deconstruct(out ImmutableArray<ISortBy<T>> operations)
6363
public SortDefinition<T> AddAscending<TResult>(
6464
Expression<Func<T, TResult>> keySelector)
6565
{
66-
if (keySelector == null)
67-
{
68-
throw new ArgumentNullException(nameof(keySelector));
69-
}
66+
ArgumentNullException.ThrowIfNull(keySelector);
7067

7168
var operations = Operations.Add(SortBy<T>.Ascending(keySelector));
7269
return new SortDefinition<T>(operations);
@@ -87,10 +84,7 @@ public SortDefinition<T> AddAscending<TResult>(
8784
public SortDefinition<T> AddDescending<TResult>(
8885
Expression<Func<T, TResult>> keySelector)
8986
{
90-
if (keySelector == null)
91-
{
92-
throw new ArgumentNullException(nameof(keySelector));
93-
}
87+
ArgumentNullException.ThrowIfNull(keySelector);
9488

9589
var operations = Operations.Add(SortBy<T>.Descending(keySelector));
9690
return new SortDefinition<T>(operations);
@@ -111,10 +105,7 @@ public SortDefinition<T> AddDescending<TResult>(
111105
public SortDefinition<T> IfEmpty(
112106
Func<SortDefinition<T>, SortDefinition<T>> applyIfEmpty)
113107
{
114-
if (applyIfEmpty == null)
115-
{
116-
throw new ArgumentNullException(nameof(applyIfEmpty));
117-
}
108+
ArgumentNullException.ThrowIfNull(applyIfEmpty);
118109

119110
if (Operations.Length == 0)
120111
{

src/GreenDonut/src/GreenDonut.Data/Cursors/CursorFormatter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ public static string Format<T>(T entity, CursorKey[] keys, CursorPageInfo pageIn
4343
throw new ArgumentNullException(nameof(entity));
4444
}
4545

46-
if (keys == null)
47-
{
48-
throw new ArgumentNullException(nameof(keys));
49-
}
46+
ArgumentNullException.ThrowIfNull(keys);
5047

5148
if (keys.Length == 0)
5249
{

src/GreenDonut/src/GreenDonut.Data/Cursors/CursorParser.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public static class CursorParser
3434
/// </exception>
3535
public static Cursor Parse(string cursor, ReadOnlySpan<CursorKey> keys)
3636
{
37-
if (cursor == null)
38-
{
39-
throw new ArgumentNullException(nameof(cursor));
40-
}
37+
ArgumentNullException.ThrowIfNull(cursor);
4138

4239
if (keys.Length == 0)
4340
{

src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutPageExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public static class GreenDonutPageExtensions
3434
/// </remarks>
3535
public static ImmutableArray<PageCursor> CreateRelativeBackwardCursors<T>(this Page<T> page, int maxCursors = 5)
3636
{
37-
if (page == null)
38-
{
39-
throw new ArgumentNullException(nameof(page));
40-
}
37+
ArgumentNullException.ThrowIfNull(page);
4138

4239
if (maxCursors < 0)
4340
{
@@ -95,10 +92,7 @@ public static ImmutableArray<PageCursor> CreateRelativeBackwardCursors<T>(this P
9592
/// </remarks>
9693
public static ImmutableArray<PageCursor> CreateRelativeForwardCursors<T>(this Page<T> page, int maxCursors = 5)
9794
{
98-
if (page == null)
99-
{
100-
throw new ArgumentNullException(nameof(page));
101-
}
95+
ArgumentNullException.ThrowIfNull(page);
10296

10397
if (maxCursors < 0)
10498
{

src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutPaginationBatchingDataLoaderExtensions.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ private static IDataLoader<TKey, Page<TValue>> WithInternal<TKey, TValue>(
7777
QueryContext<TValue>? context)
7878
where TKey : notnull
7979
{
80-
if (dataLoader is null)
81-
{
82-
throw new ArgumentNullException(nameof(dataLoader));
83-
}
80+
ArgumentNullException.ThrowIfNull(dataLoader);
8481

8582
var branchKey = pagingArguments.ComputeHash(context);
8683
var state = new PagingState<TValue>(pagingArguments, context);
@@ -113,10 +110,7 @@ public static IDataLoader<TKey, Page<TValue>> Select<TKey, TValue>(
113110
Expression<Func<TValue, TValue>>? selector)
114111
where TKey : notnull
115112
{
116-
if (dataLoader is null)
117-
{
118-
throw new ArgumentNullException(nameof(dataLoader));
119-
}
113+
ArgumentNullException.ThrowIfNull(dataLoader);
120114

121115
if (selector is null)
122116
{
@@ -162,10 +156,7 @@ public static IDataLoader<TKey, Page<TValue>> Where<TKey, TValue>(
162156
Expression<Func<TValue, bool>>? predicate)
163157
where TKey : notnull
164158
{
165-
if (dataLoader is null)
166-
{
167-
throw new ArgumentNullException(nameof(dataLoader));
168-
}
159+
ArgumentNullException.ThrowIfNull(dataLoader);
169160

170161
if (predicate is null)
171162
{
@@ -205,10 +196,7 @@ public static IDataLoader<TKey, Page<TValue>> OrderBy<TKey, TValue>(
205196
SortDefinition<TValue>? sortDefinition)
206197
where TKey : notnull
207198
{
208-
if (dataLoader is null)
209-
{
210-
throw new ArgumentNullException(nameof(dataLoader));
211-
}
199+
ArgumentNullException.ThrowIfNull(dataLoader);
212200

213201
if (sortDefinition is null)
214202
{

0 commit comments

Comments
 (0)