xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_323.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: msg_323.c,v 1.4 2021/10/10 09:17:24 rillig Exp $	*/
2 # 3 "msg_323.c"
3 
4 // Test for message: continue in 'do ... while (0)' loop [323]
5 
6 void println(const char *);
7 
8 /*
9  * In simple cases of a do-while-0 loop, the statements 'break' and
10  * 'continue' have the same effect, and 'break' is much more common.
11  *
12  * This is also covered by Clang-Tidy.
13  */
14 void
15 simple_case(const char *p)
16 {
17 	do {
18 		if (p[0] == '+')
19 			break;
20 		if (p[1] == '-')
21 			continue;
22 		println("no sign");
23 	/* expect+1: error: continue in 'do ... while (0)' loop [323] */
24 	} while (0);
25 }
26 
27 /*
28  * If there is a 'switch' statement inside the do-while-0 loop, the 'break'
29  * statement is tied to the 'switch' statement instead of the loop.
30  */
31 void
32 nested_switch(const char *p)
33 {
34 	do {
35 		switch (*p) {
36 		case 'a':
37 			continue;	/* leaves the 'do while 0' */
38 		case 'b':
39 			break;		/* leaves the 'switch' */
40 		}
41 		println("b");
42 	/* XXX: Is that really worth an error? */
43 	/* expect+1: error: continue in 'do ... while (0)' loop [323] */
44 	} while (0);
45 }
46 
47 /*
48  * In a nested loop, the 'continue' statement is bound to the inner loop,
49  * thus no warning.
50  */
51 void
52 nested_for(void)
53 {
54 	do {
55 		for (int i = 0; i < 6; i++) {
56 			if (i < 3)
57 				continue;
58 		}
59 	} while (0);
60 }
61