foundry icon indicating copy to clipboard operation
foundry copied to clipboard

bug(forge): empty if/else statements are not covered

Open PaulRBerg opened this issue 2 years ago • 1 comments

Component

Forge

Have you ensured that all of these are up to date?

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

forge 0.2.0 (249538f 2023-02-08T00:12:05.805004Z)

What command(s) is the bug in?

forge coverage

Operating System

macOS (Apple Silicon)

Describe the bug

Describe the bug

The forge coverage command does not cover empty if/ else statements.

Reproduction Steps

Take the following contract:

contract Foo {
    bool internal flag;

    function coverMe() public view {
        if (flag) {
            // Do nothing
        } else {
            // Do nothing
        }
    }
}


And the following tests:

contract FooTest is Test, Foo {
    function test_CoverMe_FlagFalse() external {
        flag = false;
        coverMe();
    }

    function test_CoverMe_FlagTrue() external {
        flag = true;
        coverMe();
    }
}

Now, run forge coverage. You will get this report:

| File        | % Lines     | % Statements  | % Branches  | % Funcs     |
|-------------|-------------|---------------|-------------|-------------|
| src/Foo.sol | 0.00% (0/1) | 100.00% (0/0) | 0.00% (0/2) | 0.00% (0/1) |
| Total       | 0.00% (0/1) | 100.00% (0/0) | 0.00% (0/2) | 0.00% (0/1) |

Notice that the coverage is 0% instead of 100%, even if the if/else statement is technically covered as part of the execution of the coverMe function.

This bug applies even if the else is removed.

PaulRBerg avatar Feb 09 '23 08:02 PaulRBerg