Skip to content

Commit c2676f5

Browse files
Tidy nullability in Mock`1.cs.
1 parent 7636b79 commit c2676f5

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/Moq/Mock`1.cs

Lines changed: 12 additions & 7 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.Linq;
78
using System.Linq.Expressions;
89
using System.Text;
@@ -84,10 +85,10 @@ static Mock()
8485
serialNumberCounter = 0;
8586
}
8687

87-
T instance;
88+
T? instance;
8889
List<Type> additionalInterfaces;
89-
Dictionary<Type, object> configuredDefaultValues;
90-
object[] constructorArguments;
90+
Dictionary<Type, object?> configuredDefaultValues;
91+
object?[] constructorArguments;
9192
DefaultValueProvider defaultValueProvider;
9293
EventHandlerCollection eventHandlers;
9394
InvocationCollection invocations;
@@ -110,6 +111,7 @@ internal Mock(bool skipInitialize)
110111
// The skipInitialize parameter is not used at all, and it's
111112
// just to differentiate this ctor that should do nothing
112113
// from the regular ones which initializes the proxy, etc.
114+
// TODO: How should nullable references be handled here?
113115
}
114116

115117
/// <summary>
@@ -176,7 +178,7 @@ public Mock(MockBehavior behavior, params object?[]? args)
176178

177179
this.additionalInterfaces = new List<Type>();
178180
this.behavior = behavior;
179-
this.configuredDefaultValues = new Dictionary<Type, object>();
181+
this.configuredDefaultValues = new Dictionary<Type, object?>();
180182
this.constructorArguments = args;
181183
this.defaultValueProvider = DefaultValueProvider.Empty;
182184
this.eventHandlers = new EventHandlerCollection();
@@ -248,9 +250,9 @@ public override bool CallBase
248250
}
249251
}
250252

251-
internal override object[] ConstructorArguments => this.constructorArguments;
253+
internal override object?[] ConstructorArguments => this.constructorArguments;
252254

253-
internal override Dictionary<Type, object> ConfiguredDefaultValues => this.configuredDefaultValues;
255+
internal override Dictionary<Type, object?> ConfiguredDefaultValues => this.configuredDefaultValues;
254256

255257
/// <summary>
256258
/// Gets or sets the <see cref="DefaultValueProvider"/> instance that will be used
@@ -295,6 +297,9 @@ public override string ToString()
295297
return this.Name;
296298
}
297299

300+
#if NET
301+
[MemberNotNull(nameof(instance))]
302+
#endif
298303
void InitializeInstance()
299304
{
300305
// Determine the set of interfaces that the proxy object should additionally implement.
@@ -623,7 +628,7 @@ public Mock<T> SetupProperty<TProperty>(Expression<Func<T, TProperty>> property)
623628
/// Assert.Equal(6, v.Value);
624629
/// </code>
625630
/// </example>
626-
public Mock<T> SetupProperty<TProperty>(Expression<Func<T, TProperty>> property, TProperty initialValue)
631+
public Mock<T> SetupProperty<TProperty>(Expression<Func<T, TProperty>> property, TProperty? initialValue)
627632
{
628633
Mock.SetupProperty(this, property, initialValue);
629634
return this;

0 commit comments

Comments
 (0)