Description
When having both -Wswitch-default
and -Wcovered-switch-default
this generates contradictory warnings. When you don't have a default
case for a switch
statement that handles every enum
value you get a warning for not having it; and if you do you get a warning for having it. However you require both those flags if you wish to get related warnings elsewhere when dealing with switch
statements not related to enum
s or enum
s but not all the values.
#include <cstdint>
enum class ETest : std::uint8_t
{
TEST1,
TEST2,
};
auto main() -> int
{
ETest test = ETest::TEST2;
[[maybe_unused]]
int a {};
switch (test)
{
case ETest::TEST1: a = 1; break;
case ETest::TEST2: a = 2; break;
// default: a = 99; break;
}
return 0;
}
What I would expect is that -Wswitch-default
is suppressed when dealing with enum
s and every case
is covered, but active otherwise.
The way it is now, you will miss out on at least one of those three possible cases that require a warning:
- A
switch
statement that is not handling anenum
doesn't have adefault
case (which is bad) - A
switch
statement that is dealing with anenum
is fully defined but has adefault
case (which is bad) - A
switch
statement that is dealing with anenum
is not fully defined and doesn't have adefault
case (which is bad)
This was not the case in earlier versions of clang, which handled this correctly.
My version is
Ubuntu clang version 19.0.0 (++20240222031214+307409a8872f-1~exp1~20240222151237.1514)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin