Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
Tracked by #4442
PaulRBerg opened this issue Feb 9, 2023 · 3 comments
Closed
2 tasks done
Tracked by #4442

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

PaulRBerg opened this issue Feb 9, 2023 · 3 comments
Labels
C-forge Command: forge Cmd-forge-coverage Command: forge coverage T-bug Type: bug

Comments

@PaulRBerg
Copy link
Contributor

PaulRBerg commented Feb 9, 2023

Component

Forge

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

  • Foundry
  • 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 PaulRBerg added the T-bug Type: bug label Feb 9, 2023
@PaulRBerg PaulRBerg changed the title bug(forge): empty if/else statements appear as not covered bug(forge): empty if/else statements are not covered Feb 9, 2023
@zerosnacks zerosnacks added C-forge Command: forge Cmd-forge-coverage Command: forge coverage labels Jun 28, 2024
@zerosnacks
Copy link
Member

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) |

@grandizzy
Copy link
Collaborator

grandizzy commented Jul 16, 2024

@PaulRBerg we added several improvements for if/else coverage (see #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.

@PaulRBerg
Copy link
Contributor Author

Makes sense!

@PaulRBerg PaulRBerg closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-coverage Command: forge coverage T-bug Type: bug
Projects
Status: Completed
Development

No branches or pull requests

3 participants