diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/NullDelegateExistingTargetMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/NullDelegateExistingTargetMapping.cs index 329c6bad31..56b33dde65 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/NullDelegateExistingTargetMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/NullDelegateExistingTargetMapping.cs @@ -33,8 +33,15 @@ public override IEnumerable Build(TypeMappingBuildContext ctx, if (!SourceType.IsNullable() && !TargetType.IsNullable()) return body; + var enumerated = body.ToArray(); + + // if body is empty don't generate an if statement + if (!enumerated.Any()) + { + return Enumerable.Empty(); + } + // if (source != null && target != null) { body } - return new[] { IfStatement(IfNoneNull((SourceType, ctx.Source), (TargetType, target)), Block(body)), }; - ; + return new[] { IfStatement(IfNoneNull((SourceType, ctx.Source), (TargetType, target)), Block(enumerated)), }; } } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.SnapshotGeneratedSource.verified.cs index 69c860dde3..1571a1b5f2 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/MapperTest.SnapshotGeneratedSource.verified.cs @@ -178,10 +178,6 @@ public partial int ParseableInt(string value) target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable); target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable; target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType; - if (dto.SpanValue != null) - { - } - target.MemoryValue = MapToStringArray(dto.MemoryValue.Span); target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString())); target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString())); diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.SnapshotGeneratedSource.verified.cs index 6321bdf662..9472fa3826 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/NET_48/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -271,10 +271,6 @@ public static partial int ParseableInt(string value) target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable); target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable; target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType; - if (dto.SpanValue != null) - { - } - target.MemoryValue = MapToStringArray(dto.MemoryValue.Span); target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString())); target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString())); diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs index 63583c5592..a6c1ff0f3f 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/MapperTest.SnapshotGeneratedSource.verified.cs @@ -174,10 +174,6 @@ public partial int ParseableInt(string value) target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable); target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable; target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType; - if (dto.SpanValue != null) - { - } - target.MemoryValue = MapToStringArray(dto.MemoryValue.Span); target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString())); target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString())); diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs index ab6f019a34..a9079eae69 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_4_OR_LOWER/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -266,10 +266,6 @@ public static partial int ParseableInt(string value) target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable); target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable; target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType; - if (dto.SpanValue != null) - { - } - target.MemoryValue = MapToStringArray(dto.MemoryValue.Span); target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString())); target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString())); diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.SnapshotGeneratedSource.verified.cs index 6045c2198d..71c0f5cb6e 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/MapperTest.SnapshotGeneratedSource.verified.cs @@ -180,10 +180,6 @@ public partial int ParseableInt(string value) target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable); target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable; target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType; - if (dto.SpanValue != null) - { - } - target.MemoryValue = MapToStringArray(dto.MemoryValue.Span); target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString())); target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString())); @@ -444,4 +440,4 @@ private string MapToString1(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumD return target; } } -} \ No newline at end of file +} diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.SnapshotGeneratedSource.verified.cs index 699997e00c..dc7814fef4 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/Roslyn_4_5/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -275,10 +275,6 @@ public static partial int ParseableInt(string value) target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable); target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable; target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType; - if (dto.SpanValue != null) - { - } - target.MemoryValue = MapToStringArray(dto.MemoryValue.Span); target.StackValue = new global::System.Collections.Generic.Stack(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString())); target.QueueValue = new global::System.Collections.Generic.Queue(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString())); diff --git a/test/Riok.Mapperly.Tests/Mapping/NullableTest.cs b/test/Riok.Mapperly.Tests/Mapping/NullableTest.cs index feacc1bf0a..5369fb4bce 100644 --- a/test/Riok.Mapperly.Tests/Mapping/NullableTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/NullableTest.cs @@ -165,6 +165,42 @@ public void WithExistingInstanceNullableSource() ); } + [Fact] + public void ToTargetShouldNotGenerateEmptyIfStatement() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "partial B Map(A source)", + "class A { public C Value { get; set; } }", + "class B { public D? Value { get; } }", + "class C { public int Id { get; set; } }", + "class D { public int Id { get; } }" + ); + + TestHelper + .GenerateMapper(source, TestHelperOptions.AllowDiagnostics) + .Should() + .HaveSingleMethodBody( + """ + var target = new global::B(); + return target; + """ + ); + } + + [Fact] + public void ExistingTargetShouldNotGenerateEmptyIfStatement() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "partial void Map(A source, B destination)", + "class A { public C Value { get; set; } }", + "class B { public D? Value { get; } }", + "class C { public int Id { get; set; } }", + "class D { public int Id { get; } }" + ); + + TestHelper.GenerateMapper(source, TestHelperOptions.AllowDiagnostics).Should().HaveSingleMethodBody(""); + } + [Fact] public Task ShouldUpgradeNullabilityInDisabledNullableContext() {