Search code examples
c#.net-corecode-analysis.net-5roslyn-code-analysis

CA1034 is not being shown, even with <AnalysisMode>AllEnabledByDefault</AnalysisMode>


I've created the class given as an example by Microsoft:

internal class ParentType
{
    public class NestedType
    {
        public NestedType()
        {
        }
    }

    public ParentType()
    {
        NestedType nt = new();
    }
}

But no warning seems to be generated. I'm using Visual Studio 2019. The framework this code is being tested on is .NET 5


Solution

  • Warning CA1034 only gets reported on externally visible nested types, which are declared within another externally visible type. Since your enclosing type is internal, your nested type cannot be accessed from outside of this assembly either, therefore the rule is not violated.

    You will see the diagnostic being reported when you make the enclosing type public as well:

    public class ParentType
    {
        public class NestedType // Warning CA1034
        {
        }
    }
    

    So I believe you've discovered an error in the example of the official docs.

    Update: The official docs are now updated with the fixed example code.