msg_241.c revision 1.7
1/*	$NetBSD: msg_241.c,v 1.7 2021/10/30 22:04:42 rillig Exp $	*/
2# 3 "msg_241.c"
3
4// Test for message: dubious operation on enum, op %s [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 */
11
12/*
13 * Enums are a possible implementation of bit-sets.
14 */
15enum color {
16	RED	= 1 << 0,
17	GREEN	= 1 << 1,
18	BLUE	= 1 << 2
19};
20
21extern void sink_bool(_Bool);
22extern void sink_int(int);
23extern void sink_color(enum color);
24
25void
26example(void)
27{
28	enum color c = RED;
29
30	sink_bool(!c);		/* expect: 241 */
31	sink_color(~c);		/* expect: 241 */
32	++c;			/* expect: 241 */
33	--c;			/* expect: 241 */
34	c++;			/* expect: 241 */
35	c--;			/* expect: 241 */
36	sink_color(+c);		/* expect: 241 */
37	sink_color(-c);		/* expect: 241 */
38	sink_color(c * c);	/* expect: 241 */
39	sink_color(c / c);	/* expect: 241 */
40	sink_color(c % c);	/* expect: 241 */
41	sink_color(c + c);	/* expect: 241 */
42	sink_color(c - c);	/* expect: 241 */
43	sink_color(c << c);	/* expect: 241 */
44	sink_color(c >> c);	/* expect: 241 */
45
46	sink_bool(c < c);
47	sink_bool(c <= c);
48	sink_bool(c > c);
49	sink_bool(c >= c);
50	sink_bool(c == c);
51	sink_bool(c != c);
52
53	sink_color(c & c);	/* expect: 241 */
54	sink_color(c ^ c);	/* expect: 241 */
55	sink_color(c | c);	/* expect: 241 */
56
57	sink_bool(c && c);	/* expect: 241 */
58	sink_bool(c || c);	/* expect: 241 */
59	sink_color(c ? c : BLUE);
60
61	c = GREEN;
62	c *= c;			/* expect: 241 */
63	c /= c;			/* expect: 241 */
64	c %= c;			/* expect: 241 */
65	c += c;			/* expect: 241 */
66	c -= c;			/* expect: 241 */
67	c <<= c;		/* expect: 241 */
68	c >>= c;		/* expect: 241 */
69	c &= c;			/* expect: 241 */
70	c ^= c;			/* expect: 241 */
71	c |= c;			/* expect: 241 */
72
73	/* The cast to unsigned is required by GCC at WARNS=6. */
74	c &= ~(unsigned)GREEN;	/* expect: 241 */
75}
76
77void
78cover_typeok_enum(enum color c, int i)
79{
80	/* expect+2: warning: dubious operation on enum, op * [241] */
81	/* expect+1: warning: combination of 'enum color' and 'int', op > [242] */
82	if (c * i > 5)
83		return;
84}
85
86const char *
87color_name(enum color c)
88{
89	static const char *name[] = {
90	    [RED] = "red",
91	    [GREEN] = "green",
92	    [BLUE] = "blue",
93	};
94
95	if (c == RED)
96		return *(c + name); /* unusual but allowed */
97	if (c == GREEN)
98		return c[name]; /* even more unusual */
99	return name[c];
100}
101