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;
}
}
Describe the bug After ensuring that an instance of
Nullable<T>
is notnull
via.HasValue
, coverlet still assumes the instance to be nullable and therefore the branch coverage indicates a missing test branch.To Reproduce
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 benull
:Configuration (please complete the following information): Please provide more information on your .NET configuration:
coverlet.msbuild v6.0.2
Additional context
Using
timeFromService.Value
rather thantimeFromService
can be used as a workaround.