xref: /llvm-project/clang/test/Sema/warn-unused-but-set-variables.c (revision 2af845a6519c9cde5c8f58db5554f8b1084ce1ed)
1 // RUN: %clang_cc1 -fblocks -fsyntax-only -Wunused-but-set-variable -verify %s
2 
3 struct S {
4   int i;
5 };
6 
f0(void)7 int f0(void) {
8   int y; // expected-warning{{variable 'y' set but not used}}
9   y = 0;
10 
11   int z __attribute__((unused));
12   z = 0;
13 
14   struct S s; // expected-warning{{variable 's' set but not used}}
15   struct S t;
16   s = t;
17 
18   // Don't warn for an extern variable.
19   extern int w;
20   w = 0;
21 
22   // Following gcc, this should not warn.
23   int a;
24   w = (a = 0);
25 
26   int j = 0; // expected-warning{{variable 'j' set but not used}}
27   for (int i = 0; i < 1000; i++)
28     j += 1;
29 
30   // Following gcc, warn for a volatile variable.
31   volatile int b; // expected-warning{{variable 'b' set but not used}}
32   b = 0;
33 
34   // volatile variable k is used, no warning.
35   volatile int k = 0;
36   for (int i = 0; i < 1000; i++)
37     k += 1;
38 
39   // typedef of volatile type, no warning.
40   typedef volatile int volint;
41   volint l = 0;
42   l += 1;
43 
44   int x;
45   x = 0;
46   return x;
47 }
48 
f1(void)49 void f1(void) {
50   (void)^() {
51     int y; // expected-warning{{variable 'y' set but not used}}
52     y = 0;
53 
54     int x;
55     x = 0;
56     return x;
57   };
58 }
59 
f2(void)60 void f2 (void) {
61   // Don't warn, even if it's only used in a non-ODR context.
62   int x;
63   x = 0;
64   (void) sizeof(x);
65 }
66 
for_cleanup(int * x)67 void for_cleanup(int *x) {
68   *x = 0;
69 }
70 
f3(void)71 void f3(void) {
72   // Don't warn if the __cleanup__ attribute is used.
73   __attribute__((__cleanup__(for_cleanup))) int x;
74   x = 5;
75 }
76 
f4(void)77 void f4(void) {
78   int x1 = 0; // expected-warning{{variable 'x1' set but not used}}
79   x1++;
80   int x2 = 0; // expected-warning{{variable 'x2' set but not used}}
81   x2--;
82   int x3 = 0; // expected-warning{{variable 'x3' set but not used}}
83   ++x3;
84   int x4 = 0; // expected-warning{{variable 'x4' set but not used}}
85   --x4;
86 
87   static int counter = 0; // expected-warning{{variable 'counter' set but not used}}
88   counter += 1;
89 
90   volatile int v1 = 0;
91   ++v1;
92   typedef volatile int volint;
93   volint v2 = 0;
94   v2++;
95 }
96