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

"Intermittent" CAC001 on await using code #32

Open
martyt opened this issue Jun 28, 2022 · 1 comment
Open

"Intermittent" CAC001 on await using code #32

martyt opened this issue Jun 28, 2022 · 1 comment

Comments

@martyt
Copy link

martyt commented Jun 28, 2022

Here's a minimal .NET 6 console app repro - using ConfigureAwaitChecker.Analyzer v5.0.0.1, in VS2022 Professional v17.2.5


Console.WriteLine("Hello, World!");

var y = await DoSomethingElseAsync().ConfigureAwait(false);

async Task<int> DoSomethingElseAsync()
{
    var foo = new Foo();
    await using var s = await foo.GetStreamAsync().ConfigureAwait(false);

    return 0;
}

public class Foo
{
    public async Task<Stream> GetStreamAsync()
    {
        return await this.AllocateStreamAsync().ConfigureAwait(false);
    }

    private async Task<Stream> AllocateStreamAsync()
    {
        return await Task.FromResult(new MemoryStream()).ConfigureAwait(false);
    }
}

I'm getting an "intermittent" CAC001 error on the await using call at line 8. And by "intermittent" I mean sometimes it's there, sometimes it's not - see the following screen captures:
image
After doing nothing but deleting the blank line after Console.WriteLine() I get this
image
And then sometimes it disappears completely and comes back seemingly at random based on unrelated code changes.

@cincuranet
Copy link
Owner

Not sure why it's intermittent, but I'll check how's the analyzer called and how the tree looks like.

The "fixed" code should be something like await using var s = (await foo.GetStreamAsync().ConfigureAwait(false)).ConfigureAwait(false); (or maybe rather var x = await foo.GetStreamAsync().ConfigureAwait(false); await using var s = x.ConfigureAwait(false);, but that's related to #28).

Note to self: Add tests for await using var x = (await foo.FooAsync().ConfigureAwait(false)).ConfigureAwait(false);.

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