• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %check_clang_tidy %s readability-isolate-declaration %t -- -- -fexceptions
2 
f()3 void f() {
4   int i;
5 }
6 
f2()7 void f2() {
8   int i, j, *k, lala = 42;
9   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
10   // CHECK-FIXES: int i;
11   // CHECK-FIXES: {{^  }}int j;
12   // CHECK-FIXES: {{^  }}int *k;
13   // CHECK-FIXES: {{^  }}int lala = 42;
14 
15   int normal, weird = /* comment */ 42;
16   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
17   // CHECK-FIXES: int normal;
18   // CHECK-FIXES: {{^  }}int weird = /* comment */ 42;
19 
20   int /* here is a comment */ v1,
21       // another comment
22       v2 = 42 // Ok, more comments
23       ;
24   // CHECK-MESSAGES: [[@LINE-4]]:3: warning: multiple declarations in a single statement reduces readability
25   // CHECK-FIXES: int /* here is a comment */ v1;
26   // CHECK-FIXES: {{^  }}int /* here is a comment */ // another comment
27   // CHECK-FIXES: {{^      }}v2 = 42 // Ok, more comments
28   // CHECK-FIXES: {{^      }};
29 
30   auto int1 = 42, int2 = 0, int3 = 43;
31   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
32   // CHECK-FIXES: auto int1 = 42;
33   // CHECK-FIXES: {{^  }}auto int2 = 0;
34   // CHECK-FIXES: {{^  }}auto int3 = 43;
35 
36   decltype(auto) ptr1 = &int1, ptr2 = &int1;
37   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
38   // CHECK-FIXES: decltype(auto) ptr1 = &int1;
39   // CHECK-FIXES: {{^  }}decltype(auto) ptr2 = &int1;
40 
41   decltype(k) ptr3 = &int1, ptr4 = &int1;
42   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
43   // CHECK-FIXES: decltype(k) ptr3 = &int1;
44   // CHECK-FIXES: {{^  }}decltype(k) ptr4 = &int1;
45 }
46 
f3()47 void f3() {
48   int i, *pointer1;
49   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
50   // CHECK-FIXES: int i;
51   // CHECK-FIXES: {{^  }}int *pointer1;
52   //
53   int *pointer2 = nullptr, *pointer3 = &i;
54   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
55   // CHECK-FIXES: int *pointer2 = nullptr;
56   // CHECK-FIXES: {{^  }}int *pointer3 = &i;
57 
58   int *(i_ptr) = nullptr, *((i_ptr2));
59   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
60   // CHECK-FIXES: int *(i_ptr) = nullptr;
61   // CHECK-FIXES: {{^  }}int *((i_ptr2));
62 
63   float(*f_ptr)[42], (((f_value))) = 42;
64   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
65   // CHECK-FIXES: float (*f_ptr)[42];
66   // CHECK-FIXES: {{^  }}float (((f_value))) = 42;
67 
68   float(((*f_ptr2)))[42], ((*f_ptr3)), f_value2 = 42.f;
69   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
70   // CHECK-FIXES: float (((*f_ptr2)))[42];
71   // CHECK-FIXES: {{^  }}float ((*f_ptr3));
72   // CHECK-FIXES: {{^  }}float f_value2 = 42.f;
73 
74   float(((*f_ptr4)))[42], *f_ptr5, ((f_value3));
75   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
76   // CHECK-FIXES: float (((*f_ptr4)))[42];
77   // CHECK-FIXES: {{^  }}float *f_ptr5;
78   // CHECK-FIXES: {{^  }}float ((f_value3));
79 
80   void(((*f2))(int)), (*g2)(int, float);
81   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
82   // CHECK-FIXES: void (((*f2))(int));
83   // CHECK-FIXES: {{^  }}void (*g2)(int, float);
84 
85   float(*(*(*f_ptr6)))[42], (*f_ptr7);
86   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
87   // CHECK-FIXES: float (*(*(*f_ptr6)))[42];
88   // CHECK-FIXES: {{^  }}float (*f_ptr7);
89 }
90 
f4()91 void f4() {
92   double d = 42. /* foo */, z = 43., /* hi */ y, c /* */ /*  */, l = 2.;
93   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
94   // CHECK-FIXES: double d = 42. /* foo */;
95   // CHECK-FIXES: {{^  }}double z = 43.;
96   // CHECK-FIXES: {{^  }}double /* hi */ y;
97   // CHECK-FIXES: {{^  }}double c /* */ /*  */;
98   // CHECK-FIXES: {{^  }}double l = 2.;
99 }
100 
101 struct SomeClass {
102   SomeClass() = default;
103   SomeClass(int value);
104 };
105 
106 class Point {
107   double x;
108   double y;
109 
110 public:
Point(double x,double y)111   Point(double x, double y) : x(x), y(y) {}
112 };
113 
114 class Rectangle {
115   Point TopLeft;
116   Point BottomRight;
117 
118 public:
Rectangle(Point TopLeft,Point BottomRight)119   Rectangle(Point TopLeft, Point BottomRight) : TopLeft(TopLeft), BottomRight(BottomRight) {}
120 };
121 
f5()122 void f5() {
123   SomeClass v1, v2(42), v3{42}, v4(42.5);
124   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
125   // CHECK-FIXES: SomeClass v1;
126   // CHECK-FIXES: {{^  }}SomeClass v2(42);
127   // CHECK-FIXES: {{^  }}SomeClass v3{42};
128   // CHECK-FIXES: {{^  }}SomeClass v4(42.5);
129 
130   SomeClass v5 = 42, *p1 = nullptr;
131   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
132   // CHECK-FIXES: SomeClass v5 = 42;
133   // CHECK-FIXES: {{^  }}SomeClass *p1 = nullptr;
134 
135   Point P1(0., 2.), P2{2., 0.};
136   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
137   // CHECK-FIXES: Point P1(0., 2.);
138   // CHECK-FIXES: {{^  }}Point P2{2., 0.};
139 
140   Rectangle R1({0., 0.}, {1., -2.}), R2{{0., 1.}, {1., 0.}}, R3(P1, P2), R4{P1, P2};
141   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
142   // CHECK-FIXES: Rectangle R1({0., 0.}, {1., -2.});
143   // CHECK-FIXES: {{^  }}Rectangle R2{{[{][{]}}0., 1.}, {1., 0.{{[}][}]}};
144   // CHECK-FIXES: {{^  }}Rectangle R3(P1, P2);
145   // CHECK-FIXES: {{^  }}Rectangle R4{P1, P2};
146 }
147 
f6()148 void f6() {
149   int array1[] = {1, 2, 3, 4}, array2[] = {1, 2, 3}, value1, value2 = 42;
150   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
151   // CHECK-FIXES: int array1[] = {1, 2, 3, 4};
152   // CHECK-FIXES: {{^  }}int array2[] = {1, 2, 3};
153   // CHECK-FIXES: {{^  }}int value1;
154   // CHECK-FIXES: {{^  }}int value2 = 42;
155 }
156 
157 template <typename T>
158 struct TemplatedType {
159   TemplatedType() = default;
160   TemplatedType(T value);
161 };
162 
f7()163 void f7() {
164   TemplatedType<int> TT1(42), TT2{42}, TT3;
165   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
166   // CHECK-FIXES: TemplatedType<int> TT1(42);
167   // CHECK-FIXES: {{^  }}TemplatedType<int> TT2{42};
168   // CHECK-FIXES: {{^  }}TemplatedType<int> TT3;
169   //
170   TemplatedType<int *> *TT4(nullptr), TT5, **TT6 = nullptr, *const *const TT7{nullptr};
171   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
172   // CHECK-FIXES: TemplatedType<int *> *TT4(nullptr);
173   // CHECK-FIXES: {{^  }}TemplatedType<int *> TT5;
174   // CHECK-FIXES: {{^  }}TemplatedType<int *> **TT6 = nullptr;
175   // CHECK-FIXES: {{^  }}TemplatedType<int *> *const *const TT7{nullptr};
176 
177   TemplatedType<int &> **TT8(nullptr), *TT9;
178   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
179   // CHECK-FIXES: TemplatedType<int &> **TT8(nullptr);
180   // CHECK-FIXES: {{^  }}TemplatedType<int &> *TT9;
181 
182   TemplatedType<int *> TT10{nullptr}, *TT11(nullptr);
183   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
184   // CHECK-FIXES: TemplatedType<int *> TT10{nullptr};
185   // CHECK-FIXES: {{^  }}TemplatedType<int *> *TT11(nullptr);
186 }
187 
forbidden_transformations()188 void forbidden_transformations() {
189   for (int i = 0, j = 42; i < j; ++i)
190     ;
191 }
192 
193 #define NULL 0
194 #define MY_NICE_TYPE int **
195 #define VAR_NAME(name) name##__LINE__
196 #define A_BUNCH_OF_VARIABLES int m1 = 42, m2 = 43, m3 = 44;
197 
macros()198 void macros() {
199   int *p1 = NULL, *p2 = NULL;
200   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
201   // CHECK-FIXES: int *p1 = NULL;
202   // CHECK-FIXES: {{^  }}int *p2 = NULL;
203 
204   // Macros are involved, so there will be no transformation
205   MY_NICE_TYPE p3, v1, v2;
206   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
207 
208   int VAR_NAME(v3),
209       VAR_NAME(v4),
210       VAR_NAME(v5);
211   // CHECK-MESSAGES: [[@LINE-3]]:3: warning: multiple declarations in a single statement reduces readability
212 
213   A_BUNCH_OF_VARIABLES
214   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
215 
216   int Unconditional,
217   // Explanatory comment.
218 #if CONFIGURATION
219       IfConfigured = 42,
220 #else
221       IfConfigured = 0;
222 #endif
223   // CHECK-MESSAGES: [[@LINE-7]]:3: warning: multiple declarations in a single statement reduces readability
224 }
225 
dontTouchParameter(int param1,int param2)226 void dontTouchParameter(int param1, int param2) {}
227 
228 struct StructOne {
StructOneStructOne229   StructOne() {}
StructOneStructOne230   StructOne(int b) {}
231 
232   int member1, member2;
233   // TODO: Handle FieldDecl's as well
234 };
235 
236 using PointerType = int;
237 
238 struct {
239   int i;
240 } AS1, AS2;
241 struct TemT {
242   template <typename T>
getAsTemT243   T *getAs() {
244     return nullptr;
245   }
246 } TT1, TT2;
247 
complex_typedefs()248 void complex_typedefs() {
249   typedef int *IntPtr;
250   typedef int ArrayType[2];
251   typedef int FunType(void);
252 
253   IntPtr intptr1, intptr2 = nullptr, intptr3;
254   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
255   // CHECK-FIXES: IntPtr intptr1;
256   // CHECK-FIXES: {{^  }}IntPtr intptr2 = nullptr;
257   // CHECK-FIXES: {{^  }}IntPtr intptr3;
258 
259   IntPtr *DoublePtr1 = nullptr, **TriplePtr, SinglePtr = nullptr;
260   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
261   // CHECK-FIXES: IntPtr *DoublePtr1 = nullptr;
262   // CHECK-FIXES: {{^  }}IntPtr **TriplePtr;
263   // CHECK-FIXES: {{^  }}IntPtr SinglePtr = nullptr;
264 
265   IntPtr intptr_array1[2], intptr_array2[4] = {nullptr, nullptr, nullptr, nullptr};
266   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
267   // CHECK-FIXES: IntPtr intptr_array1[2];
268   // CHECK-FIXES: {{^  }}IntPtr intptr_array2[4] = {nullptr, nullptr, nullptr, nullptr};
269 
270   ArrayType arraytype1, arraytype2 = {1}, arraytype3;
271   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
272   // CHECK-FIXES: ArrayType arraytype1;
273   // CHECK-FIXES: {{^  }}ArrayType arraytype2 = {1};
274   // CHECK-FIXES: {{^  }}ArrayType arraytype3;
275 
276   // Don't touch function declarations.
277   FunType funtype1, funtype2, functype3;
278 
279   for (int index1 = 0, index2 = 0;;) {
280     int localFor1 = 1, localFor2 = 2;
281     // CHECK-MESSAGES: [[@LINE-1]]:5: warning: multiple declarations in a single statement reduces readability
282     // CHECK-FIXES: int localFor1 = 1;
283     // CHECK-FIXES: {{^    }}int localFor2 = 2;
284   }
285 
286   StructOne s1, s2(23), s3, s4(3), *sptr = new StructOne(2);
287   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
288   // CHECK-FIXES: StructOne s1;
289   // CHECK-FIXES: {{^  }}StructOne s2(23);
290   // CHECK-FIXES: {{^  }}StructOne s3;
291   // CHECK-FIXES: {{^  }}StructOne s4(3);
292   // CHECK-FIXES: {{^  }}StructOne *sptr = new StructOne(2);
293 
294   struct StructOne cs1, cs2(42);
295   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
296   // CHECK-FIXES: struct StructOne cs1;
297   // CHECK-FIXES: {{^  }}struct StructOne cs2(42);
298 
299   int *ptrArray[3], dummy, **ptrArray2[5], twoDim[2][3], *twoDimPtr[2][3];
300   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
301   // CHECK-FIXES: int *ptrArray[3];
302   // CHECK-FIXES: {{^  }}int dummy;
303   // CHECK-FIXES: {{^  }}int **ptrArray2[5];
304   // CHECK-FIXES: {{^  }}int twoDim[2][3];
305   // CHECK-FIXES: {{^  }}int *twoDimPtr[2][3];
306 
307   {
308     void f1(int), g1(int, float);
309   }
310 
311   {
312     void gg(int, float);
313 
314     void (*f2)(int), (*g2)(int, float) = gg;
315     // CHECK-MESSAGES: [[@LINE-1]]:5: warning: multiple declarations in a single statement reduces readability
316     // CHECK-FIXES: void (*f2)(int);
317     // CHECK-FIXES: {{^    }}void (*g2)(int, float) = gg;
318 
319     void /*(*/ (/*(*/ *f3)(int), (*g3)(int, float);
320     // CHECK-MESSAGES: [[@LINE-1]]:5: warning: multiple declarations in a single statement reduces readability
321     // CHECK-FIXES: void /*(*/ (/*(*/ *f3)(int);
322     // CHECK-FIXES: {{^    }}void /*(*/ (*g3)(int, float);
323   }
324 
325   // clang-format off
326   auto returner = []() { return int(32); };
327   int intfunction = returner(), intarray[] =
328           {
329                   1,
330                   2,
331                   3,
332                   4
333           }, bb = 4;
334   // CHECK-MESSAGES: [[@LINE-7]]:3: warning: multiple declarations in a single statement reduces readability
335   // CHECK-FIXES: int intfunction = returner();
336   // CHECK-FIXES: {{^  }}int intarray[] =
337   // CHECK-FIXES: {{^          }}{
338   // CHECK-FIXES: {{^                  }}1,
339   // CHECK-FIXES: {{^                  }}2,
340   // CHECK-FIXES: {{^                  }}3,
341   // CHECK-FIXES: {{^                  }}4
342   // CHECK-FIXES: {{^          }}};
343   // CHECK-FIXES: {{^  }}int bb = 4;
344   // clang-format on
345 
346   TemT *T1 = &TT1, *T2 = &TT2;
347   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
348   // CHECK-FIXES: TemT *T1 = &TT1;
349   // CHECK-FIXES: {{^  }}TemT *T2 = &TT2;
350 
351   const PointerType *PT1 = T1->getAs<PointerType>(),
352                     *PT2 = T2->getAs<PointerType>();
353   // CHECK-MESSAGES: [[@LINE-2]]:3: warning: multiple declarations in a single statement reduces readability
354   // CHECK-FIXES: const PointerType *PT1 = T1->getAs<PointerType>();
355   // CHECK-FIXES: {{^  }}const PointerType *PT2 = T2->getAs<PointerType>();
356 
357   const int *p1 = nullptr;
358   const int *p2 = nullptr;
359 
360   const int *&pref1 = p1, *&pref2 = p2;
361   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
362   // CHECK-FIXES: const int *&pref1 = p1;
363   // CHECK-FIXES: {{^  }}const int *&pref2 = p2;
364 
365   // clang-format off
366   const char *literal1 = "clang"   "test"\
367                          "one",
368              *literal2 = "empty", literal3[] = "three";
369   // CHECK-MESSAGES: [[@LINE-3]]:3: warning: multiple declarations in a single statement reduces readability
370   // CHECK-FIXES: const char *literal1 = "clang"   "test"\
371   // CHECK-FIXES: {{^                         }}"one";
372   // CHECK-FIXES: {{^  }}const char *literal2 = "empty";
373   // CHECK-FIXES: {{^  }}const char literal3[] = "three";
374   // clang-format on
375 }
376 
g()377 void g() try {
378   int i, j;
379   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
380   // CHECK-FIXES: int i;
381   // CHECK-FIXES: {{^  }}int j;
382 } catch (...) {
383 }
384 
385 struct S {
386   int a;
387   const int b;
fS388   void f() {}
389 };
390 
memberPointers()391 void memberPointers() {
392   typedef const int S::*MemPtr;
393   MemPtr aaa = &S::a, bbb = &S::b;
394   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
395   // CHECK-FIXES: MemPtr aaa = &S::a;
396   // CHECK-FIXES: {{^  }}MemPtr bbb = &S::b;
397 }
398 
399 typedef int *tptr, tbt;
400 typedef int (&tfp)(int, long), tarr[10];
401 typedef int tarr2[10], tct;
402 
403 template <typename A, typename B>
404 void should_not_be_touched(A, B);
405 
406 int variable, function(void);
407 
408 int call_func_with_sideeffect();
bad_if_decl()409 void bad_if_decl() {
410   if (true)
411     int i, j, k = call_func_with_sideeffect();
412 }
413