Module Name: src Committed By: rillig Date: Tue Mar 9 23:40:43 UTC 2021
Modified Files: src/tests/usr.bin/xlint/lint1: msg_130.c msg_130.exp Log Message: tests/lint: add example for anonymous enum type in switch expression To generate a diff of this commit: cvs rdiff -u -r1.9 -r1.10 src/tests/usr.bin/xlint/lint1/msg_130.c cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/msg_130.exp Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/xlint/lint1/msg_130.c diff -u src/tests/usr.bin/xlint/lint1/msg_130.c:1.9 src/tests/usr.bin/xlint/lint1/msg_130.c:1.10 --- src/tests/usr.bin/xlint/lint1/msg_130.c:1.9 Tue Mar 9 23:09:48 2021 +++ src/tests/usr.bin/xlint/lint1/msg_130.c Tue Mar 9 23:40:43 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: msg_130.c,v 1.9 2021/03/09 23:09:48 rillig Exp $ */ +/* $NetBSD: msg_130.c,v 1.10 2021/03/09 23:40:43 rillig Exp $ */ # 3 "msg_130.c" // Test for message: enum type mismatch: '%s' '%s' '%s' [130] @@ -90,3 +90,39 @@ enum_constant_from_unnamed_type(int x) return 0; } + +/* + * A typical legitimate use case for an anonymous enum type that should not + * be mixed with other types is a state machine. + * + * This example demonstrates that the type of the 'switch' expression can be + * an anonymous enum. + */ +void +state_machine(const char *str) +{ + enum { + begin, + seen_letter, + seen_letter_digit, + error + } state = begin; + + for (const char *p = str; *p != '\0'; p++) { + switch (state) { + case begin: + state = *p == 'A' ? seen_letter : error; + break; + case seen_letter: + state = *p == '1' ? seen_letter_digit : error; + break; + default: + state = error; + } + } + + if (state == 2) /* might be worth a warning */ + return; + if (state == sizeof_int) /* expect: 130 */ + return; +} Index: src/tests/usr.bin/xlint/lint1/msg_130.exp diff -u src/tests/usr.bin/xlint/lint1/msg_130.exp:1.7 src/tests/usr.bin/xlint/lint1/msg_130.exp:1.8 --- src/tests/usr.bin/xlint/lint1/msg_130.exp:1.7 Tue Mar 9 23:09:48 2021 +++ src/tests/usr.bin/xlint/lint1/msg_130.exp Tue Mar 9 23:40:43 2021 @@ -7,3 +7,4 @@ msg_130.c(49): warning: enum type mismat msg_130.c(75): warning: enum type mismatch: 'int' '==' 'enum <unnamed>' [130] msg_130.c(77): warning: enum type mismatch: 'int' '==' 'enum <unnamed>' [130] msg_130.c(88): warning: enum type mismatch: 'enum <unnamed>' '==' 'enum <unnamed>' [130] +msg_130.c(126): warning: enum type mismatch: 'enum <unnamed>' '==' 'enum <unnamed>' [130]