xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_241.c (revision a194378b9e1779a0079141bfd4343c41708da313)
1 /*	$NetBSD: msg_241.c,v 1.11 2023/07/09 11:01:27 rillig Exp $	*/
2 # 3 "msg_241.c"
3 
4 // Test for message: dubious operation '%s' on enum [241]
5 //
6 // As of February 2021, the option -e is not enabled by default in
7 // share/mk/sys.mk, therefore this message is neither well-known nor
8 // well-tested.
9 
10 /* lint1-extra-flags: -e -X 351 */
11 
12 /*
13  * Enums are a possible implementation of bit-sets.
14  */
15 enum color {
16 	RED	= 1 << 0,
17 	GREEN	= 1 << 1,
18 	BLUE	= 1 << 2
19 };
20 
21 extern void sink_bool(_Bool);
22 extern void sink_int(int);
23 extern void sink_color(enum color);
24 
25 void
example(void)26 example(void)
27 {
28 	enum color c = RED;
29 
30 	/* expect+1: warning: dubious operation '!' on enum [241] */
31 	sink_bool(!c);
32 	/* expect+1: warning: dubious operation '~' on enum [241] */
33 	sink_color(~c);
34 	/* expect+1: warning: dubious operation '++x' on enum [241] */
35 	++c;
36 	/* expect+1: warning: dubious operation '--x' on enum [241] */
37 	--c;
38 	/* expect+1: warning: dubious operation 'x++' on enum [241] */
39 	c++;
40 	/* expect+1: warning: dubious operation 'x--' on enum [241] */
41 	c--;
42 	/* expect+1: warning: dubious operation '+' on enum [241] */
43 	sink_color(+c);
44 	/* expect+1: warning: dubious operation '-' on enum [241] */
45 	sink_color(-c);
46 	/* expect+1: warning: dubious operation '*' on enum [241] */
47 	sink_color(c * c);
48 	/* expect+1: warning: dubious operation '/' on enum [241] */
49 	sink_color(c / c);
50 	/* expect+1: warning: dubious operation '%' on enum [241] */
51 	sink_color(c % c);
52 	/* expect+1: warning: dubious operation '+' on enum [241] */
53 	sink_color(c + c);
54 	/* expect+1: warning: dubious operation '-' on enum [241] */
55 	sink_color(c - c);
56 	/* expect+1: warning: dubious operation '<<' on enum [241] */
57 	sink_color(c << c);
58 	/* expect+1: warning: dubious operation '>>' on enum [241] */
59 	sink_color(c >> c);
60 
61 	sink_bool(c < c);
62 	sink_bool(c <= c);
63 	sink_bool(c > c);
64 	sink_bool(c >= c);
65 	sink_bool(c == c);
66 	sink_bool(c != c);
67 
68 	/* expect+1: warning: dubious operation '&' on enum [241] */
69 	sink_color(c & c);
70 	/* expect+1: warning: dubious operation '^' on enum [241] */
71 	sink_color(c ^ c);
72 	/* expect+1: warning: dubious operation '|' on enum [241] */
73 	sink_color(c | c);
74 
75 	/* expect+1: warning: dubious operation '&&' on enum [241] */
76 	sink_bool(c && c);
77 	/* expect+1: warning: dubious operation '||' on enum [241] */
78 	sink_bool(c || c);
79 	sink_color(c ? c : BLUE);
80 
81 	c = GREEN;
82 	/* expect+1: warning: dubious operation '*=' on enum [241] */
83 	c *= c;
84 	/* expect+1: warning: dubious operation '/=' on enum [241] */
85 	c /= c;
86 	/* expect+1: warning: dubious operation '%=' on enum [241] */
87 	c %= c;
88 	/* expect+1: warning: dubious operation '+=' on enum [241] */
89 	c += c;
90 	/* expect+1: warning: dubious operation '-=' on enum [241] */
91 	c -= c;
92 	/* expect+1: warning: dubious operation '<<=' on enum [241] */
93 	c <<= c;
94 	/* expect+1: warning: dubious operation '>>=' on enum [241] */
95 	c >>= c;
96 	/* expect+1: warning: dubious operation '&=' on enum [241] */
97 	c &= c;
98 	/* expect+1: warning: dubious operation '^=' on enum [241] */
99 	c ^= c;
100 	/* expect+1: warning: dubious operation '|=' on enum [241] */
101 	c |= c;
102 
103 	/* The cast to unsigned is required by GCC at WARNS=6. */
104 	/* expect+1: warning: dubious operation '&=' on enum [241] */
105 	c &= ~(unsigned)GREEN;
106 }
107 
108 void
cover_typeok_enum(enum color c,int i)109 cover_typeok_enum(enum color c, int i)
110 {
111 	/* expect+2: warning: dubious operation '*' on enum [241] */
112 	/* expect+1: warning: combination of 'enum color' and 'int', op '>' [242] */
113 	if (c * i > 5)
114 		return;
115 }
116 
117 const char *
color_name(enum color c)118 color_name(enum color c)
119 {
120 	static const char *name[] = {
121 	    [RED] = "red",
122 	    [GREEN] = "green",
123 	    [BLUE] = "blue",
124 	};
125 
126 	if (c == RED)
127 		return *(c + name); /* unusual but allowed */
128 	if (c == GREEN)
129 		return c[name]; /* even more unusual */
130 	return name[c];
131 }
132