[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / warn-unused-but-set-variables.c
bloba390944815a9a3c29927526daa61fe053e273948
1 // RUN: %clang_cc1 -fblocks -fsyntax-only -Wunused-but-set-variable -verify %s
3 struct S {
4 int i;
5 };
7 int f0(void) {
8 int y; // expected-warning{{variable 'y' set but not used}}
9 y = 0;
11 int z __attribute__((unused));
12 z = 0;
14 struct S s; // expected-warning{{variable 's' set but not used}}
15 struct S t;
16 s = t;
18 // Don't warn for an extern variable.
19 extern int w;
20 w = 0;
22 // Following gcc, this should not warn.
23 int a;
24 w = (a = 0);
26 int j = 0; // expected-warning{{variable 'j' set but not used}}
27 for (int i = 0; i < 1000; i++)
28 j += 1;
30 // Following gcc, warn for a volatile variable.
31 volatile int b; // expected-warning{{variable 'b' set but not used}}
32 b = 0;
34 // volatile variable k is used, no warning.
35 volatile int k = 0;
36 for (int i = 0; i < 1000; i++)
37 k += 1;
39 // typedef of volatile type, no warning.
40 typedef volatile int volint;
41 volint l = 0;
42 l += 1;
44 int x;
45 x = 0;
46 return x;
49 void f1(void) {
50 (void)^() {
51 int y; // expected-warning{{variable 'y' set but not used}}
52 y = 0;
54 int x;
55 x = 0;
56 return x;
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);
67 void for_cleanup(int *x) {
68 *x = 0;
71 void f3(void) {
72 // Don't warn if the __cleanup__ attribute is used.
73 __attribute__((__cleanup__(for_cleanup))) int x;
74 x = 5;
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;
87 static int counter = 0; // expected-warning{{variable 'counter' set but not used}}
88 counter += 1;
90 volatile int v1 = 0;
91 ++v1;
92 typedef volatile int volint;
93 volint v2 = 0;
94 v2++;