coverlet icon indicating copy to clipboard operation
coverlet copied to clipboard

Incorrect branch coverage with `await using` in generic methods

Open arseni-mourzenko opened this issue 1 year ago • 2 comments

Hi.

Using coverlet.msbuild.6.0.0, I noticed an issue with code coverage in generic methods that contain await using.

Here's a short example which reproduces the issue:

public static class A
{
    public static async Task<int> Demo()
    {
        await using var example = new DisposableExample<int>();
        var result = await Task.FromResult(42);
        return result;
    }

    public static async Task<T> Demo<T>()
    {
        await using var example = new DisposableExample<T>();
        var result = await Task.FromResult(default(T));
        return result;
    }
}

public class DisposableExample<T> : IAsyncDisposable
{
    public ValueTask DisposeAsync()
    {
        GC.SuppressFinalize(this);
        return ValueTask.CompletedTask;
    }
}

Given the following tests:

[Fact] public async Task One() => Assert.Equal(42, await A.Demo());
[Fact] public async Task Two() => Assert.Null(await A.Demo<string>());

the first, non-generic method Demo() will have a 100% branch coverage. The branch coverage of generic method, Demo<T>(), will show a missing branch on line return result; (1 visits, 3 of 4 branches are covered).

Unless I missed something, I would expect the branch coverage to be 100% in both cases.

arseni-mourzenko avatar Jun 07 '23 13:06 arseni-mourzenko

Looks like it can be the same issue as #1333, and maybe related to #1284 and #1448.

petli avatar Jun 07 '23 13:06 petli

This issue is stale because it has been open for 3 months with no activity.

github-actions[bot] avatar Dec 03 '23 01:12 github-actions[bot]