Skip to content

[BUG] False positive branch coverage of if without else #1786

@carlreinke

Description

@carlreinke

Describe the bug
When there is an if without an else and the condition is satisfied then it is possible to have 100% branch coverage without a test that does not satisfy the condition.

To Reproduce

namespace CoverageTest;

public static class Class1
{
    public static int Test1(int n)
    {
        if (n < 10)
            n = Random.Shared.Next();
        return n;
    }
}
using Xunit;

namespace CoverageTest.Tests;

public static class Class1Tests
{
    [Fact]
    public static void Test1()
    {
        Class1.Test1(2);
    }
}

Expected behavior
Branch is 50% covered.

Actual behavior

<Methods>
  <Method cyclomaticComplexity="2" nPathComplexity="2" sequenceCoverage="100" branchCoverage="100" isConstructor="False" isGetter="False" isSetter="False" isStatic="True">
    <Summary numSequencePoints="5" visitedSequencePoints="5" numBranchPoints="2" visitedBranchPoints="2" sequenceCoverage="100" branchCoverage="100" maxCyclomaticComplexity="2" minCyclomaticComplexity="2" visitedClasses="0" numClasses="0" visitedMethods="1" numMethods="1" />
    <MetadataToken />
    <Name>System.Int32 CoverageTest.Class1::Test1(System.Int32)</Name>
    <FileRef uid="1" />
    <SequencePoints>
      <SequencePoint vc="1" uspid="6" ordinal="0" sl="6" sc="1" el="6" ec="2" bec="0" bev="0" fileid="1" />
      <SequencePoint vc="1" uspid="7" ordinal="1" sl="7" sc="1" el="7" ec="2" bec="2" bev="2" fileid="1" />
      <SequencePoint vc="1" uspid="8" ordinal="2" sl="8" sc="1" el="8" ec="2" bec="0" bev="0" fileid="1" />
      <SequencePoint vc="1" uspid="9" ordinal="3" sl="9" sc="1" el="9" ec="2" bec="0" bev="0" fileid="1" />
      <SequencePoint vc="1" uspid="10" ordinal="4" sl="10" sc="1" el="10" ec="2" bec="0" bev="0" fileid="1" />
    </SequencePoints>
    <BranchPoints>
      <BranchPoint vc="1" uspid="7" ordinal="0" path="0" offset="8" offsetend="10" sl="7" fileid="1" />
      <BranchPoint vc="1" uspid="7" ordinal="1" path="1" offset="8" offsetend="22" sl="7" fileid="1" />
    </BranchPoints>
    <MethodPoint vc="5" uspid="0" p8:type="SequencePoint" ordinal="0" offset="0" sc="0" sl="6" ec="1" el="10" bec="0" bev="0" fileid="1" xmlns:p8="xsi" />
  </Method>

Configuration:

  • Which coverlet package and version was used? coverlet.collector 6.0.4 and coverlet.msbuild 6.0.4
  • Which version of .NET is the code running on? .NET 10.0.0
  • What OS and version, and what distro if applicable? Windows 10.0.26100 Build 26100
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? Likely not.

Additional context
There was #1028, but it was closed as stale.

Metadata

Metadata

Assignees

No one assigned

    Labels

    untriagedTo be investigated

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions