coverlet icon indicating copy to clipboard operation
coverlet copied to clipboard

[BUG] HasValue of Nullable<T> is not taken into account

Open mu88 opened this issue 1 year ago • 2 comments

Describe the bug After ensuring that an instance of Nullable<T> is not null via .HasValue, coverlet still assumes the instance to be nullable and therefore the branch coverage indicates a missing test branch.

To Reproduce

public static class ServiceExtensions
{
    public static bool IsInPast(this IService service)
    {
        Nullable<TimeOnly> timeFromService = service.GetTime();
        if (!timeFromService.HasValue) return true;

        bool isInPast = timeFromService < TimeOnly.FromDateTime(DateTime.UtcNow);

        return isInPast;
    }
}

Here you find a complete repro case.

Expected behavior

Branch coverage is 100% for the line var isInPast = timeFromService < TimeOnly.FromDateTime(DateTime.UtcNow);.

Actual behavior

Coverlet complains that only 1 out of 2 branches is tested as it assumes that timeFromService can still be null:
image

Configuration (please complete the following information): Please provide more information on your .NET configuration:

  • Which coverlet package and version was used? → coverlet.msbuild v6.0.2
  • Which version of .NET is the code running on? → .NET 8
  • What OS and version, and what distro if applicable? → Windows 10, 22H2
  • What is the architecture (x64, x86, ARM, ARM64)? → x64
  • Do you know whether it is specific to that configuration? → it is not specific to that configuration

Additional context

Using timeFromService.Value rather than timeFromService can be used as a workaround.

:exclamation: Please also read Known Issues

mu88 avatar Mar 19 '24 12:03 mu88