Skip to content

CA1515 is reported when extension members are present despite being suppressed in .editorconfig #51683

@ezhevita

Description

@ezhevita

Describe the bug

CA1515 (Consider making public types internal) is reported when it is silenced in .editorconfig, but only if the project has at least a single extension member included.

To Reproduce

https://github.com/ezhevita/CA1034Repro

This is actually a repro for another somewhat related issue, but they were mostly the same so I've merged them into one.

Exceptions (if any)

CSC : warning 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)

Further technical details

details of dotnet --info

.NET SDK: Version: 10.0.100-rc.2.25502.107 Commit: 89c8f6a112 Workload version: 10.0.100-manifests.0cee6f9c MSBuild version: 18.0.0-preview-25502-107+89c8f6a11

Runtime Environment:
OS Name: Mac OS X
OS Version: 15.7
OS Platform: Darwin
RID: osx-arm64
Base Path: /nix/store/cal0gw8y2hk29gzj348fi3lqpf4qmgyp-dotnet-sdk-10.0.100-rc.2.25502.107/share/dotnet/sdk/10.0.100-rc.2.25502.107/

.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.

Host:
Version: 10.0.0-rc.2.25502.107
Architecture: arm64
Commit: 89c8f6a112

.NET SDKs installed:
8.0.415 [/nix/store/x5s31j5yv843l8shp7ss7v7n4kpvfgq5-dotnet-combined/share/dotnet/sdk]
9.0.306 [/nix/store/x5s31j5yv843l8shp7ss7v7n4kpvfgq5-dotnet-combined/share/dotnet/sdk]
10.0.100-rc.2.25502.107 [/nix/store/x5s31j5yv843l8shp7ss7v7n4kpvfgq5-dotnet-combined/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.21 [/nix/store/x5s31j5yv843l8shp7ss7v7n4kpvfgq5-dotnet-combined/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.10 [/nix/store/x5s31j5yv843l8shp7ss7v7n4kpvfgq5-dotnet-combined/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.0-rc.2.25502.107 [/nix/store/x5s31j5yv843l8shp7ss7v7n4kpvfgq5-dotnet-combined/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.21 [/nix/store/x5s31j5yv843l8shp7ss7v7n4kpvfgq5-dotnet-combined/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.10 [/nix/store/x5s31j5yv843l8shp7ss7v7n4kpvfgq5-dotnet-combined/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.0-rc.2.25502.107 [/nix/store/x5s31j5yv843l8shp7ss7v7n4kpvfgq5-dotnet-combined/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
None

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

- The IDE (VS / VS Code/ VS4Mac) you're running on, and its version: irrelevant since it reproduces in CLI.

Analysis

Analyzer checks whether to report the diagnostic here:

if (namedTypeSymbol.IsPublic()
&& GetIdentifier(namedTypeSymbol.DeclaringSyntaxReferences[0].GetSyntax()) is SyntaxToken identifier)
{
context.ReportDiagnostic(identifier.CreateDiagnostic(Rule));
}

Then GetIdentifier attempt to extract Identifier from the type:

protected override SyntaxToken? GetIdentifier(SyntaxNode type) => type switch
{
TypeDeclarationSyntax tds => tds.Identifier,
EnumDeclarationSyntax eds => eds.Identifier,
DelegateDeclarationSyntax dds => dds.Identifier,
_ => null
};

ExtensionBlockDeclarationSyntax extends from TypeDeclarationSyntax, however it doesn't have a valid value for Identifier:
https://github.com/dotnet/roslyn/blob/76a39c1a97fe5c73549786fc6944dc188a7b7b3b/src/Compilers/CSharp/Portable/Syntax/ExtensionBlockDeclarationSyntax.cs#L9

Since Identifier is not null but doesn't have any information about source file, file filter in .editorconfig doesn't have anything to work with and thus the diagnostic is not filtered out as it should be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions