Super User's BSD Cross Reference: /NetBSD/tests/usr.bin/xlint/lint1/msg_161.c

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

AltStyle によって変換されたページ (->オリジナル) /