Skip to content

Commit c4ff1b5

Browse files
andrewimcclementkzu
authored andcommitted
Partially fix up nullability in ProtectedMock.cs.
1 parent 247bdd5 commit c4ff1b5

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/Moq/Protected/ProtectedMock.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Globalization;
78
using System.Linq;
89
using System.Linq.Expressions;
@@ -51,7 +52,7 @@ public ISetup<T> Setup(string methodName, Type[] genericTypeArguments, bool exac
5152
return this.InternalSetup(methodName, genericTypeArguments, exactParameterMatch, args);
5253
}
5354

54-
ISetup<T> InternalSetup(string methodName, Type[] genericTypeArguments, bool exactParameterMatch, params object[] args)
55+
ISetup<T> InternalSetup(string methodName, Type[]? genericTypeArguments, bool exactParameterMatch, params object[] args)
5556
{
5657
Guard.NotNull(methodName, nameof(methodName));
5758

@@ -80,7 +81,7 @@ public ISetup<T, TResult> Setup<TResult>(string methodName, Type[] genericTypeAr
8081
return this.InternalSetup<TResult>(methodName, genericTypeArguments, exactParameterMatch, args);
8182
}
8283

83-
ISetup<T, TResult> InternalSetup<TResult>(string methodName, Type[] genericTypeArguments,
84+
ISetup<T, TResult> InternalSetup<TResult>(string methodName, Type[]? genericTypeArguments,
8485
bool exactParameterMatch, params object[] args)
8586
{
8687
Guard.NotNullOrEmpty(methodName, nameof(methodName));
@@ -147,7 +148,7 @@ public ISetupSequentialAction SetupSequence(string methodOrPropertyName, Type[]
147148
return this.InternalSetupSequence(methodOrPropertyName, genericTypeArguments, exactParameterMatch, args);
148149
}
149150

150-
ISetupSequentialAction InternalSetupSequence(string methodOrPropertyName, Type[] genericTypeArguments, bool exactParameterMatch, params object[] args)
151+
ISetupSequentialAction InternalSetupSequence(string methodOrPropertyName, Type[]? genericTypeArguments, bool exactParameterMatch, params object[] args)
151152
{
152153
Guard.NotNullOrEmpty(methodOrPropertyName, nameof(methodOrPropertyName));
153154

@@ -175,7 +176,7 @@ public ISetupSequentialResult<TResult> SetupSequence<TResult>(string methodOrPro
175176
return this.InternalSetupSequence<TResult>(methodOrPropertyName, genericTypeArguments, exactParameterMatch, args);
176177
}
177178

178-
ISetupSequentialResult<TResult> InternalSetupSequence<TResult>(string methodOrPropertyName, Type[] genericTypeArguments, bool exactParameterMatch, params object[] args)
179+
ISetupSequentialResult<TResult> InternalSetupSequence<TResult>(string methodOrPropertyName, Type[]? genericTypeArguments, bool exactParameterMatch, params object[] args)
179180
{
180181
Guard.NotNullOrEmpty(methodOrPropertyName, nameof(methodOrPropertyName));
181182

@@ -223,7 +224,7 @@ public void Verify(string methodName, Type[] genericTypeArguments, Times times,
223224
this.InternalVerify(methodName, genericTypeArguments, times, exactParameterMatch, args);
224225
}
225226

226-
void InternalVerify(string methodName, Type[] genericTypeArguments, Times times, bool exactParameterMatch, params object[] args)
227+
void InternalVerify(string methodName, Type[]? genericTypeArguments, Times times, bool exactParameterMatch, params object[] args)
227228
{
228229
Guard.NotNullOrEmpty(methodName, nameof(methodName));
229230

@@ -256,7 +257,7 @@ public void Verify<TResult>(string methodName, Type[] genericTypeArguments, Time
256257
this.InternalVerify<TResult>(methodName, genericTypeArguments, times, exactParameterMatch, args);
257258
}
258259

259-
void InternalVerify<TResult>(string methodName, Type[] genericTypeArguments, Times times, bool exactParameterMatch, params object[] args)
260+
void InternalVerify<TResult>(string methodName, Type[]? genericTypeArguments, Times times, bool exactParameterMatch, params object[] args)
260261
{
261262
Guard.NotNullOrEmpty(methodName, nameof(methodName));
262263

@@ -359,7 +360,11 @@ static Expression<Action<T>> GetSetterExpression(PropertyInfo property, Expressi
359360
param);
360361
}
361362

362-
static void ThrowIfMemberMissing(string memberName, MemberInfo member)
363+
#if NULLABLE_REFERENCE_TYPES
364+
static void ThrowIfMemberMissing(string memberName, [NotNull] MemberInfo? member)
365+
#else
366+
static void ThrowIfMemberMissing(string memberName, MemberInfo? member)
367+
#endif
363368
{
364369
if (member == null)
365370
{
@@ -371,7 +376,11 @@ static void ThrowIfMemberMissing(string memberName, MemberInfo member)
371376
}
372377
}
373378

374-
static void ThrowIfMethodMissing(string methodName, MethodInfo method, object[] args)
379+
#if NULLABLE_REFERENCE_TYPES
380+
static void ThrowIfMethodMissing(string methodName, [NotNull] MethodInfo? method, object[] args)
381+
#else
382+
static void ThrowIfMethodMissing(string methodName, MethodInfo? method, object[] args)
383+
#endif
375384
{
376385
if (method == null)
377386
{
@@ -443,14 +452,14 @@ static void ThrowIfVoidMethod(MethodInfo method)
443452
}
444453
}
445454

446-
static Type[] ToArgTypes(object[] args)
455+
static Type?[] ToArgTypes(object[] args)
447456
{
448457
if (args == null)
449458
{
450459
throw new ArgumentException(Resources.UseItExprIsNullRatherThanNullArgumentValue);
451460
}
452461

453-
var types = new Type[args.Length];
462+
var types = new Type?[args.Length];
454463
for (int index = 0; index < args.Length; index++)
455464
{
456465
if (args[index] == null)
@@ -503,9 +512,9 @@ static bool IsItRefAny(Expression expression)
503512
return ItRefAnyField(expression) != null;
504513
}
505514

506-
static FieldInfo ItRefAnyField(Expression expr)
515+
static FieldInfo? ItRefAnyField(Expression expr)
507516
{
508-
FieldInfo itRefAnyField = null;
517+
FieldInfo? itRefAnyField = null;
509518

510519
if (expr.NodeType == ExpressionType.MemberAccess)
511520
{
@@ -514,7 +523,7 @@ static FieldInfo ItRefAnyField(Expression expr)
514523
{
515524
if (field.Name == nameof(It.Ref<object>.IsAny))
516525
{
517-
var fieldDeclaringType = field.DeclaringType;
526+
var fieldDeclaringType = field.DeclaringType!;
518527
if (fieldDeclaringType.IsGenericType)
519528
{
520529
var fieldDeclaringTypeDefinition = fieldDeclaringType.GetGenericTypeDefinition();

0 commit comments

Comments
 (0)