xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_161.c (revision 6937eff333b197fb14840f0e58df0f7a0edfd51a)
1 /*	$NetBSD: msg_161.c,v 1.12 2024/11/13 04:32:49 rillig Exp $	*/
2 # 3 "msg_161.c"
3 
4 // Test for message: constant in conditional context [161]
5 
6 /* lint1-extra-flags: -h -X 351 */
7 
8 void
9 while_1(void)
10 {
11 	/* expect+1: warning: constant in conditional context [161] */
12 	while (1)
13 		continue;
14 }
15 
16 void
17 while_0(void)
18 {
19 	/* expect+1: warning: constant in conditional context [161] */
20 	while (0) {
21 		/* expect+1: warning: 'continue' statement not reached [193] */
22 		continue;
23 	}
24 }
25 
26 /*
27  * The pattern 'do { } while (0)' is a common technique to define a
28  * preprocessor macro that behaves like a single statement.  There is
29  * nothing unusual or surprising about the constant condition.
30  * Before tree.c 1.202 from 2021-01-31, lint warned about it.
31  */
32 void
33 do_while_0(void)
34 {
35 	do {
36 
37 	} while (0);
38 }
39 
40 void
41 do_while_1(void)
42 {
43 	do {
44 		/* expect+1: warning: constant in conditional context [161] */
45 	} while (1);
46 }
47 
48 extern void println(const char *);
49 
50 /*
51  * Since 2021-02-28, lint no longer warns about constant controlling
52  * expressions involving sizeof since these are completely legitimate.
53  */
54 void
55 test_sizeof(void)
56 {
57 	if (sizeof(int) > sizeof(char))
58 		println("very probable");
59 	if (sizeof(int) < sizeof(char))
60 		println("impossible");
61 }
62 
63 const _Bool conditions[] = {
64 	/* XXX: Why no warning here? */
65 	13 < 13,
66 	/* XXX: Why no warning here? */
67 	0 < 0,
68 	/* XXX: Why no warning here? */
69 	0 != 0,
70 	/* expect+1: warning: constant in conditional context [161] */
71 	0 == 0 && 1 == 0,
72 	/* expect+1: warning: constant in conditional context [161] */
73 	1 == 0 || 2 == 1,
74 	/* expect+2: warning: constant in conditional context [161] */
75 	/* expect+1: error: non-constant initializer [177] */
76 	0 == 0 && ""[0] == '\0',
77 	/* expect+2: warning: constant in conditional context [161] */
78 	/* expect+1: error: non-constant initializer [177] */
79 	""[0] == '\0' && 0 == 0,
80 	/* C99 6.6p3: Constant expressions shall not contain [...] comma */
81 	/* expect+2: warning: expression has null effect [129] */
82 	/* expect+1: error: non-constant initializer [177] */
83 	(0 == 0, 1 == 0),
84 };
85