-
Notifications
You must be signed in to change notification settings - Fork 11.8k
/
warn-shadow-in-lambdas.cpp
267 lines (243 loc) · 9.99 KB
/
warn-shadow-in-lambdas.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// RUN: %clang_cc1 -std=c++14 -verify=expected,cxx14 -fsyntax-only -Wshadow -D AVOID %s
// RUN: %clang_cc1 -std=c++14 -verify=expected,cxx14 -fsyntax-only -Wshadow -Wshadow-uncaptured-local %s
// RUN: %clang_cc1 -std=c++14 -verify=expected,cxx14 -fsyntax-only -Wshadow-all %s
// RUN: %clang_cc1 -std=c++17 -verify -fsyntax-only -Wshadow-all %s
// RUN: %clang_cc1 -std=c++20 -verify -fsyntax-only -Wshadow-all %s
void foo(int param) { // expected-note 1+ {{previous declaration is here}}
int var = 0; // expected-note 1+ {{previous declaration is here}}
// Avoid warnings for variables that aren't implicitly captured.
{
#ifdef AVOID
auto f1 = [=] { int var = 1; }; // no warning
auto f2 = [&] { int var = 2; }; // no warning
auto f3 = [=] (int param) { ; }; // no warning
auto f4 = [&] (int param) { ; }; // no warning
auto f5 = [=] { static int var = 1; }; // no warning
auto f6 = [&] { static int var = 2; }; // no warning
#else
auto f1 = [=] { int var = 1; }; // expected-warning {{declaration shadows a local variable}}
auto f2 = [&] { int var = 2; }; // expected-warning {{declaration shadows a local variable}}
auto f3 = [=] (int param) { ; }; // expected-warning {{declaration shadows a local variable}}
auto f4 = [&] (int param) { ; }; // expected-warning {{declaration shadows a local variable}}
auto f5 = [=] { static int var = 1; }; // expected-warning {{declaration shadows a local variable}}
auto f6 = [&] { static int var = 2; }; // expected-warning {{declaration shadows a local variable}}
#endif
}
// Warn for variables that are implicitly captured.
{
auto f1 = [=] () {
{
int var = 1; // expected-warning {{declaration shadows a local variable}}
}
int x = var; // expected-note {{variable 'var' is captured here}}
};
auto f2 = [&]
#ifdef AVOID
(int param) {
#else
(int param) { // expected-warning {{declaration shadows a local variable}}
#endif
int x = var; // expected-note {{variable 'var' is captured here}}
int var = param; // expected-warning {{declaration shadows a local variable}}
};
}
// Warn for variables that are explicitly captured when a lambda has a default
// capture specifier.
{
auto f1 = [=, &var] () { // expected-note {{variable 'var' is captured here}}
int x = param; // expected-note {{variable 'param' is captured here}}
int var = 0; // expected-warning {{declaration shadows a local variable}}
int param = 0; // expected-warning {{declaration shadows a local variable}}
};
}
// Warn normally inside of lambdas.
auto l1 = [] { // expected-note {{previous declaration is here}}
int x = 1; // expected-note {{previous declaration is here}}
{ int x = 2; } // expected-warning {{declaration shadows a local variable}}
};
auto l2 = [] (int x) { // expected-note {{previous declaration is here}}
{ int x = 1; } // expected-warning {{declaration shadows a local variable}}
};
// Avoid warnings for variables that aren't explicitly captured.
{
#ifdef AVOID
auto f1 = [] { int var = 1; }; // no warning
auto f2 = [] (int param) { ; }; // no warning
auto f3 = [param] () { int var = 1; }; // no warning
auto f4 = [var] (int param) { ; }; // no warning
auto f5 = [param] () { static int var = 1; }; // no warning
auto f6 = [] { static int var = 1; }; // no warning
#else
auto f1 = [] { int var = 1; }; // expected-warning {{declaration shadows a local variable}}
auto f2 = [] (int param) { ; }; // expected-warning {{declaration shadows a local variable}}
auto f3 = [param] () { int var = 1; }; // expected-warning {{declaration shadows a local variable}}
auto f4 = [var] (int param) { ; }; // expected-warning {{declaration shadows a local variable}}
auto f5 = [param] () { static int var = 1; }; // expected-warning {{declaration shadows a local variable}}
auto f6 = [] { static int var = 1; }; // expected-warning {{declaration shadows a local variable}}
#endif
};
// Warn for variables that are explicitly captured.
{
auto f1 = [var] () { // expected-note {{variable 'var' is explicitly captured here}}
int var = 1; // expected-warning {{declaration shadows a local variable}}
};
auto f2 = [param] // expected-note {{variable 'param' is explicitly captured here}}
(int param) { ; }; // expected-error {{a lambda parameter cannot shadow an explicitly captured entity}}
}
// Warn for variables defined in the capture list.
auto l3 = [z = var] { // expected-note {{previous declaration is here}}
#ifdef AVOID
int var = 1; // no warning
#else
int var = 1; // expected-warning {{declaration shadows a local variable}}
#endif
{ int z = 1; } // expected-warning {{declaration shadows a local variable}}
};
#ifdef AVOID
auto l4 = [var = param] (int param) { ; }; // no warning
#else
auto l4 = [var = param](int param) { ; }; // expected-warning 2{{declaration shadows a local variable}}
#endif
// Make sure that inner lambdas work as well.
auto l5 = [var, l1] { // expected-note {{variable 'l1' is explicitly captured here}}
auto l1 = [] { // expected-warning {{declaration shadows a local variable}}
#ifdef AVOID
int var = 1; // no warning
#else
int var = 1; // expected-warning {{declaration shadows a local variable}}
#endif
};
#ifdef AVOID
auto f1 = [] { int var = 1; }; // no warning
auto f2 = [=] { int var = 1; }; // no warning
#else
auto f1 = [] { int var = 1; }; // expected-warning {{declaration shadows a local variable}}
auto f2 = [=] { int var = 1; }; // expected-warning {{declaration shadows a local variable}}
#endif
auto f3 = [var] // expected-note {{variable 'var' is explicitly captured here}}
{ int var = 1; }; // expected-warning {{declaration shadows a local variable}}
auto f4 = [&] {
int x = var; // expected-note {{variable 'var' is captured here}}
int var = 2; // expected-warning {{declaration shadows a local variable}}
};
};
auto l6 = [&] {
auto f1 = [param] { // expected-note {{variable 'param' is explicitly captured here}}
int param = 0; // expected-warning {{declaration shadows a local variable}}
};
};
auto l7 = [&] {
auto f1 = [param] { // expected-note {{variable 'param' is explicitly captured here}}
static int param = 0; // expected-warning {{declaration shadows a local variable}}
};
};
// Generic lambda arguments should work.
#ifdef AVOID
auto g1 = [](auto param) { ; }; // no warning
auto g2 = [=](auto param) { ; }; // no warning
#else
auto g1 = [](auto param) { ; }; // expected-warning {{declaration shadows a local variable}}
auto g2 = [=](auto param) { ; }; // expected-warning {{declaration shadows a local variable}}
#endif
auto g3 = [param] // expected-note {{variable 'param' is explicitly captured here}}
(auto param) { ; }; // expected-error {{a lambda parameter cannot shadow an explicitly captured entity}}
}
void avoidWarningWhenRedefining() {
int a = 1;
auto l = [b = a] { // expected-note {{previous definition is here}}
// Don't warn on redefinitions.
int b = 0; // expected-error {{redefinition of 'b'}}
};
}
namespace GH61105 {
void f() {
int y = 0;
int x = 0;
#if __cplusplus >= 202002L
auto l1 = [y]<typename y>(y) { return 0; }; // expected-error {{declaration of 'y' shadows template parameter}} \
// expected-note {{template parameter is declared here}}
auto l2 = [=]<typename y>() { int a = y; return 0; }; // expected-error {{'y' does not refer to a value}} \
// expected-note {{declared here}}
auto l3 = [&, y]<typename y, typename>(y) { int a = x; return 0; }; // expected-error {{declaration of 'y' shadows template parameter}} \
// expected-note {{template parameter is declared here}}
auto l4 = [x, y]<typename y, int x>() { return 0; }; // expected-error {{declaration of 'y' shadows template parameter}} \
// expected-error {{declaration of 'x' shadows template parameter}} \
// expected-note 2{{template parameter is declared here}}
auto l5 = []<typename y>(y) { return 0; }; // No diagnostic
#endif
}
}
namespace GH71976 {
#ifdef AVOID
struct A {
int b = 5;
int foo() {
return [b = b]() { return b; }(); // no -Wshadow diagnostic, init-capture does not shadow b due to not capturing this
}
};
struct B {
int a;
void foo() {
auto b = [a = this->a] {}; // no -Wshadow diagnostic, init-capture does not shadow a due to not capturing his
}
};
struct C {
int b = 5;
int foo() {
return [a = b]() {
return [=, b = a]() { // no -Wshadow diagnostic, init-capture does not shadow b due to outer lambda
return b;
}();
}();
}
};
#else
struct A {
int b = 5; // expected-note {{previous}}
int foo() {
return [b = b]() { return b; }(); // expected-warning {{declaration shadows a field}}
}
};
struct B {
int a; // expected-note {{previous}}
void foo() {
auto b = [a = this->a] {}; // expected-warning {{declaration shadows a field}}
}
};
struct C {
int b = 5; // expected-note {{previous}}
int foo() {
return [a = b]() {
return [=, b = a]() { // expected-warning {{declaration shadows a field}}
return b;
}();
}();
}
};
struct D {
int b = 5; // expected-note {{previous}}
int foo() {
return [b = b, this]() { return b; }(); // expected-warning {{declaration shadows a field}}
}
};
struct E {
int b = 5;
int foo() {
return [a = b]() { // expected-note {{previous}}
return [=, a = a]() { // expected-warning {{shadows a local}}
return a;
}();
}();
}
};
#endif
struct S {
int a ;
};
int foo() {
auto [a] = S{0}; // expected-note {{previous}} \
// cxx14-warning {{decomposition declarations are a C++17 extension}}
[a = a] () { // expected-warning {{declaration shadows a structured binding}}
}();
}
}