For the following code, I hope there is a warning because a
is type enum A
, but the case B1
and B2
are type enum B
; but I couldn’t find a way to make gcc/clang warn about it.
Any suggestion on how to detect the potential bugs like this?
Thanks
<code>enum A { A1, A2 };
enum B { B1, B2 };
int foo(enum A a) {
switch(a) {
case B1:
return 1;
case B2:
return 2;
default:
return 3;
}
}
</code>
<code>enum A { A1, A2 };
enum B { B1, B2 };
int foo(enum A a) {
switch(a) {
case B1:
return 1;
case B2:
return 2;
default:
return 3;
}
}
</code>
enum A { A1, A2 };
enum B { B1, B2 };
int foo(enum A a) {
switch(a) {
case B1:
return 1;
case B2:
return 2;
default:
return 3;
}
}
<code>$ clang -Wall -Wextra -Wpedantic -Werror -c enum3.c; echo $?
0
$ gcc -Wall -Wextra -Wpedantic -Werror -c enum3.c; echo $?
0
$ clang --version
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</code>
<code>$ clang -Wall -Wextra -Wpedantic -Werror -c enum3.c; echo $?
0
$ gcc -Wall -Wextra -Wpedantic -Werror -c enum3.c; echo $?
0
$ clang --version
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</code>
$ clang -Wall -Wextra -Wpedantic -Werror -c enum3.c; echo $?
0
$ gcc -Wall -Wextra -Wpedantic -Werror -c enum3.c; echo $?
0
$ clang --version
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.