Skip to content

Commit 143564f

Browse files
andrewimcclementkzu
authored andcommitted
Some nullability improvements to ExpressionExtensions.cs. It seems like nullability is a little dodgy here.
1 parent f47c1be commit 143564f

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/Moq/ExpressionExtensions.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Globalization;
89
using System.Linq;
910
using System.Linq.Expressions;
@@ -64,7 +65,11 @@ internal static TDelegate CompileUsingExpressionCompiler<TDelegate>(this Express
6465
return ExpressionCompiler.Instance.Compile(expression);
6566
}
6667

67-
public static bool IsMatch(this Expression expression, out Match match)
68+
#if NULLABLE_REFERENCE_TYPES
69+
public static bool IsMatch(this Expression expression, [NotNullWhen(true)] out Match? match)
70+
#else
71+
public static bool IsMatch(this Expression expression, out Match? match)
72+
#endif
6873
{
6974
if (expression is MatchExpression matchExpression)
7075
{
@@ -262,6 +267,7 @@ void Split(Expression e, out Expression r /* remainder */, out MethodExpectation
262267
}
263268
else // This should be unreachable.
264269
{
270+
// TODO: Should we throw here?
265271
method = null;
266272
}
267273
p = new MethodExpectation(
@@ -286,7 +292,7 @@ void Split(Expression e, out Expression r /* remainder */, out MethodExpectation
286292
expression: Expression.Lambda(
287293
Expression.Invoke(parameter, arguments),
288294
parameter),
289-
method: r.Type.GetMethod("Invoke", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance),
295+
method: r.Type.GetMethod("Invoke", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)!,
290296
arguments);
291297
return;
292298
}
@@ -321,7 +327,8 @@ void Split(Expression e, out Expression r /* remainder */, out MethodExpectation
321327
}
322328
else // This should be unreachable.
323329
{
324-
method = null;
330+
// TODO: Should we throw here?
331+
method = null!;
325332
}
326333
p = new MethodExpectation(
327334
expression: Expression.Lambda(
@@ -341,7 +348,7 @@ void Split(Expression e, out Expression r /* remainder */, out MethodExpectation
341348

342349
bool IsResult(MemberInfo member, out IAwaitableFactory? awaitableFactory)
343350
{
344-
var instanceType = member.DeclaringType;
351+
var instanceType = member.DeclaringType!;
345352
awaitableFactory = AwaitableFactory.TryGet(instanceType);
346353
var returnType = member switch
347354
{
@@ -363,7 +370,7 @@ internal static PropertyInfo GetReboundProperty(this MemberExpression expression
363370
// the expression. we attempt to correct this here by checking whether the type of the accessed object
364371
// has a property by the same name whose base definition equals the property in the expression; if so,
365372
// we "upgrade" to the derived property.
366-
if (property.DeclaringType != expression.Expression.Type)
373+
if (property.DeclaringType != expression.Expression!.Type)
367374
{
368375
var parameterTypes = new ParameterTypes(property.GetIndexParameters());
369376
var derivedProperty = expression.Expression.Type

0 commit comments

Comments
 (0)