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
  • 482
  • 4
  • 21
  • 3
    isnt this the advantage of using a `switch` ? when "the list of handled cases will grow with time" you cannot easily miss one because you get that warning. Can you show a [mcve]? would be interesting to see why `default` doesnt help – 463035818_is_not_an_ai Oct 23 '18 at 18:41
  • i guess what makes the difference is the "explicitly". You do handle all cases, but not explicitly – 463035818_is_not_an_ai Oct 23 '18 at 19:01
  • please also add the warning verbatim in the question – 463035818_is_not_an_ai Oct 23 '18 at 19:02
  • @user463035818: can you explain what you mean? I only want to handle three cases for now, later maybe 10 or 20, but in total there are about 170 and most of them I'll never need. – user2567875 Oct 23 '18 at 19:03
  • i think you are confusing `-Wswitch` and `-Wswitch-enum`. The other question is about the first and can be silenced by a `default`, the warning about the "not explicitly handled case" is from the second. I think you just need enable the first and disable the second. – 463035818_is_not_an_ai Oct 23 '18 at 19:05
  • see eg here: https://clang.llvm.org/docs/DiagnosticsReference.html#w-warnings – 463035818_is_not_an_ai Oct 23 '18 at 19:06
  • maybe i confused the two, but how can i disable it only for this one switch, and keep it enabled for all others? – user2567875 Oct 23 '18 at 19:07
  • 1
    [this](https://stackoverflow.com/questions/20078941/how-to-use-pragma-clang-diagnostics) is not really a dupe, but the question has the answer – 463035818_is_not_an_ai Oct 23 '18 at 19:13
  • thanks a lot! works with pragma and "-Wswitch", I'll add it to the question – user2567875 Oct 23 '18 at 19:19
  • 1
    you can post an answer to your own question. I am not familiar with clang and wasnt in the mood to find out to be sure enough to write an answer, but imho your edit should be rather an answer – 463035818_is_not_an_ai Oct 23 '18 at 20:23

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
  • 482
  • 4
  • 21
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
  • 14,913
  • 17
  • 70
  • 99