Skip to content

Commit

Permalink
Add check for nullable value type when excluding ToString from null s…
Browse files Browse the repository at this point in the history
…afeguard
  • Loading branch information
Danevandy99 committed May 18, 2024
1 parent 6744e69 commit 475fd88
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,13 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
}

// if object is nullable, add null safeguard before calling the function
// we special-case Nullable<>.GetValueOrDefault and Nullable<>.ToString, which don't need the safeguard
// we special-case Nullable<>.GetValueOrDefault, which doesn't need the safeguard,
// and Nullable<>.ToString when the object is a nullable value type.
if (methodCallExpression.Object != null
&& @object!.Type.IsNullableType()
&& methodCallExpression.Method.Name != nameof(Nullable<int>.GetValueOrDefault)
&& methodCallExpression.Method.Name != nameof(Nullable<int>.ToString))
&& (!@object!.Type.IsNullableValueType()
|| methodCallExpression.Method.Name != nameof(Nullable<int>.ToString)))
{
var result = (Expression)methodCallExpression.Update(
Expression.Convert(@object, methodCallExpression.Object.Type),
Expand Down

0 comments on commit 475fd88

Please sign in to comment.