dotnet / roslyn-analyzers

MIT License
1.6k stars 466 forks source link

SupportedOSPlatformAttribute have no effect on local functions #7437

Open Arthri opened 1 month ago

Arthri commented 1 month ago

Analyzer

Diagnostic ID: CA1416: Validate platform compatibility

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 8.0.0

Describe the bug

Invoking a platform-specific API inside a local function annotated with a SupportedOSPlatformAttribute corresponding to the platform-specific API still leads to the CA1416 diagnostic being raised.

Steps To Reproduce

public class Class1
{
    [SupportedOSPlatform("windows")]
    private static void X()
    {
        _ = Registry.LocalMachine;
    }

    private static void Y()
    {
        [SupportedOSPlatform("windows")]
        static void L()
        {
            // CA1416: This call site is reachable on all platforms. 'Registry.LocalMachine' is only supported on: 'windows'.
            _ = Registry.LocalMachine;
        }
    }

    [SupportedOSPlatform("windows")]
    private static void Z()
    {
        static void L()
        {
            _ = Registry.LocalMachine;
        }
    }
}

Minimal working project https://github.com/Arthri/demo-roslyn-sop

Expected behavior

No CA1416 warnings.

Actual behavior

One CA1416 raised in Y's local function L due to invocation of Registry methods.

Additional context

none