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

False positive CA1508 with Parallel.Invoke #7534

Open
rampaa opened this issue Jan 13, 2025 · 2 comments
Open

False positive CA1508 with Parallel.Invoke #7534

rampaa opened this issue Jan 13, 2025 · 2 comments

Comments

@rampaa
Copy link

rampaa commented Jan 13, 2025

Description

    public static bool MyAwesomeMethod(bool doIt)
    {
        string? myAwesomeName = null;
        Parallel.Invoke(() =>
        {
            myAwesomeName = doIt
                ? "Banzai!"
                : null;
        });
        return myAwesomeName is not null;
    }

The code given above triggers the following warning: CA1508 'myAwesomeName is not null' is always 'false'. Remove or refactor the condition(s) to avoid dead code.

But ideally speaking, the given code should not trigger CA1508 because at the point we check the value of the object, it can have a non-null value.

Reproduction Steps

  1. Add <AnalysisLevel>9-all</AnalysisLevel> to the csproj file
  2. Compile the code given in the Description section

Expected behavior

CA1508 shouldn't get triggered because at the point we check the value of the object, it can have a non-null value.

Actual behavior

CA1508 is being triggered

Regression?

No response

Known Workarounds

No response

Configuration

.NET 9

Other information

No response

@rampaa rampaa changed the title CA1508 with Parallel.Invoke False positive CA1508 with Parallel.Invoke Jan 13, 2025
Copy link

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@stephentoub
Copy link
Member

This isn't specific to Parallel.Invoke; whether Parallel.Invoke will invoke the delegate is part of Invoke's implementation, which the analyzer/compiler isn't privvy to, and thus it doesn't know that Invoke is guaranteed to invoke the delegate... this would be the case for any other such method that accepted a lambda/delegate.

That said, the analyzer really shouldn't be issuing this diagnostic here, simply because it can't know whether the code is dead or not.

@stephentoub stephentoub transferred this issue from dotnet/runtime Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants