roslyn: Incorrect IDE0044 when NRTs are enabled, the class is generic, and the field is only set by a non-this reference

Visual Studio 16.3.1 and 16.4p1

IDE0044 should not be reported.

class GenericClass<T>
{
    // IDE0044 Make field readonly
    private int field;

    public static void Method(GenericClass<T> instance)
    {
        instance.field++;
    }
}

Similarly:

class GenericClass<T>
{
    // IDE0044 Make field readonly
    private int field;

    public struct Helper
    {
        private readonly GenericClass<T> instance;

        public Helper(GenericClass<T> instance)
        {
            this.instance = instance;
        }

        public void Method()
        {
            instance.field++;
        }
    }
}

Csproj:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <LangVersion>latest</LangVersion>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (17 by maintainers)

Most upvoted comments

Fixed via #41123

Was actually planning on starting #38195 on Monday 😃 aiming for 16.5 but we’ll see on timelines if it makes it in.