msg_129.c revision 1.4 1 /* $NetBSD: msg_129.c,v 1.4 2021/08/21 07:52:07 rillig Exp $ */
2 # 3 "msg_129.c"
3
4 // Test for message: expression has null effect [129]
5
6 /* lint1-extra-flags: -h */
7
8 typedef unsigned char uint8_t;
9 typedef unsigned int uint32_t;
10
11 _Bool side_effect(void);
12
13 /*
14 * Before tree.c 1.198 from 2021-01-30, the nested comma operators were
15 * wrongly reported as having no side effect.
16 *
17 * The bug was that has_side_effect did not properly examine the sub-nodes.
18 * The ',' operator itself has m_has_side_effect == false since it depends
19 * on its operands whether the ',' actually has side effects. For nested ','
20 * operators, the function did not evaluate the operands deeply but only did
21 * a quick shallow test on the m_has_side_effect property. Since that is
22 * false, lint thought that the whole expression would have no side effect.
23 */
24 void
25 uint8_buffer_write_uint32(uint8_t *c, uint32_t l)
26 {
27 (*(c++) = (uint8_t)(l & 0xff),
28 *(c++) = (uint8_t)((l >> 8L) & 0xff),
29 *(c++) = (uint8_t)((l >> 16L) & 0xff),
30 *(c++) = (uint8_t)((l >> 24L) & 0xff));
31 }
32
33 void
34 operator_comma(void)
35 {
36 side_effect(), 0; /* the 0 is redundant */
37 0, side_effect(); /* expect: 129 */
38
39 if (side_effect(), 0) /* the 0 controls the 'if' */
40 return;
41 if (0, side_effect()) /* expect: 129 */
42 return;
43 }
44
45 void
46 legitimate_use_cases(int arg)
47 {
48 int local = 3;
49
50 /*
51 * This expression is commonly used to mark the argument as
52 * deliberately unused.
53 */
54 /* TODO: remove this bogus warning. */
55 /* expect+1: warning: expression has null effect [129] */
56 (void)arg;
57
58 /*
59 * This expression is commonly used to mark the local variable as
60 * deliberately unused. This situation occurs when the local
61 * variable is only used in some but not all compile-time selectable
62 * variants of the code, such as in debugging mode, and writing down
63 * the exact conditions would complicate the code unnecessarily.
64 */
65 /* TODO: remove this bogus warning. */
66 /* expect+1: warning: expression has null effect [129] */
67 (void)local;
68
69 /* This is a short-hand notation for a do-nothing command. */
70 /* TODO: remove this bogus warning. */
71 /* expect+1: warning: expression has null effect [129] */
72 (void)0;
73
74 /*
75 * This variant of the do-nothing command is commonly used in
76 * preprocessor macros since it works nicely with if-else and if-then
77 * statements. It is longer than the above variant though.
78 */
79 do {
80 } while (0);
81
82 /*
83 * Only the expression '(void)0' is common, other expressions are
84 * unusual enough that they warrant a warning.
85 */
86 /* TODO: remove this bogus warning. */
87 /* expect+1: warning: expression has null effect [129] */
88 (void)13;
89 }
90