dotnet / roslyn-analyzers

MIT License
1.59k stars 466 forks source link

CA1515: false positive with delegates #7464

Open pcf0 opened 1 week ago

pcf0 commented 1 week ago

Analyzer

Diagnostic ID: CA1515: Consider making public types internal

Analyzer source

SDK: Built-in CA analyzers in .NET 9 SDK or later

Version: SDK 9.0.100-rc.2.24474.11

OR

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 9.0.0-preview.24454.1

Describe the bug

A public delegate is defined within an internal class. CA1859 is generated for the delegate.

Steps To Reproduce

  1. Create an executable project, e.g. a console project.
  2. Create a internal class with a public delegate
    internal class Class1
    {
    public delegate void SomeDelegate();
    }

Expected behavior

No errors reported

Actual behavior

Error reported: Class1.cs(3,26): error CA1515: Because an application's API isn't typically referenced from outside the assembly, types can be made internal (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1515)

Additional context

invino4 commented 1 day ago

This false positive seems to happen for any public nested type of an otherwise internal type. The analyzer SHOULD be checking if the target type is "effectively public" rather than marked public.