Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA1416 warnings are erroneously shown in lambdas which are only reachable after platform checks #7323

Closed
jnm2 opened this issue Jun 4, 2024 · 2 comments

Comments

@jnm2
Copy link
Contributor

jnm2 commented Jun 4, 2024

Analyzer

Diagnostic ID: CA1416: Validate platform compatibility

Analyzer source

Version: SDK 8.0.300

This is particularly agonizing for APIs such as the host/services builders.

The below should not show warnings, but it does:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

if (!OperatingSystem.IsWindows()) return 1;

await Host.CreateDefaultBuilder(args)
    .ConfigureServices((hostContext, services) =>
    {
        services.AddLogging(builder =>
        {
            // ⚠️ CA1416: This call site is reachable on all platforms. 'EventLoggerFactoryExtensions.AddEventLog(
            // ILoggingBuilder, Action<EventLogSettings>)' is only supported on: 'windows'.
            builder.AddEventLog(eventLogSettings =>
            {
                // ⚠️ CA1416: This call site is reachable on all platforms. 'EventLogSettings.SourceName' is only
                // supported on: 'windows'.
                eventLogSettings.SourceName = "Foo";
            });
        });
    }).Build().RunAsync();
return 0;

The workaround makes it clear that this is an inconsistency in the analyzer's behavior; an implicit "SupportedOSPlatform" should be applied to all statements following if (!OperatingSystem.IsWindows()) return 1; without manually stating this:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Runtime.Versioning;

if (!OperatingSystem.IsWindows()) return 1;

[SupportedOSPlatform("windows")] void Configure(HostBuilderContext hostContext, IServiceCollection services)
{
    services.AddLogging(builder =>
    {
        builder.AddEventLog(eventLogSettings =>
        {
            eventLogSettings.SourceName = "Foo";
        });
    });
}

await Host.CreateDefaultBuilder(args).ConfigureServices(Configure).Build().RunAsync();
return 0;
@buyaa-n
Copy link
Contributor

buyaa-n commented Sep 6, 2024

Closing as duplicate of #4282, which is closed as by design

The flow analysis could not evaluate a lambda expression unless it is explicitly invoked. Please check the issue for details

The workaround makes it clear that this is an inconsistency in the analyzer's behavior; an implicit "SupportedOSPlatform" should be applied to all statements following if (!OperatingSystem.IsWindows()) return 1; without manually stating this:

The suggested workaround is to add a Debug.Assert at the start of the lambda for the expected OS information.

@buyaa-n buyaa-n closed this as not planned Won't fix, can't repro, duplicate, stale Sep 6, 2024
@jnm2
Copy link
Contributor Author

jnm2 commented Sep 6, 2024

@buyaa-n Thanks. I asked for clarification in the linked issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants