dotnet / roslyn-analyzers

MIT License
1.55k stars 460 forks source link

CA1805: Do not initialize unnecessarily is suppressed when any attribute is applied to the member #7341

Open solah1701 opened 6 days ago

solah1701 commented 6 days ago

For testing of scenarios, a suppression for IDE0044 was applied to this member in order to test that CA1805 was being successfully triggered, however in doing so, CA1805 was also being suppressed:

        [System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Used for testing CA1805")]
    class CA1805
    {
        //CA1805:Do not initialize unnecessarily
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Used for testing CA1805")]
        int _value1 = 0;
        //CA1805:Do not initialize unnecessarily
        static readonly int _value2 = 0;
    }

The comment within your source indicates that attributes are excluded in case they impact nullability analysis, however even scalar attributes are now impacted by this approach https://github.com/dotnet/roslyn-analyzers/blob/43709af7570da7140fb3e9a5237f55ffb24677e7/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/DoNotInitializeUnnecessarily.cs#L50

Is it possible restrict only attributes which do impact nullability analysis?