mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Generated code should suppress warnings about obsolete members

Open hankovich opened this issue 2 years ago • 1 comments

With the following project

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Riok.Mapperly" Version="2.8.0" />
  </ItemGroup>

</Project>
using Riok.Mapperly.Abstractions;

Console.WriteLine(new Source().MapToTarget());

internal sealed class Source
{
    [Obsolete]
    public int Id { get; set; }
}

internal sealed class Target
{
    public int Id { get; set; }
}

[Mapper]
internal static partial class Mapper
{
    public static partial Target MapToTarget(this Source source);
}
root = true

[*.cs]

# CS0612: Member is obsolete
dotnet_diagnostic.CS0612.severity = none

This code is generated by Riok.Mapperly

#nullable enable
internal static partial class Mapper
{
    public static partial global::Target MapToTarget(this global::Source source)
    {
        var target = new global::Target();
        target.Id = source.Id; // <- a diagnostic is reported here
        return target;
    }
}

and the CS0612 diagnostic is reported durung accessing obsolete members.

Expected Behavior:

No CS0612 diagnostic is reported.

Actual Behavior:

A diagnostic C:\Users\Hello\source\repos\ConsoleApp2\ConsoleApp2\Riok.Mapperly\Riok.Mapperly.MapperGenerator\Mapper.g.cs(7,21,7,30): warning CS0612: 'Source.Id' is obsolete is reported.

During the discussion in github.com/dotnet/roslyn/issues/69298 @sharwell said this warning should be supressed in Riok.Mapperly.

hankovich avatar Jul 31 '23 19:07 hankovich

Thanks for raising the issue. tbh I thought editor config applied to generated code as well 🤔

As you suggested I can disable the warning in each generated file, which would fix this issue.

I've considered disabling the warning for each use of an obsolete member if ObsoleteMappingStrategy is set. I decided that it would be too hard to insert the suppression without breaking a lot of code. Unfortunately, there might be some cases where users aren't warned that nested obsolete members are being set even when using ObsoleteMappingStrategy, as it applies to top level member only.

Aside from that I can't think of any other solutions.

TimothyMakkison avatar Aug 01 '23 22:08 TimothyMakkison