summaryrefslogtreecommitdiff
path: root/tests/usr.bin/xlint/lint1/msg_161.c
blob: c33538c78bd92290b1fada3dcaa3e9f7cf538016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*	$NetBSD: msg_161.c,v 1.11 2023/06/19 12:33:43 rillig Exp $	*/
# 3 "msg_161.c"

// Test for message: constant in conditional context [161]

/* lint1-extra-flags: -h -X 351 */

void
while_1(void)
{
	/* expect+1: warning: constant in conditional context [161] */
	while (1)
		continue;
}

void
while_0(void)
{
	/* expect+1: warning: constant in conditional context [161] */
	while (0) {
		/* expect+1: warning: statement not reached [193] */
		continue;
	}
}

/*
 * The pattern 'do { } while (0)' is a common technique to define a
 * preprocessor macro that behaves like a single statement.  There is
 * nothing unusual or surprising about the constant condition.
 * Before tree.c 1.202 from 2021-01-31, lint warned about it.
 */
void
do_while_0(void)
{
	do {

	} while (0);
}

void
do_while_1(void)
{
	do {
		/* expect+1: warning: constant in conditional context [161] */
	} while (1);
}

extern void println(const char *);

/*
 * Since 2021-02-28, lint no longer warns about constant controlling
 * expressions involving sizeof since these are completely legitimate.
 */
void
test_sizeof(void)
{
	if (sizeof(int) > sizeof(char))
		println("very probable");
	if (sizeof(int) < sizeof(char))
		println("impossible");
}

const _Bool conditions[] = {
	/* XXX: Why no warning here? */
	13 < 13,
	/* XXX: Why no warning here? */
	0 < 0,
	/* XXX: Why no warning here? */
	0 != 0,
	/* expect+1: warning: constant in conditional context [161] */
	0 == 0 && 1 == 0,
	/* expect+1: warning: constant in conditional context [161] */
	1 == 0 || 2 == 1,
	/* expect+2: warning: constant in conditional context [161] */
	/* expect+1: error: non-constant initializer [177] */
	0 == 0 && ""[0] == '\0',
	/* expect+2: warning: constant in conditional context [161] */
	/* expect+1: error: non-constant initializer [177] */
	""[0] == '\0' && 0 == 0,
	/* C99 6.6p3: Constant expressions shall not contain [...] comma */
	/* expect+2: warning: expression has null effect [129] */
	/* expect+1: error: non-constant initializer [177] */
	(0 == 0, 1 == 0),
};