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

Code analysis IDE0039 quick fix does not respect nullability of async return value #76447

Open
vsfeedback opened this issue Dec 16, 2024 · 1 comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


var testTask = async () =>
{
await Task.Delay(1);
if (Random.Shared.Next(2) == 0)
{
return "test";
}
return null;
};

Quick fix result:

static async Task<string> testTask()
{
await Task.Delay(1);
if (Random.Shared.Next(2) == 0)
{
return "test";
}
return null;
}

It should be Task<string?> because it has a nullable return value "return null"


Original Comments

Feedback Bot on 12/16/2024, 00:24 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 16, 2024
@CyrusNajmabadi
Copy link
Member

I've debugged through. When we ask the compiler what the delegate type of the lambda is (using semanticModel.GetTypeInfo(anonymousFunction, cancellationToken) it returns Func<Task<string>>, which can also be seen here in quick info:

Image

As such, when we make the local function, we make it have a return type of Task<string>, not Task<string?>. I do not know why the compiler thinks the return type here is that.

Note: you can repro this without the feature, just with no compiler warnings like so:

    void M()
    {
        var testTask = async () =>
        {
            await Task.Delay(1);
            if (Random.Shared.Next(2) == 0)
            {
                return "test";
            }
            return null;
        };

        Func<Task<string>> s = testTask;
    }

This assignment should warn. If you try to write:

    void M()
    {
        Func<Task<string>> testTask = async () =>
        {
            await Task.Delay(1);
            if (Random.Shared.Next(2) == 0)
            {
                return "test";
            }
            return null;
        };
    }

You do get a warning, as hte compiler clearly can tell this body doesn't support that specified return type. But something about the 'var' case breaks it.

@jcouv jcouv added the Feature - Nullable Reference Types Nullable Reference Types label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

3 participants