Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated code should suppress warnings about obsolete members #610

Open
hankovich opened this issue Jul 31, 2023 · 1 comment
Open

Generated code should suppress warnings about obsolete members #610

hankovich opened this issue Jul 31, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@hankovich
Copy link

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.

@TimothyMakkison
Copy link
Collaborator

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.

@latonz latonz added the enhancement New feature or request label Mar 11, 2024
@latonz latonz changed the title Generated mappers should supress warnings about obsolete members Generated code should suppress warnings about obsolete members Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants