Skip to content

Commit

Permalink
fix: throw NullReferenceException for element access (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed May 9, 2023
1 parent 98a44a0 commit 78b2736
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs
Expand Up @@ -77,6 +77,12 @@ public static ExpressionSyntax NullSubstitute(ITypeSymbol t, ExpressionSyntax ar
NullFallbackValue.Default => DefaultLiteral(),
NullFallbackValue.EmptyString => StringLiteral(string.Empty),
NullFallbackValue.CreateInstance => CreateInstance(t),
_ when argument is ElementAccessExpressionSyntax memberAccess
=> ThrowNullReferenceException(
InterpolatedString(
$"Sequence {NameOf(memberAccess.Expression)}, contained a null value at index {memberAccess.ArgumentList.Arguments[0].Expression}."
)
),
_ => ThrowArgumentNullException(argument),
};
}
Expand Down Expand Up @@ -165,6 +171,13 @@ public static ThrowExpressionSyntax ThrowNullReferenceException(string message)
);
}

public static ThrowExpressionSyntax ThrowNullReferenceException(ExpressionSyntax arg)
{
return ThrowExpression(
ObjectCreationExpression(IdentifierName(NullReferenceExceptionClassName)).WithArgumentList(ArgumentList(arg))
);
}

public static ThrowExpressionSyntax ThrowArgumentOutOfRangeException(ExpressionSyntax arg, string message)
{
return ThrowExpression(
Expand Down
6 changes: 3 additions & 3 deletions test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs
Expand Up @@ -35,7 +35,7 @@ public void ArrayOfNullablePrimitiveTypesToNonNullableArray()
var target = new int[source.Length];
for (var i = 0; i < source.Length; i++)
{
target[i] = source[i] == null ? throw new System.ArgumentNullException(nameof(source[i])) : source[i].Value;
target[i] = source[i] == null ? throw new System.NullReferenceException($"Sequence {nameof(source)}, contained a null value at index {i}.") : source[i].Value;
}

return target;
Expand Down Expand Up @@ -82,7 +82,7 @@ public void ArrayOfNullablePrimitiveTypesToNonNullableArrayDeepCloning()
var target = new int[source.Length];
for (var i = 0; i < source.Length; i++)
{
target[i] = source[i] == null ? throw new System.ArgumentNullException(nameof(source[i])) : source[i].Value;
target[i] = source[i] == null ? throw new System.NullReferenceException($"Sequence {nameof(source)}, contained a null value at index {i}.") : source[i].Value;
}

return target;
Expand Down Expand Up @@ -129,7 +129,7 @@ public void ArrayCustomClassNullableToArrayCustomClassNonNullable()
var target = new global::B[source.Length];
for (var i = 0; i < source.Length; i++)
{
target[i] = source[i] == null ? throw new System.ArgumentNullException(nameof(source[i])) : source[i];
target[i] = source[i] == null ? throw new System.NullReferenceException($"Sequence {nameof(source)}, contained a null value at index {i}.") : source[i];
}

return target;
Expand Down

0 comments on commit 78b2736

Please sign in to comment.