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