C++ If statement before case in switch
Asked Answered
V

1

8

I am tasked to rewrite some old software for my company and found an interesting construct inside the sources.

switch(X){
    if(Y==Z){
       case A: ... break;
       case B: ... break;
    }
    case C: ... break;
    default: ...;
}

The compiler warns me, that the code inside the if statment will not be executed but while testing it just seems that the if statment is not validated, the case statments however are.

Is there any reason, maybe early C++ days as some of the code is over 20 years by now, why you would write a construct like that? It is in production code so it does seem to do something?

I would be thankfull for any inside why this might have been done and if I can just ignore it as the compiler seems to do.

Edit: It is also occuring multiple times, so it does not seem to be a simple error. This is what is confusing me.

Edit2: Here is the example I used for testing. I am not sure how helpful it is however the origial code is inside a 1200 line monster function so creating one from that is basically impossible.

for (int i=0; i<5;i++)
{
    switch(i)
    {
        if (i==0 || i ==1)
        {
            cout << "reached before case";
            case 0: cout << "inside case 0" << std::endl; break;
            case 1: cout << "inside case 1" << std::endl; break;
            case 2: cout << "inside case 2" << std::endl; break;
        }
        case 3: cout << "inside case 3" << std::endl; break;
        default: cout << "inside default" << std::endl;
    }
}
Vacuole answered 11/4, 2022 at 13:5 Comment(11)
Is it really the first thing in the switch?Rubinrubina
Please include the warning message, and a minimal reproducible example.Totalizer
Yeah it is. I have never seen that before. The IDE gives a -wswitch-unreachable warning at those positions. This is why I looked into it.Vacuole
The if doesn't do anything. It might have done something in the past, for example if there was an earlier case which was later removed. If you are using some sort of version control like git or svn you could try to "blame" the code and find out who wrote it, then ask them what their intention was.Scandalize
Do you have any edit history for these files? I would guess that the if existed before the switch did. But a switch behaves very much like a goto in this regard, even in old versions of C++. This code requires that Y==Z can compile, but the comparison is never made at runtime.Pourparler
There are some weird constructs that you can make with switch, like Duff's device. So it could be an attempt at an optimization. But it's just as likely IMO that it's just an old bug (syntactically valid, but semantically incorrect) that older compilers didn't warn about.Disunity
The "if-statement" that the compiler refers to is just the condition, not the body. That is, the condition is ignored. The code looks a bit like code that used to work but suffered from a bad merge when someone either added or removed a bit of "clever".Totalizer
@FrançoisAndrieux Unfortunalty I only have a folder with the sources. No documentation, no edit history nothing. And the person has left the company years ago so asking is also not an option. But it seems it is just a bug so I do not need to worry about it.Vacuole
you have at least one more thing: You know what the code is supposed to do. Write tests, refactor and get rid of the obscure stuffAstrophotography
Given your code in Edit2, I'm strongly inclined to guess that the author of this code didn't understand and didn't test the code. Since you are already tasked with a rewrite, I would suggest finding ways to pull logic out of this function into smaller, testable functions.Pourparler
@DrewDormann yeah that is anyways the plan. However I have to somehow understand it first. And I did not want to just remove some functionality and spend ages trying to figure out whats wrong. Just wanted to make sure that there is no feature of C++ that I am unaware of but I am now convinced that it is just a bug. Thanks.Vacuole
A
4

Is there any reason ... why you would write a construct like that?

Only the author knows for sure (even they might not know). If there is source versioning metadata available, then associated commit message might be useful. Without more information, we can only guess what they were thinking. Some potential answers:

  • The author assumed that the condition of the if-statement would have some effect, but they were wrong and the mistake wasn't tested.
  • It's a vestigial result of some refactoring.
    • Perhaps some code was removed that used to make the statement meaningful.
    • Or perhaps it was copied from elsewhere where it did have a meaning.
    • Or perhaps there used to be a series of if-statements and there was an intention to replace them with a switch, but the change was half-assed.
Alvaalvan answered 11/4, 2022 at 13:44 Comment(1)
No commit history, no documentation, just a folder with all the sources. So I guess I will never know why it was done and I highly doubt this code was ever tested. I think it was just put into the customers systems and tested there. But if there is, and importantly was, no use in this construct I can stop looking for a deeper meaning and just accept that it is a bug. Thanks.Vacuole

© 2022 - 2024 — McMap. All rights reserved.