warn-unused-lambda-capture.cpp
8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// RUN: %clang_cc1 -fsyntax-only -Wunused-lambda-capture -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++1z %s
class NonTrivialConstructor {
public:
NonTrivialConstructor() {}
};
class NonTrivialCopyConstructor {
public:
NonTrivialCopyConstructor() = default;
NonTrivialCopyConstructor(const NonTrivialCopyConstructor &) {}
};
class NonTrivialDestructor {
public:
~NonTrivialDestructor() {}
};
class Trivial {
public:
Trivial() = default;
Trivial(int a) {}
};
int side_effect() {
return 42;
}
void test() {
int i = 0;
const int k = 0;
auto captures_nothing = [] {};
auto captures_nothing_by_value = [=] {};
auto captures_nothing_by_reference = [&] {};
auto implicit_by_value = [=]() mutable { i++; };
auto implicit_by_reference = [&] { i++; };
auto explicit_by_value_used = [i] { return i + 1; };
auto explicit_by_value_used_void = [i] { (void)i; };
auto explicit_by_value_unused = [i] {}; // expected-warning{{lambda capture 'i' is not used}}
auto explicit_by_value_unused_sizeof = [i] { return sizeof(i); }; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
auto explicit_by_value_unused_decltype = [i] { decltype(i) j = 0; }; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
auto explicit_by_value_unused_const = [k] { return k + 1; }; // expected-warning{{lambda capture 'k' is not required to be captured for this use}}
auto explicit_by_reference_used = [&i] { i++; };
auto explicit_by_reference_unused = [&i] {}; // expected-warning{{lambda capture 'i' is not used}}
auto explicit_initialized_reference_used = [&j = i] { return j + 1; };
auto explicit_initialized_reference_unused = [&j = i]{}; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_used = [j = 1] { return j + 1; };
auto explicit_initialized_value_unused = [j = 1] {}; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_non_trivial_constructor = [j = NonTrivialConstructor()]{};
auto explicit_initialized_value_non_trivial_destructor = [j = NonTrivialDestructor()]{};
auto explicit_initialized_value_trivial_init = [j = Trivial()]{}; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_non_trivial_init = [j = Trivial(42)]{};
auto explicit_initialized_value_with_side_effect = [j = side_effect()]{};
auto nested = [&i] {
auto explicit_by_value_used = [i] { return i + 1; };
auto explicit_by_value_unused = [i] {}; // expected-warning{{lambda capture 'i' is not used}}
};
Trivial trivial;
auto explicit_by_value_trivial = [trivial] {}; // expected-warning{{lambda capture 'trivial' is not used}}
NonTrivialConstructor cons;
auto explicit_by_value_non_trivial_constructor = [cons] {}; // expected-warning{{lambda capture 'cons' is not used}}
NonTrivialCopyConstructor copy_cons;
auto explicit_by_value_non_trivial_copy_constructor = [copy_cons] {};
NonTrivialDestructor dest;
auto explicit_by_value_non_trivial_destructor = [dest] {};
volatile int v;
auto explicit_by_value_volatile = [v] {};
}
class TrivialThis : Trivial {
void test() {
auto explicit_this_used = [this] { return i; };
auto explicit_this_used_void = [this] { (void)this; };
auto explicit_this_unused = [this] {}; // expected-warning{{lambda capture 'this' is not used}}
auto explicit_star_this_used = [*this] { return i; };
auto explicit_star_this_used_void = [*this] { (void)this; };
auto explicit_star_this_unused = [*this] {}; // expected-warning{{lambda capture 'this' is not used}}
}
int i;
};
class NonTrivialConstructorThis : NonTrivialConstructor {
void test() {
auto explicit_this_used = [this] { return i; };
auto explicit_this_used_void = [this] { (void)this; };
auto explicit_this_unused = [this] {}; // expected-warning{{lambda capture 'this' is not used}}
auto explicit_star_this_used = [*this] { return i; };
auto explicit_star_this_used_void = [*this] { (void)this; };
auto explicit_star_this_unused = [*this] {}; // expected-warning{{lambda capture 'this' is not used}}
}
int i;
};
class NonTrivialCopyConstructorThis : NonTrivialCopyConstructor {
void test() {
auto explicit_this_used = [this] { return i; };
auto explicit_this_used_void = [this] { (void)this; };
auto explicit_this_unused = [this] {}; // expected-warning{{lambda capture 'this' is not used}}
auto explicit_star_this_used = [*this] { return i; };
auto explicit_star_this_used_void = [*this] { (void)this; };
auto explicit_star_this_unused = [*this] {};
}
int i;
};
class NonTrivialDestructorThis : NonTrivialDestructor {
void test() {
auto explicit_this_used = [this] { return i; };
auto explicit_this_used_void = [this] { (void)this; };
auto explicit_this_unused = [this] {}; // expected-warning{{lambda capture 'this' is not used}}
auto explicit_star_this_used = [*this] { return i; };
auto explicit_star_this_used_void = [*this] { (void)this; };
auto explicit_star_this_unused = [*this] {};
}
int i;
};
template <typename T>
void test_templated() {
int i = 0;
const int k = 0;
auto captures_nothing = [] {};
auto captures_nothing_by_value = [=] {};
auto captures_nothing_by_reference = [&] {};
auto implicit_by_value = [=]() mutable { i++; };
auto implicit_by_reference = [&] { i++; };
auto explicit_by_value_used = [i] { return i + 1; };
auto explicit_by_value_used_generic = [i](auto c) { return i + 1; };
auto explicit_by_value_used_void = [i] { (void)i; };
auto explicit_by_value_unused = [i] {}; // expected-warning{{lambda capture 'i' is not used}}
auto explicit_by_value_unused_sizeof = [i] { return sizeof(i); }; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
auto explicit_by_value_unused_decltype = [i] { decltype(i) j = 0; }; // expected-warning{{lambda capture 'i' is not used}}
auto explicit_by_value_unused_const = [k] { return k + 1; }; // expected-warning{{lambda capture 'k' is not required to be captured for this use}}
auto explicit_by_value_unused_const_generic = [k](auto c) { return k + 1; }; // expected-warning{{lambda capture 'k' is not required to be captured for this use}}
auto explicit_by_reference_used = [&i] { i++; };
auto explicit_by_reference_unused = [&i] {}; // expected-warning{{lambda capture 'i' is not used}}
auto explicit_initialized_reference_used = [&j = i] { return j + 1; };
auto explicit_initialized_reference_unused = [&j = i]{}; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_used = [j = 1] { return j + 1; };
auto explicit_initialized_value_unused = [j = 1] {}; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_non_trivial_constructor = [j = NonTrivialConstructor()]{};
auto explicit_initialized_value_non_trivial_destructor = [j = NonTrivialDestructor()]{};
auto explicit_initialized_value_trivial_init = [j = Trivial()]{}; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_non_trivial_init = [j = Trivial(42)]{};
auto explicit_initialized_value_with_side_effect = [j = side_effect()]{};
auto explicit_initialized_value_generic_used = [i = 1](auto c) mutable { i++; };
auto explicit_initialized_value_generic_unused = [i = 1](auto c) mutable {}; // expected-warning{{lambda capture 'i' is not used}}
auto nested = [&i] {
auto explicit_by_value_used = [i] { return i + 1; };
auto explicit_by_value_unused = [i] {}; // expected-warning{{lambda capture 'i' is not used}}
};
Trivial trivial;
auto explicit_by_value_trivial = [trivial] {}; // expected-warning{{lambda capture 'trivial' is not used}}
NonTrivialConstructor cons;
auto explicit_by_value_non_trivial_constructor = [cons] {}; // expected-warning{{lambda capture 'cons' is not used}}
NonTrivialCopyConstructor copy_cons;
auto explicit_by_value_non_trivial_copy_constructor = [copy_cons] {};
NonTrivialDestructor dest;
auto explicit_by_value_non_trivial_destructor = [dest] {};
volatile int v;
auto explicit_by_value_volatile = [v] {};
}
void test_use_template() {
test_templated<int>(); // expected-note{{in instantiation of function template specialization 'test_templated<int>' requested here}}
}
namespace pr35555 {
int a;
void b() {
int c[a];
auto vla_used = [&c] { return c[0]; };
auto vla_unused = [&c] {}; // expected-warning{{lambda capture 'c' is not used}}
}
} // namespace pr35555