diff --git a/src/Riok.Mapperly/AnalyzerReleases.Shipped.md b/src/Riok.Mapperly/AnalyzerReleases.Shipped.md index 527de7cde8..bf813d94db 100644 --- a/src/Riok.Mapperly/AnalyzerReleases.Shipped.md +++ b/src/Riok.Mapperly/AnalyzerReleases.Shipped.md @@ -79,3 +79,9 @@ RMG030 | Mapper | Error | Reference loop detected while mapping to an init RMG031 | Mapper | Warning | Reference loop detected while mapping to a constructor member RMG032 | Mapper | Warning | The enum mapping strategy ByName cannot be used in projection mappings RMG033 | Mapper | Info | Object mapped to another object without deep clone + +### Removed Rules + +Rule ID | Category | Severity | Notes +--------|----------|----------|------- +RMG019 | Mapper | Disabled | Partial instance mapping method in a static mapper diff --git a/src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs b/src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs index f1316917cd..76987f63b6 100644 --- a/src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs +++ b/src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs @@ -67,7 +67,7 @@ bool isStatic BuildParameters(ctx, method, out var parameters) && !method.ReturnsVoid && (allowPartial || !method.IsPartialDefinition) - && isStatic == method.IsStatic; + && (!isStatic || method.IsStatic); return valid ? new UserImplementedMethodMapping(method, parameters.Source, parameters.ReferenceHandler) : null; } @@ -76,15 +76,10 @@ bool isStatic if (!methodSymbol.IsPartialDefinition) return null; - if (isStatic != methodSymbol.IsStatic) + if (!isStatic && methodSymbol.IsStatic) { - ctx.ReportDiagnostic( - isStatic - ? DiagnosticDescriptors.PartialInstanceMethodInStaticMapper - : DiagnosticDescriptors.PartialStaticMethodInInstanceMapper, - methodSymbol, - methodSymbol.Name - ); + ctx.ReportDiagnostic(DiagnosticDescriptors.PartialStaticMethodInInstanceMapper, methodSymbol, methodSymbol.Name); + return null; } diff --git a/src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs b/src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs index 690c50b0b9..76ed10a06f 100644 --- a/src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs +++ b/src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs @@ -168,15 +168,6 @@ internal static class DiagnosticDescriptors true ); - public static readonly DiagnosticDescriptor PartialInstanceMethodInStaticMapper = new DiagnosticDescriptor( - "RMG019", - "Partial instance mapping method in a static mapper", - "{0} is a partial instance mapping method in a static mapper. Instance mapping methods are only supported in instance (non-static) mappers.", - DiagnosticCategories.Mapper, - DiagnosticSeverity.Error, - true - ); - public static readonly DiagnosticDescriptor SourceMemberNotMapped = new DiagnosticDescriptor( "RMG020", "Source member is not mapped to any target member", diff --git a/test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs b/test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs index 283448845b..b433048229 100644 --- a/test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs @@ -13,7 +13,7 @@ public Task WithNamespaceShouldWork() } [Fact] - public Task StaticMapperShouldEmitDiagnosticForInstanceMethods() + public Task InstanceMapperShouldEmitDiagnosticForPartialStaticMethods() { var source = @" @@ -22,9 +22,9 @@ public Task StaticMapperShouldEmitDiagnosticForInstanceMethods() using Riok.Mapperly.Abstractions; [Mapper] -public static partial class MyStaticMapper +public partial class MyMapper { - public partial static object StaticToObject(string s); + public static object StaticToObject(string s); public partial object InstanceToObject(string s); } @@ -33,7 +33,7 @@ public static partial class MyStaticMapper } [Fact] - public Task InstanceMapperShouldEmitDiagnosticForStaticMethods() + public Task InstanceMapperShouldSupportUserDefinedStaticMethods() { var source = @" @@ -44,10 +44,13 @@ public Task InstanceMapperShouldEmitDiagnosticForStaticMethods() [Mapper] public partial class MyMapper { - public partial static object StaticToObject(string s); + public static int StaticMapper(int s) => s; - public partial object InstanceToObject(string s); + public partial B Map(A s); } + +public record A(int Value); +public record B(int Value); "; return TestHelper.VerifyGenerator(source); } diff --git a/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldEmitDiagnosticForStaticMethods#MyMapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldEmitDiagnosticForPartialStaticMethods#MyMapper.g.verified.cs similarity index 100% rename from test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldEmitDiagnosticForStaticMethods#MyMapper.g.verified.cs rename to test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldEmitDiagnosticForPartialStaticMethods#MyMapper.g.verified.cs diff --git a/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldEmitDiagnosticForStaticMethods.verified.txt b/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldEmitDiagnosticForStaticMethods.verified.txt deleted file mode 100644 index ba2017516a..0000000000 --- a/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldEmitDiagnosticForStaticMethods.verified.txt +++ /dev/null @@ -1,16 +0,0 @@ -{ - Diagnostics: [ - { - Id: RMG018, - Title: Partial static mapping method in an instance mapper, - Severity: Error, - WarningLevel: 0, - Location: : (8,4)-(8,58), - Description: , - HelpLink: , - MessageFormat: {0} is a partial static mapping method in an instance mapper. Static mapping methods are only supported in static mappers., - Message: StaticToObject is a partial static mapping method in an instance mapper. Static mapping methods are only supported in static mappers., - Category: Mapper - } - ] -} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldSupportUserDefinedStaticMethods#MyMapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldSupportUserDefinedStaticMethods#MyMapper.g.verified.cs new file mode 100644 index 0000000000..c08d21e4f1 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.InstanceMapperShouldSupportUserDefinedStaticMethods#MyMapper.g.verified.cs @@ -0,0 +1,10 @@ +//HintName: MyMapper.g.cs +#nullable enable +public partial class MyMapper +{ + public partial global::B Map(global::A s) + { + var target = new global::B(StaticMapper(s.Value)); + return target; + } +} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.StaticMapperShouldEmitDiagnosticForInstanceMethods#MyStaticMapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.StaticMapperShouldEmitDiagnosticForInstanceMethods#MyStaticMapper.g.verified.cs deleted file mode 100644 index e55763b2cb..0000000000 --- a/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.StaticMapperShouldEmitDiagnosticForInstanceMethods#MyStaticMapper.g.verified.cs +++ /dev/null @@ -1,9 +0,0 @@ -//HintName: MyStaticMapper.g.cs -#nullable enable -public static partial class MyStaticMapper -{ - public static partial object StaticToObject(string s) - { - return (object)s; - } -} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.StaticMapperShouldEmitDiagnosticForInstanceMethods.verified.txt b/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.StaticMapperShouldEmitDiagnosticForInstanceMethods.verified.txt deleted file mode 100644 index 33f2633f46..0000000000 --- a/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.StaticMapperShouldEmitDiagnosticForInstanceMethods.verified.txt +++ /dev/null @@ -1,16 +0,0 @@ -{ - Diagnostics: [ - { - Id: RMG019, - Title: Partial instance mapping method in a static mapper, - Severity: Error, - WarningLevel: 0, - Location: : (10,4)-(10,53), - Description: , - HelpLink: , - MessageFormat: {0} is a partial instance mapping method in a static mapper. Instance mapping methods are only supported in instance (non-static) mappers., - Message: InstanceToObject is a partial instance mapping method in a static mapper. Instance mapping methods are only supported in instance (non-static) mappers., - Category: Mapper - } - ] -} \ No newline at end of file