4

Can you tell me how to disable the compiler/codemodel warning for just one block of switch/case?

In general, I think it is very useful to be warned, but here it complains about 167 enumeration values not explicitly handled in the switch.

I found this other question:

c++ warning: enumeration value not handled in switch [-Wswitch]

It says that you can get rid of the warning with default: break; but in this case (recent QtCreator with clang) this does not apply.

I know I could change the code to if/else if/else if .. but I expect the list of handled cases will grow with time, so I'd prefer to stay with switch/case.

So, my question is, is there any keyword/macro/comment/attribute that says ignore the issue for just this single block?

The following code produces the warning, the other 167 values seem to be the possible return values of QEvent::type(), which are part of Qt:

bool MyClass::event(QEvent * e) {
    switch(e->type()) {
    case QEvent::HoverEnter:
        qDebug() << "enter"; 
        return true;
    case QEvent::HoverLeave:
        qDebug() << "leave"; 
        return true;
    case QEvent::HoverMove:
        qDebug() << "move"; 
        return true;
    default:
        break;
    }
    return Piece::event(e);
}
user2567875
  • 440
  • 3
  • 19

2 Answers2

2

As pointed out in comment by user463035818 the message can be disabled for specific part of code by adding #pragma:

bool MyClass::event(QEvent * e) {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wswitch"
    switch(e->type()) {
    case QEvent::HoverEnter:
        qDebug() << "enter"; 
        return true;
    case QEvent::HoverLeave:
        qDebug() << "leave"; 
        return true;
    case QEvent::HoverMove:
        qDebug() << "move"; 
        return true;
    default:
        break;
    }
    #pragma clang diagnostic pop
    return Piece::event(e);
}
user2567875
  • 440
  • 3
  • 19
2

In addition to the accepted answer posted by user2567875, if you are using the GCC build-tool., you can get the same like this:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
switch(...)
{
...
}
#pragma GCC diagnostic pop

Notice that GCC must be capital.

Mohammed Noureldin
  • 12,127
  • 14
  • 68
  • 86