Thread overview
[Issue 16224] -cov marks the last line of do/while(0); as uncovered
Jul 01, 2016
Johan Engelen
Dec 17, 2022
Iain Buclaw
July 01, 2016
https://issues.dlang.org/show_bug.cgi?id=16224

hsteoh@quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh@quickfur.ath.cx

--- Comment #1 from hsteoh@quickfur.ath.cx ---
So what's the problem here?

--
July 01, 2016
https://issues.dlang.org/show_bug.cgi?id=16224

--- Comment #2 from hsteoh@quickfur.ath.cx ---
Nevermind, I get it now. Sorry for the noise.

--
July 01, 2016
https://issues.dlang.org/show_bug.cgi?id=16224

--- Comment #3 from Andrei Alexandrescu <andrei@erdani.com> ---
For clarity, I'll paste the code (fixed to do something useful, i.e. compute the cube of an int) and the listing:

int cube(int x)
{
    do
    {
        if (x == 0)
            break;
        if (x == 1)
            break;
        if (x == -1)
            break;
        return x * x * x;
    }
    while (0);
    return x;
}

unittest
{
    cube(0);
    cube(1);
    cube(-1);
    cube(2);
}

=================================

       |int cube(int x)
       |{
       |    do
       |    {
      4|        if (x == 0)
      1|            break;
      3|        if (x == 1)
      1|            break;
      2|        if (x == -1)
      1|            break;
      1|        return x * x * x;
       |    }
0000000|    while (0);
      3|    return x;
       |}
       |
       |unittest
       |{
      1|    cube(0);
      1|    cube(1);
      1|    cube(-1);
      1|    cube(2);
       |}

--
July 01, 2016
https://issues.dlang.org/show_bug.cgi?id=16224

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com

--- Comment #4 from Steven Schveighoffer <schveiguy@yahoo.com> ---
I don't see why while(true) is any worse.

Essentially:

while(true)
{
...
break;
}

Is the same as the do...while(0)

--
July 01, 2016
https://issues.dlang.org/show_bug.cgi?id=16224

--- Comment #5 from Andrei Alexandrescu <andrei@erdani.com> ---
(In reply to Steven Schveighoffer from comment #4)
> I don't see why while(true) is any worse.
> 
> Essentially:
> 
> while(true)
> {
> ...
> break;
> }
> 
> Is the same as the do...while(0)

Yah, switch is also fine:

switch (0)
{
default:
  ....
}

It just seems to me do/while(0) is the clearest, most obvious, and the least
bug-prone.

--
July 01, 2016
https://issues.dlang.org/show_bug.cgi?id=16224

--- Comment #6 from Johan Engelen <jbc.engelen@gmail.com> ---
>  The coverage analyzer marks the line with "while (0);" as uncovered, although that is an useless tidbit.

I don't think it is useless. In the OP example, while(0) is uncovered because
there is never a "fallthrough exit" of that scope. Consider something like this
where coverage of while(0) is useful.

int fun(int x)
{
    do
    {
        if (x != 0)
            break;
        if (x != 1)
            break;
        if (x != -1)
            break;
        mightThrow(x);
    }
    while (0);
    return x * x * x;
}

--
July 01, 2016
https://issues.dlang.org/show_bug.cgi?id=16224

--- Comment #7 from Andrei Alexandrescu <andrei@erdani.com> ---
(In reply to Johan Engelen from comment #6)
> >  The coverage analyzer marks the line with "while (0);" as uncovered, although that is an useless tidbit.
> 
> I don't think it is useless. In the OP example, while(0) is uncovered
> because there is never a "fallthrough exit" of that scope. Consider
> something like this where coverage of while(0) is useful.
> 
> int fun(int x)
> {
>     do
>     {
>         if (x != 0)
>             break;
>         if (x != 1)
>             break;
>         if (x != -1)
>             break;
>         mightThrow(x);
>     }
>     while (0);
>     return x * x * x;
> }

In this case I agree coverage should be as usual. The presence of "return" changes things.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=16224

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--
December 13
https://issues.dlang.org/show_bug.cgi?id=16224

--- Comment #8 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17764

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--