Skip to content

Commit

Permalink
fix: disable warning when iterating nullable array elements (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Jul 25, 2023
1 parent 9b74e8f commit 8f37f44
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMapping.cs
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Helpers;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
Expand Down Expand Up @@ -60,6 +61,12 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
// source == null ? <null-substitute> : Map(source.Value)
var sourceValue = SourceType.IsNullableValueType() ? MemberAccess(ctx.Source, NullableValueProperty) : ctx.Source;

// disable nullable waring if accessing array
if (sourceValue is ElementAccessExpressionSyntax)
{
sourceValue = PostfixUnaryExpression(SyntaxKind.SuppressNullableWarningExpression, sourceValue);
}

return ConditionalExpression(
IsNull(ctx.Source),
NullSubstitute(TargetType.NonNullable(), ctx.Source, _nullFallbackValue),
Expand Down
2 changes: 1 addition & 1 deletion test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs
Expand Up @@ -82,7 +82,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.NullReferenceException($"Sequence {nameof(source)}, contained a null value at index {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 8f37f44

Please sign in to comment.