Skip to content

Refering to unknown platform names should result in warnings  #45851

@terrajobst

Description

@terrajobst

We use string literals for OS platform names. We should validate that the name is known. The list is the combined set from two places:

  1. OperatingSystem.Is(PlatformName)[VersionAtLeast](). That's the target framework's known set of platforms.
  2. The project's MSBuild item group SupportedPlatform. This is the project's specific knowledge of known platforms. This allows class library authors to target an older version of .NET that can still light up on later versions with more platform support.

Note: The diagnostics should be raised for the definitions the attributes are applied to, not use sites of the APIs. IOW, APIs with unknown platform annotations should be treated as having no attributes applied. This avoids cascading errors for the author.

Note: We should treat the universal OperatingSystem.IsPlatform() and OperatingSystem.IsPlatformVersionAtLeast() similar to attributes in that if we can't statically determine its value or if the value isn't in the known set, we should provide a warning.

Repro

class Some
{    
    public static void UseSite()
    {
        SomeType.Api1();
        SomeType.Api2();
        SomeType.Api3();
    }

    public static void GuardedCall()
    {
        if (OperatingSystem.IsOSPlatform("x")) // $1
            SomeType.Api1();

        if (OperatingSystem.IsOSPlatformVersionAtLeast("x", 1)) // $2
            SomeType.Api1();
    }
}

class SomeType
{
    [SupportedOSPlatform("x")] // $3
    public static void Api1() { }

    [UnsupportedOSPlatform("y")] // $4
    public static void Api2() { }

    [UnsupportedOSPlatform("z")] // $5
    public static void Api3() { }
}

Expected

$1: The platform 'x' isn't a known platform name.
$2: The platform 'x' isn't a known platform name.
$3: The platform 'x' isn't a known platform name.
$4: The platform 'y' isn't a known platform name.
$5: The platform 'z' isn't a known platform name.

Metadata

Metadata

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzer

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions