Skip to content

CA1416 with multiple guard attributes #7665

Description

@rolfbjarne

Analyzer

Diagnostic ID: warning CA1416: This call site is reachable on: 'macOS/OSX' 11.0 and later, 'tvos' 12.2 and later. 'TestType.PerformanceRating' is only supported on: 'tvos' 13.0 and later.

Analyzer source

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

$ .dotnet/dotnet --version
10.0.100-preview.3.25167.3

Describe the bug

Incorrect CA1416 when using multiple guard attributes.

Test code (from provided unit test):

using System;
using System.Runtime.Versioning;

// Commenting out either the macOS or the tvOS attributes (in all three places) makes this work.
[assembly: SupportedOSPlatform ("macos12.0")]
[assembly: SupportedOSPlatform ("tvos12.2")]

partial class TestType {
    void DoSomething ()
    {
        if (IsAtLeastXcode11) {
            Console.WriteLine (PerformanceRating); // warning CA1416: This call site is reachable on: 'macOS/OSX' 11.0 and later, 'tvos' 12.2 and later. 'TestType.PerformanceRating' is only supported on: 'tvos' 13.0 and later.
        }
    }

    [SupportedOSPlatform ("macos11.0")]
    [SupportedOSPlatform ("tvos13.0")]
    public ulong? PerformanceRating { get; private set; }

    [SupportedOSPlatformGuard ("macos11.0")]
    [SupportedOSPlatformGuard ("tvos13.0")]
    internal static bool IsAtLeastXcode11 {
        get {
            return true;
        }
    }
}

One interesting thing here is that commenting out either all the tvOS attributes or all the macOS attributes makes the warning go away.

Steps To Reproduce

Run this unit test: rolfbjarne@8746314

Expected behavior

Unit test should pass

Actual behavior

Unit test fails with:

System.InvalidOperationException : Context: Diagnostics of test state
Mismatch between number of diagnostics returned, expected "0" actual "1"

Diagnostics:
// /0/Test0.cs(12,32): warning CA1416: This call site is reachable on: 'macOS/OSX' 11.0 and later, 'tvos' 12.2 and later. 'TestType.PerformanceRating' is only supported on: 'tvos' 13.0 and later.
VerifyCS.Diagnostic(PlatformCompatibilityAnalyzer.CA1416).WithSpan(12, 32, 12, 49).WithArguments("TestType.PerformanceRating", "'tvos' 13.0 and later", "'macOS/OSX' 11.0 and later, 'tvos' 12.2 and later"),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions