Skip to content

Enable nullable references in Moq.csproj #1535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 93 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
1410178
Enable nullable warnings in Moq.csproj.
andrewimcclement Feb 25, 2025
bbd54e2
Fix up nullability of ExpressionComparer.cs.
andrewimcclement Feb 25, 2025
17cd46d
Fix up nullability in ActionObserver.cs.
andrewimcclement Feb 25, 2025
26e6e85
Fix up nullability in Match.cs.
andrewimcclement Feb 25, 2025
4ec4e53
Fix up nullability in IProtectedAsMock.cs.
andrewimcclement Feb 25, 2025
c738dbb
Fix up nullability in MethodExpectation.cs.
andrewimcclement Feb 25, 2025
05b0ef8
Fix up some nullable issues in ProtectedAsMock.cs.
andrewimcclement Feb 25, 2025
18e987e
Fix up nullability in AwaitableFactory*.cs.
andrewimcclement Feb 25, 2025
69727d4
Fix up nullability in Awaitable.cs.
andrewimcclement Feb 25, 2025
8f1ea9e
Fix up nullability in TypeMatcherAttribute.cs.
andrewimcclement Feb 25, 2025
44bfa2d
Fix up nullability in Extensions.cs. A new symbol has been defined fo…
andrewimcclement Feb 25, 2025
46c00d4
Fix up nullability in RaiseEvent.cs.
andrewimcclement Feb 25, 2025
b634569
Fix up nullability in Expectation.cs and MethodExpectation.cs.
andrewimcclement Feb 25, 2025
393279d
Fix up AwaitableFactory`2.CreateCompleted(object?) to correctly assum…
andrewimcclement Feb 25, 2025
0c2c4cc
Simplify TaskFactory.CreateCompleted.
andrewimcclement Feb 25, 2025
c0989d7
Fix nullability for AsInterface.cs.
andrewimcclement Feb 25, 2025
3e499c0
Fix up nullability in EventHandlerCollection.cs.
andrewimcclement Feb 25, 2025
aa15e96
Fix up nullable types in ExpressionReconstructor.cs.
andrewimcclement Feb 25, 2025
059e59a
Fix nullability in Condition.cs. Add guard clause in Mock.When as not…
andrewimcclement Feb 25, 2025
1fa5dda
Fix up nullability in StubbedPropertySetup.cs.
andrewimcclement Feb 25, 2025
6ee4e21
Fix up nullability in StubbedPropertiesSetup.cs.
andrewimcclement Feb 25, 2025
2e03d1b
Fix up nullability in Invocation.cs.
andrewimcclement Feb 25, 2025
f55e666
Fix up nullability in Extensions.cs.
andrewimcclement Feb 25, 2025
d8328f9
Fix nullability in InnerMockSetup.cs.
andrewimcclement Feb 25, 2025
063899e
Fix up nullability in StubbedPropertySetup.cs.
andrewimcclement Feb 25, 2025
44bd2db
Fix nullability in Setup.cs.
andrewimcclement Feb 25, 2025
6171f9b
Fix nullability in SetupCollection.cs.
andrewimcclement Feb 25, 2025
07ea33e
Fix up nullability in StringBuilderExtensions.cs.
andrewimcclement Feb 25, 2025
e63957e
Fix up nullability in AwaitableFactory.cs.
andrewimcclement Feb 25, 2025
4742a09
Fix up nullability in Times.cs.
andrewimcclement Feb 25, 2025
1e71f78
Fix up nullability in EmptyDefaultValueProvider.cs.
andrewimcclement Feb 25, 2025
5419049
Fix up TaskFactory`1.cs nullability.
andrewimcclement Feb 25, 2025
c25d29a
Fix up nullability in ActionObserver.cs.
andrewimcclement Feb 25, 2025
a886aa4
Fix up nullability in ValueTaskFactory`1.cs.
andrewimcclement Feb 25, 2025
f4f0a5d
Fix up nullability for Invocation.cs.
andrewimcclement Feb 25, 2025
bd6b534
Tidy nullability in UpgradePropertyAccessorMethods.cs.
andrewimcclement Feb 25, 2025
a9ec75c
Fix up nullability in Extensions.cs.
andrewimcclement Feb 25, 2025
031f031
Tidy nullability in AwaitableFactory`2.cs.
andrewimcclement Feb 25, 2025
41521ec
Fix up nullability in DefaultValueProvider.cs.
andrewimcclement Feb 25, 2025
654fe22
Fix up nullability in LookupOrFallbackDefaultValueProvider.cs.
andrewimcclement Feb 25, 2025
12b4428
Fix up nullability in Match.cs.
andrewimcclement Feb 25, 2025
c61ad9f
Tidy nullability in ReturnBaseOrDefaultValue.cs.
andrewimcclement Feb 25, 2025
f9e995b
Fix up nullability in InvocationCollection.cs.
andrewimcclement Feb 25, 2025
b749b31
Tidy nullability in Guard.cs.
andrewimcclement Feb 25, 2025
148309c
Fix up nullability in RaiseEvent.cs.
andrewimcclement Feb 25, 2025
a77d923
Fix up nullability in EmptyDefaultValueProvider.cs.
andrewimcclement Feb 25, 2025
8f04601
Fix up nullability in Pair.cs
andrewimcclement Feb 25, 2025
a42abc0
Fix up nullability in SetupWithOutParameterSupport.cs.
andrewimcclement Feb 25, 2025
5ea40e6
Fix up nullability in CastleProxyFactory.cs.
andrewimcclement Feb 25, 2025
e8ff3db
Fix up a couple more Setup classes.
andrewimcclement Feb 25, 2025
d2c730d
Tidy nullability in Match.cs.
andrewimcclement Feb 25, 2025
ec8cd30
Tidy nullability in InterfaceProxy.cs.
andrewimcclement Feb 25, 2025
0b51a08
Fix up nullability in ReturnValue.cs.
andrewimcclement Feb 25, 2025
a72a6fe
FIx up nullability in EvaluateCaptures.cs.
andrewimcclement Feb 25, 2025
607ab00
Tidy nullability in RefMatcher.cs.
andrewimcclement Feb 25, 2025
eaf298a
Fix up nullability in IMatcher.cs and derived types.
andrewimcclement Feb 25, 2025
d67fd7e
Tidy nullability in Match.cs.
andrewimcclement Feb 25, 2025
47b2480
Tidy nullability in MatcherFactory.cs.
andrewimcclement Feb 25, 2025
e3eb36a
Tidy nullability in MatcherObserver.cs.
andrewimcclement Feb 25, 2025
e4b46b7
Fix up nullability in MockException.cs.
andrewimcclement Feb 25, 2025
7e59fe9
Fix up nullability in ParamArrayMatcher.cs.
andrewimcclement Feb 25, 2025
f61f8b7
Fix up nullability in ReturnComputedValue.cs.
andrewimcclement Feb 25, 2025
7c2626e
Tidy nullability in SetupPhrase.cs.
andrewimcclement Feb 25, 2025
9284486
Make best guess at how ThrowComputedException.cs should work.
andrewimcclement Feb 25, 2025
314574c
Tidy nullability in Mock.cs.
andrewimcclement Feb 25, 2025
e4cda52
Tidy nullability in ActionObserver.cs.
andrewimcclement Feb 25, 2025
954b72d
Tidy nullability in StubbedPropertiesSetup.cs.
andrewimcclement Feb 25, 2025
d9f2d74
Tidy nulability in Capture.cs.
andrewimcclement Feb 25, 2025
5369d15
Tidy nullability in ProtectedAsMock.cs and ProtectedMock.cs.
andrewimcclement Feb 25, 2025
1902930
Tidy nullability in MockDefaultValueProvider.cs.
andrewimcclement Feb 26, 2025
459d68b
Improve nullability in MethodCall.cs - still some strangeness left over.
andrewimcclement Feb 26, 2025
782f1da
Tidy nullability in Guard.cs.
andrewimcclement Feb 26, 2025
7069422
Tidy nullability in CastleProxyFactory.cs, InterceptionAspects.cs and…
andrewimcclement Feb 26, 2025
c0e646c
Fix up nullability in MatcherAttributeMatcher.cs.
andrewimcclement Feb 26, 2025
ddf91e0
Tidy nullability in ActionObserver.cs.
andrewimcclement Feb 26, 2025
7fbc056
Tidy nullability in MockSetupsBuilder.cs.
andrewimcclement Feb 26, 2025
9abd02b
Tidy nullability in StringBuilderExtensions.AppendExpression.cs.
andrewimcclement Feb 26, 2025
ba857e7
Tidy nullability in ExpressionReconstructor.cs.
andrewimcclement Feb 26, 2025
26221cd
Tidy nullability of Evaluator.cs.
andrewimcclement Feb 26, 2025
110780f
Tidy nullability in ItExpr.cs.
andrewimcclement Feb 26, 2025
320f9cf
Tidy nullability in MockFactory.cs.
andrewimcclement Feb 26, 2025
5befbbd
Tidy AsInterface.cs.
andrewimcclement Feb 26, 2025
f01aee1
Tidy ConstructorCallVisitor.cs. Fields `constructor` and `arguments` …
andrewimcclement Feb 26, 2025
dc60cd2
Tidy ReturnsExtensions.cs nullability.
andrewimcclement Feb 26, 2025
b9b97ba
Tidy nullability in Mock`1.cs.
andrewimcclement Feb 26, 2025
289fad3
Tidy nullability in MockException.cs.
andrewimcclement Feb 26, 2025
e88a34d
Partially fix up nullability in ProtectedMock.cs.
andrewimcclement Feb 26, 2025
bc0d5f9
Tidy nullability in Mock.cs.
andrewimcclement Feb 26, 2025
b4e4d5e
Some nullability improvements to ExpressionExtensions.cs. It seems li…
andrewimcclement Feb 26, 2025
a92acc6
Tidy nullability in It.cs.
andrewimcclement Feb 26, 2025
13d0344
Revert change to Recorder.invocationTimestamp.
andrewimcclement Feb 26, 2025
b14b04a
Dotnet format.
andrewimcclement May 28, 2025
111b277
Simplify StringBuilder.AppendValueOf handling of collections by testi…
andrewimcclement Jul 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/Moq/ActionObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ namespace Moq
/// </summary>
sealed class ActionObserver : ExpressionReconstructor
{
public override Expression<Action<T>> ReconstructExpression<T>(Action<T> action, object[] ctorArgs = null)
public override Expression<Action<T>> ReconstructExpression<T>(Action<T> action, object?[]? ctorArgs = null)
{
using (var matcherObserver = MatcherObserver.Activate())
{
// Create the root recording proxy:
var root = (T)CreateProxy(typeof(T), ctorArgs, matcherObserver, out var rootRecorder);

Exception error = null;
Exception? error = null;
try
{
// Execute the delegate. The root recorder will automatically "mock" return values
Expand All @@ -61,7 +61,7 @@ public override Expression<Action<T>> ReconstructExpression<T>(Action<T> action,
var invocation = recorder.Invocation;
if (invocation != null)
{
var resultType = invocation.Method.DeclaringType;
var resultType = invocation.Method.DeclaringType!;
if (resultType.IsAssignableFrom(body.Type) == false)
{
if (AwaitableFactory.TryGet(body.Type) is { } awaitableHandler
Expand Down Expand Up @@ -128,7 +128,7 @@ Expression[] GetArgumentExpressions(Invocation invocation, Match[] matches)
// it will have left behind a `default(T)` argument, possibly coerced to the parameter type.
// Therefore, we attempt to reproduce such coercions using `Convert.ChangeType`:
Type defaultValueType = matches[matchIndex].RenderExpression.Type;
object defaultValue = defaultValueType.GetDefaultValue();
object? defaultValue = defaultValueType.GetDefaultValue();
try
{
defaultValue = Convert.ChangeType(defaultValue, parameterTypes[argumentIndex]);
Expand Down Expand Up @@ -178,7 +178,7 @@ Expression[] GetArgumentExpressions(Invocation invocation, Match[] matches)
CultureInfo.CurrentCulture,
Resources.MatcherAssignmentFailedDuringExpressionReconstruction,
matches.Length,
$"{invocation.Method.DeclaringType.GetFormattedName()}.{invocation.Method.Name}"));
$"{invocation.Method.DeclaringType!.GetFormattedName()}.{invocation.Method.Name}"));
}

bool CanDistribute(int msi, int asi)
Expand Down Expand Up @@ -228,10 +228,10 @@ bool CanDistribute(int msi, int asi)
}

// Creates a proxy (way more light-weight than a `Mock<T>`!) with an invocation `Recorder` attached to it.
static IProxy CreateProxy(Type type, object[] ctorArgs, MatcherObserver matcherObserver, out Recorder recorder)
static IProxy CreateProxy(Type type, object?[]? ctorArgs, MatcherObserver matcherObserver, out Recorder recorder)
{
recorder = new Recorder(matcherObserver);
return (IProxy)ProxyFactory.Instance.CreateProxy(type, recorder, Type.EmptyTypes, ctorArgs ?? new object[0]);
return (IProxy)ProxyFactory.Instance.CreateProxy(type, recorder, Type.EmptyTypes, ctorArgs ?? new object?[0]);

}

Expand All @@ -241,9 +241,9 @@ sealed class Recorder : IInterceptor
{
readonly MatcherObserver matcherObserver;
int creationTimestamp;
Invocation invocation;
Invocation? invocation;
int invocationTimestamp;
object returnValue;
object? returnValue;

public Recorder(MatcherObserver matcherObserver)
{
Expand All @@ -253,7 +253,7 @@ public Recorder(MatcherObserver matcherObserver)
this.creationTimestamp = this.matcherObserver.GetNextTimestamp();
}

public Invocation Invocation => this.invocation;
public Invocation? Invocation => this.invocation;

public IEnumerable<Match> Matches
{
Expand All @@ -264,7 +264,7 @@ public IEnumerable<Match> Matches
}
}

public Recorder Next => (Awaitable.TryGetResultRecursive(this.returnValue) as IProxy)?.Interceptor as Recorder;
public Recorder? Next => (Awaitable.TryGetResultRecursive(this.returnValue) as IProxy)?.Interceptor as Recorder;

public void Intercept(Invocation invocation)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Moq/AsInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public AsInterface(Mock owner)

internal override List<Type> AdditionalInterfaces => this.owner.AdditionalInterfaces;

internal override Dictionary<Type, object> ConfiguredDefaultValues => this.owner.ConfiguredDefaultValues;
internal override Dictionary<Type, object?> ConfiguredDefaultValues => this.owner.ConfiguredDefaultValues;

internal override object[] ConstructorArguments => this.owner.ConstructorArguments;
internal override object?[] ConstructorArguments => this.owner.ConstructorArguments;

internal override InvocationCollection MutableInvocations => this.owner.MutableInvocations;

Expand Down Expand Up @@ -49,7 +49,7 @@ public override DefaultValueProvider DefaultValueProvider

public override TInterface Object
{
get { return this.owner.Object as TInterface; }
get { return (TInterface)this.owner.Object; }
}

internal override SetupCollection MutableSetups => this.owner.MutableSetups;
Expand All @@ -72,7 +72,7 @@ protected override object OnGetObject()

public override string ToString()
{
return this.owner.ToString();
return this.owner.ToString()!;
}
}
}
2 changes: 1 addition & 1 deletion src/Moq/Async/Awaitable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static class Awaitable
/// this method will return <c>42</c>.
/// </remarks>
/// <param name="obj">The (possibly awaitable) object to be "unwrapped".</param>
public static object TryGetResultRecursive(object obj)
public static object? TryGetResultRecursive(object? obj)
{
if (obj != null
&& AwaitableFactory.TryGet(obj.GetType()) is { } awaitableFactory
Expand Down
2 changes: 1 addition & 1 deletion src/Moq/Async/AwaitableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static IAwaitableFactory Create(Type awaitableFactoryType, Type awaitableType)
{
return (IAwaitableFactory)Activator.CreateInstance(
awaitableFactoryType.MakeGenericType(
awaitableType.GetGenericArguments()));
awaitableType.GetGenericArguments()))!;
}

public static IAwaitableFactory? TryGet(Type type)
Expand Down
5 changes: 3 additions & 2 deletions src/Moq/Async/AwaitableFactory`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ namespace Moq.Async
/// for awaitables that do not produce a result when awaited.
/// </summary>
abstract class AwaitableFactory<TAwaitable> : IAwaitableFactory
where TAwaitable : notnull
{
Type IAwaitableFactory.ResultType => typeof(void);

public abstract TAwaitable CreateCompleted();

object IAwaitableFactory.CreateCompleted(object result)
object IAwaitableFactory.CreateCompleted(object? result)
{
Debug.Assert(result == null);

Expand Down Expand Up @@ -50,7 +51,7 @@ Expression IAwaitableFactory.CreateResultExpression(Expression awaitableExpressi
return new AwaitExpression(awaitableExpression, this);
}

bool IAwaitableFactory.TryGetResult(object awaitable, out object result)
bool IAwaitableFactory.TryGetResult(object awaitable, out object? result)
{
Debug.Assert(awaitable is TAwaitable);

Expand Down
13 changes: 10 additions & 3 deletions src/Moq/Async/AwaitableFactory`2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;

Expand All @@ -14,16 +15,18 @@ namespace Moq.Async
/// for awaitables that produce a result when awaited.
/// </summary>
abstract class AwaitableFactory<TAwaitable, TResult> : IAwaitableFactory
where TAwaitable : notnull
{
public Type ResultType => typeof(TResult);

public abstract TAwaitable CreateCompleted(TResult result);

object IAwaitableFactory.CreateCompleted(object result)
object IAwaitableFactory.CreateCompleted(object? result)
{
// TODO: result should only be null if TResult is a nullable type.
Debug.Assert(result is TResult || result == null);

return this.CreateCompleted((TResult)result);
return this.CreateCompleted((TResult)result!);
}

public abstract TAwaitable CreateFaulted(Exception exception);
Expand All @@ -45,11 +48,15 @@ object IAwaitableFactory.CreateFaulted(IEnumerable<Exception> exceptions)
return this.CreateFaulted(exceptions);
}

#if NULLABLE_REFERENCE_TYPES
public abstract bool TryGetResult(TAwaitable awaitable, [MaybeNullWhen(false)] out TResult result);
#else
public abstract bool TryGetResult(TAwaitable awaitable, out TResult result);
#endif

public abstract Expression CreateResultExpression(Expression awaitableExpression);

bool IAwaitableFactory.TryGetResult(object awaitable, out object result)
bool IAwaitableFactory.TryGetResult(object awaitable, out object? result)
{
Debug.Assert(awaitable is TAwaitable);

Expand Down
4 changes: 2 additions & 2 deletions src/Moq/Async/IAwaitableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ interface IAwaitableFactory
{
Type ResultType { get; }

object CreateCompleted(object result = null);
object CreateCompleted(object? result = null);

object CreateFaulted(Exception exception);

object CreateFaulted(IEnumerable<Exception> exceptions);

Expression CreateResultExpression(Expression awaitableExpression);

bool TryGetResult(object awaitable, out object result);
bool TryGetResult(object awaitable, out object? result);
}
}
2 changes: 1 addition & 1 deletion src/Moq/Async/TaskFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sealed class TaskFactory : AwaitableFactory<Task>

public override Task CreateCompleted()
{
return Task.FromResult<object>(default);
return Task.CompletedTask;
}

public override Task CreateFaulted(Exception exception)
Expand Down
9 changes: 7 additions & 2 deletions src/Moq/Async/TaskFactory`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Threading.Tasks;

Expand Down Expand Up @@ -33,10 +34,14 @@ public override Expression CreateResultExpression(Expression awaitableExpression
{
return Expression.MakeMemberAccess(
awaitableExpression,
typeof(Task<TResult>).GetProperty(nameof(Task<TResult>.Result)));
typeof(Task<TResult>).GetProperty(nameof(Task<TResult>.Result))!);
}

public override bool TryGetResult(Task<TResult> task, out TResult result)
#if NULLABLE_REFERENCE_TYPES
public override bool TryGetResult(Task<TResult> task, [MaybeNullWhen(false)] out TResult result)
#else
public override bool TryGetResult(Task<TResult> task, out TResult? result)
#endif
{
if (task.Status == TaskStatus.RanToCompletion)
{
Expand Down
7 changes: 6 additions & 1 deletion src/Moq/Async/ValueTaskFactory`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Threading.Tasks;

Expand Down Expand Up @@ -33,10 +34,14 @@ public override Expression CreateResultExpression(Expression awaitableExpression
{
return Expression.MakeMemberAccess(
awaitableExpression,
typeof(ValueTask<TResult>).GetProperty(nameof(ValueTask<TResult>.Result)));
typeof(ValueTask<TResult>).GetProperty(nameof(ValueTask<TResult>.Result))!);
}

#if NULLABLE_REFERENCE_TYPES
public override bool TryGetResult(ValueTask<TResult> valueTask, [MaybeNullWhen(false)] out TResult result)
#else
public override bool TryGetResult(ValueTask<TResult> valueTask, out TResult result)
#endif
{
if (valueTask.IsCompletedSuccessfully)
{
Expand Down
14 changes: 7 additions & 7 deletions src/Moq/Behaviors/RaiseEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ sealed class RaiseEvent : Behavior
{
Mock mock;
LambdaExpression expression;
Delegate eventArgsFunc;
object[] eventArgsParams;
Delegate? eventArgsFunc;
object?[]? eventArgsParams;

public RaiseEvent(Mock mock, LambdaExpression expression, Delegate eventArgsFunc, object[] eventArgsParams)
public RaiseEvent(Mock mock, LambdaExpression expression, Delegate? eventArgsFunc, object?[]? eventArgsParams)
{
Debug.Assert(mock != null);
Debug.Assert(expression != null);
Expand All @@ -28,22 +28,22 @@ public RaiseEvent(Mock mock, LambdaExpression expression, Delegate eventArgsFunc

public override void Execute(Invocation invocation)
{
object[] args;
object?[] args;

if (this.eventArgsParams != null)
{
args = this.eventArgsParams;
}
else
{
var argsFuncType = this.eventArgsFunc.GetType();
var argsFuncType = this.eventArgsFunc!.GetType();
if (argsFuncType.IsGenericType && argsFuncType.GetGenericArguments().Length == 1)
{
args = new object[] { this.mock.Object, this.eventArgsFunc.InvokePreserveStack() };
args = new object?[] { this.mock.Object, this.eventArgsFunc.InvokePreserveStack() };
}
else
{
args = new object[] { this.mock.Object, this.eventArgsFunc.InvokePreserveStack(invocation.Arguments) };
args = new object?[] { this.mock.Object, this.eventArgsFunc.InvokePreserveStack(invocation.Arguments) };
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Moq/Behaviors/ReturnBaseOrDefaultValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void Execute(Invocation invocation)
var tryCallDefaultInterfaceImplementation = false;
#endif

var declaringType = method.DeclaringType;
var declaringType = method.DeclaringType!;
if (declaringType.IsInterface)
{
if (this.mock.MockedType.IsInterface)
Expand Down
4 changes: 2 additions & 2 deletions src/Moq/Behaviors/ReturnComputedValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Moq.Behaviors
{
sealed class ReturnComputedValue : Behavior
{
readonly Func<IInvocation, object> valueFactory;
readonly Func<IInvocation, object?> valueFactory;

public ReturnComputedValue(Func<IInvocation, object> valueFactory)
public ReturnComputedValue(Func<IInvocation, object?> valueFactory)
{
Debug.Assert(valueFactory != null);

Expand Down
6 changes: 3 additions & 3 deletions src/Moq/Behaviors/ReturnValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace Moq.Behaviors
{
sealed class ReturnValue : Behavior
{
readonly object value;
readonly object? value;

public ReturnValue(object value)
public ReturnValue(object? value)
{
this.value = value;
}

public object Value => this.value;
public object? Value => this.value;

public override void Execute(Invocation invocation)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Moq/Behaviors/ThrowComputedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
{
sealed class ThrowComputedException : Behavior
{
readonly Func<IInvocation, Exception> exceptionFactory;
readonly Func<IInvocation, Exception?> exceptionFactory;

public ThrowComputedException(Func<IInvocation, Exception> exceptionFactory)
public ThrowComputedException(Func<IInvocation, Exception?> exceptionFactory)
{
Debug.Assert(exceptionFactory != null);

Expand All @@ -19,7 +19,8 @@

public override void Execute(Invocation invocation)
{
// TODO: Technically this permits `throw null` here.
throw this.exceptionFactory.Invoke(invocation);

Check warning on line 23 in src/Moq/Behaviors/ThrowComputedException.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

Thrown value may be null.
}
}
}
2 changes: 1 addition & 1 deletion src/Moq/Capture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static T In<T>(IList<T> collection, Expression<Func<T, bool>> predicate)
public static T With<T>(CaptureMatch<T> match)
{
Match.Register(match);
return default(T);
return default(T)!;
}
}
}
Loading