-Wswitch-bool?

All,

If I have source file like this:

/**********************************/

#include <stdbool.h>
#include <stdio.h>
int main(int argc, char* argv)
{
switch(argc == 2)
{
case true:
puts(“argc == 2”);
break;
case false:
puts(“argc != 2”);
break;
}
return 0;
}

/**********************************/

When I compile this source file with -Wall -Werror flags I get an error:
“main.c:5:3: error: switch condition has boolean value [-Werror]”

And that’s fine. I guess it even makes sense. After all, why on earth wouldn’t I just use an if-else statement? I appreciate all responses to this email, but if you do respond PLEASE pretend I don’t have much of a choice. My question is, how do turn this specific warning off? I’ve looked online and saw some post mentioning that this warning is named “switch-bool” but clang doesn’t seem to know that. It suggests “switch-enum” but that doesn’t help…

  • Max

You're probably using an old clang. There is an option "-Wno-switch-bool" that turns this warning off, but if your clang does not understand it, there may be no other way of disabling it. Alternatively, you could try -w, but that would turn off all warnings.

If you can change the source, you could make it "switch ((int)(argc == 2))".

-Krzysztof

Krzysztof,

Thanks for the response.

You’re probably using an old clang.

You’re right about that. Sadly, I’m stuck with it.

If you can change the source, you could make it “switch ((int)(argc == 2))”.

I think that will have to do. Thank you!

-Max