Fri Mar 5 16:31:49 2021 UTC ()
tests/lint: add test for incompatible enums in switch

Neither lint nor GCC 10 nor Clang 8 have a warning for an enum type
mismatch in a switch statement.

GCC 10 issues a warning but completely misses the point of the
mismatched enum types.  It only warns because in this test, EVENING has
the numeric value 3, which is out of bounds for enum color, where the
valid range is from 0 to 2.  It says:

> msg_130.c:45:2: warning:
>     case value ‘3’ not in enumerated type ‘enum color’ [-Wswitch]

Clang 8 behaves almost the same, it just doesn't mention the value of
the constant, saying merely 'case value not in enumerated type'.


(rillig)
diff -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_130.c

cvs diff -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_130.c (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/msg_130.c 2021/02/28 01:30:22 1.5
+++ src/tests/usr.bin/xlint/lint1/msg_130.c 2021/03/05 16:31:49 1.6
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: msg_130.c,v 1.5 2021/02/28 01:30:22 rillig Exp $ */ 1/* $NetBSD: msg_130.c,v 1.6 2021/03/05 16:31:49 rillig Exp $ */
2# 3 "msg_130.c" 2# 3 "msg_130.c"
3 3
4// Test for message: enum type mismatch: '%s' '%s' '%s' [130] 4// Test for message: enum type mismatch: '%s' '%s' '%s' [130]
5 5
6enum color { 6enum color {
7 RED = 1 << 0, 7 RED = 1 << 0,
8 GREEN = 1 << 1, 8 GREEN = 1 << 1,
9 BLUE = 1 << 2 9 BLUE = 1 << 2
10}; 10};
11 11
12enum size { 12enum size {
13 SMALL, 13 SMALL,
14 MEDIUM, 14 MEDIUM,
@@ -27,13 +27,26 @@ example(_Bool cond, enum color c, enum s @@ -27,13 +27,26 @@ example(_Bool cond, enum color c, enum s
27 sink(cond ? GREEN : MORNING); /* expect: 130 */ 27 sink(cond ? GREEN : MORNING); /* expect: 130 */
28 28
29 sink(c != s); /* expect: 130 */ 29 sink(c != s); /* expect: 130 */
30 sink(c == s); /* expect: 130 */ 30 sink(c == s); /* expect: 130 */
31 sink((c & MEDIUM) != 0); /* might be useful to warn about */ 31 sink((c & MEDIUM) != 0); /* might be useful to warn about */
32 sink((c | MEDIUM) != 0); /* might be useful to warn about */ 32 sink((c | MEDIUM) != 0); /* might be useful to warn about */
33 33
34 c |= MEDIUM; /* might be useful to warn about */ 34 c |= MEDIUM; /* might be useful to warn about */
35 c &= MEDIUM; /* might be useful to warn about */ 35 c &= MEDIUM; /* might be useful to warn about */
36 36
37 /* The cast to unsigned is required by GCC at WARNS=6. */ 37 /* The cast to unsigned is required by GCC at WARNS=6. */
38 c &= ~(unsigned)MEDIUM; /* might be useful to warn about */ 38 c &= ~(unsigned)MEDIUM; /* might be useful to warn about */
39} 39}
 40
 41void
 42switch_example(enum color c)
 43{
 44 switch (c) {
 45 case EVENING: /* TODO: 130 */
 46 case LARGE: /* TODO: 130 */
 47 sink(1 == 1);
 48 break;
 49 default:
 50 break;
 51 }
 52}