1
+ // Not a definition, only a declaration:
2
+ extern int g1 ; // COMPLIANT
3
+
4
+ // Both declared + defined:
5
+ extern int g2 ; // COMPLIANT
6
+ int g2 = 1 ; // NON_COMPLIANT
7
+
8
+ // Definition is only declaration:
9
+ int g3 = 1 ; // NON_COMPLIANT
10
+
11
+ // Definition, but value is required for program to compile:
12
+ int g4 = 1 ; // COMPLIANT
13
+ void f1 () { g4 ; }
14
+
15
+ // Local variables:
16
+ void f2 () {
17
+ int l1 ; // COMPLIANT
18
+ l1 ;
19
+
20
+ int l2 ; // NON-COMPLIANT
21
+
22
+ // Value is required for the program to compile:
23
+ int l3 ; // COMPLIANT
24
+ sizeof (l3 );
25
+
26
+ int l4 , // COMPLIANT
27
+ l5 ; // NON-COMPLIANT
28
+ l4 ;
29
+ }
30
+
31
+ // Struct fields are not objects:
32
+ struct s {
33
+ int x ; // COMPLIANT
34
+ };
35
+
36
+ // Declaration of type struct is an object:
37
+ struct s g5 ; // NON-COMPLIANT
38
+
39
+ // Struct fields are not objects:
40
+ union u {
41
+ int x ; // COMPLIANT
42
+ };
43
+
44
+ // Declaration of type union is an object:
45
+ union u g6 ; // NON-COMPLIANT
46
+
47
+ // Typedefs are not objects:
48
+ typedef int td1 ; // COMPLIANT
49
+
50
+ // Declaration of typedef type object:
51
+ td1 g7 ; // NON-COMPLIANT
52
+
53
+ // Function parameters are not objects:
54
+ void f3 (int p ) {} // COMPLIANT
55
+
56
+ // Function type parameters are not objects:
57
+ typedef int td2 (int x ); // COMPLIANT
58
+
59
+ // Macros that define unused vars tests:
60
+ #define ONLY_DEF_VAR (x ) int x = 0;
61
+ void f4 () {
62
+ ONLY_DEF_VAR (l1 ); // COMPLIANT
63
+ l1 ;
64
+ ONLY_DEF_VAR (l2 ); // NON-COMPLIANT
65
+ }
66
+
67
+ // NON-COMPLIANT
68
+ #define ALSO_DEF_VAR (x ) \
69
+ int x = 0; \
70
+ while (1) \
71
+ ;
72
+ void f5 () {
73
+ ALSO_DEF_VAR (l1 ); // COMPLIANT
74
+ ALSO_DEF_VAR (l2 ); // COMPLIANT
75
+ }
76
+
77
+ #define DEF_UNUSED_INNER_VAR () \
78
+ { \
79
+ int _v = 0; \
80
+ while (1) \
81
+ ; \
82
+ } // NON-COMPLIANT
83
+ void f6 () {
84
+ DEF_UNUSED_INNER_VAR (); // COMPLIANT
85
+ }
86
+
87
+ __attribute__((unused )) int g8 = 1 ; // NON-COMPLIANT
88
+ #define ONLY_DEF_ATTR_UNUSED_VAR (x ) __attribute__((unused)) int x = 0;
89
+ void f7 () {
90
+ ONLY_DEF_ATTR_UNUSED_VAR (l1 ); // COMPLIANT
91
+ l1 ;
92
+ ONLY_DEF_ATTR_UNUSED_VAR (l2 ); // NON-COMPLIANT
93
+ }
94
+
95
+ // NON-COMPLIANT
96
+ #define ALSO_DEF_ATTR_UNUSED_VAR (x ) \
97
+ __attribute__((unused)) int x = 0; \
98
+ while (1) \
99
+ ;
100
+ void f8 () {
101
+ ALSO_DEF_ATTR_UNUSED_VAR (l1 ); // COMPLIANT
102
+ ALSO_DEF_ATTR_UNUSED_VAR (l2 ); // COMPLIANT
103
+ }
104
+
105
+ // NON-COMPLIANT
106
+ #define DEF_ATTR_UNUSED_INNER_VAR () \
107
+ { \
108
+ __attribute__((unused)) int _v = 0; \
109
+ while (1) \
110
+ ; \
111
+ }
112
+
113
+ void f9 () {
114
+ DEF_ATTR_UNUSED_INNER_VAR (); // COMPLIANT
115
+ }
116
+
117
+ // Const variable tests:
118
+ const int g9 = 1 ; // COMPLIANT
119
+ const int g10 = 1 ; // NON-COMPLIANT
120
+
121
+ void f10 () {
122
+ g9 ;
123
+ const int l1 = 1 ; // COMPLIANT
124
+ const int l2 = 1 ; // NON-COMPLIANT
125
+ l1 ;
126
+ }
127
+
128
+ // Side effects should not disable this rule:
129
+ void f11 () {
130
+ int l1 = 1 ; // COMPLIANT
131
+ int l2 = l1 ++ ; // COMPLIANT
132
+ l2 ;
133
+ }
0 commit comments