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

Able to reproduce the bug, now yields

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

zerosnacks avatar Jun 28 '24 15:06 zerosnacks

@PaulRBerg we added several improvements for if/else coverage (see https://github.com/foundry-rs/foundry/pull/8414) , however this is an edge case that would add complexity in code (and not fully justified IMO). Besides that, having coverage reporting such leftovers should be of benefit for code quality (forcing devs to remove such useless code). We discussed to close this as won't implement and ack the limitation, wdyt? Thank you.

grandizzy avatar Jul 16 '24 18:07 grandizzy

Makes sense!

PaulRBerg avatar Jul 22 '24 12:07 PaulRBerg