Skip to content

CA1034 is incorrectly reported for extension members #51681

@ezhevita

Description

@ezhevita

Describe the bug

Adding extension members to any class causes CA1034 (Nested types should not be visible) to be incorrectly reported.

To Reproduce

https://github.com/ezhevita/CA1034Repro

Exceptions (if any)

Program.cs(9,2): Warning CA1034 : Do not nest type . Alternatively, change its accessibility so that it is not externally visible. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1034)

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

This issue is caused due to extension members looking like a type for the analyzer while not being actually one. The fix would be to just filter these out by checking TypeKind against Extension somewhere around here:

// Do not report diagnostic for compiler generated nested types for delegate declaration
if (nestedType.TypeKind == TypeKind.Delegate)
{
return;
}

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