reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
// RUN: %check_clang_tidy %s readability-isolate-declaration %t -- -- -fexceptions

void f() {
  int i;
}

void f2() {
  int i, j, *k, lala = 42;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int i;
  // CHECK-FIXES: {{^  }}int j;
  // CHECK-FIXES: {{^  }}int *k;
  // CHECK-FIXES: {{^  }}int lala = 42;

  int normal, weird = /* comment */ 42;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int normal;
  // CHECK-FIXES: {{^  }}int weird = /* comment */ 42;

  int /* here is a comment */ v1,
      // another comment
      v2 = 42 // Ok, more comments
      ;
  // CHECK-MESSAGES: [[@LINE-4]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int /* here is a comment */ v1;
  // CHECK-FIXES: {{^  }}int /* here is a comment */ // another comment
  // CHECK-FIXES: {{^      }}v2 = 42 // Ok, more comments
  // CHECK-FIXES: {{^      }};

  auto int1 = 42, int2 = 0, int3 = 43;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: auto int1 = 42;
  // CHECK-FIXES: {{^  }}auto int2 = 0;
  // CHECK-FIXES: {{^  }}auto int3 = 43;

  decltype(auto) ptr1 = &int1, ptr2 = &int1;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: decltype(auto) ptr1 = &int1;
  // CHECK-FIXES: {{^  }}decltype(auto) ptr2 = &int1;

  decltype(k) ptr3 = &int1, ptr4 = &int1;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: decltype(k) ptr3 = &int1;
  // CHECK-FIXES: {{^  }}decltype(k) ptr4 = &int1;
}

void f3() {
  int i, *pointer1;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int i;
  // CHECK-FIXES: {{^  }}int *pointer1;
  //
  int *pointer2 = nullptr, *pointer3 = &i;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int *pointer2 = nullptr;
  // CHECK-FIXES: {{^  }}int *pointer3 = &i;

  int *(i_ptr) = nullptr, *((i_ptr2));
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int *(i_ptr) = nullptr;
  // CHECK-FIXES: {{^  }}int *((i_ptr2));

  float(*f_ptr)[42], (((f_value))) = 42;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: float (*f_ptr)[42];
  // CHECK-FIXES: {{^  }}float (((f_value))) = 42;

  float(((*f_ptr2)))[42], ((*f_ptr3)), f_value2 = 42.f;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: float (((*f_ptr2)))[42];
  // CHECK-FIXES: {{^  }}float ((*f_ptr3));
  // CHECK-FIXES: {{^  }}float f_value2 = 42.f;

  float(((*f_ptr4)))[42], *f_ptr5, ((f_value3));
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: float (((*f_ptr4)))[42];
  // CHECK-FIXES: {{^  }}float *f_ptr5;
  // CHECK-FIXES: {{^  }}float ((f_value3));

  void(((*f2))(int)), (*g2)(int, float);
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: void (((*f2))(int));
  // CHECK-FIXES: {{^  }}void (*g2)(int, float);

  float(*(*(*f_ptr6)))[42], (*f_ptr7);
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: float (*(*(*f_ptr6)))[42];
  // CHECK-FIXES: {{^  }}float (*f_ptr7);
}

void f4() {
  double d = 42. /* foo */, z = 43., /* hi */ y, c /* */ /*  */, l = 2.;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: double d = 42. /* foo */;
  // CHECK-FIXES: {{^  }}double z = 43.;
  // CHECK-FIXES: {{^  }}double /* hi */ y;
  // CHECK-FIXES: {{^  }}double c /* */ /*  */;
  // CHECK-FIXES: {{^  }}double l = 2.;
}

struct SomeClass {
  SomeClass() = default;
  SomeClass(int value);
};

class Point {
  double x;
  double y;

public:
  Point(double x, double y) : x(x), y(y) {}
};

class Rectangle {
  Point TopLeft;
  Point BottomRight;

public:
  Rectangle(Point TopLeft, Point BottomRight) : TopLeft(TopLeft), BottomRight(BottomRight) {}
};

void f5() {
  SomeClass v1, v2(42), v3{42}, v4(42.5);
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: SomeClass v1;
  // CHECK-FIXES: {{^  }}SomeClass v2(42);
  // CHECK-FIXES: {{^  }}SomeClass v3{42};
  // CHECK-FIXES: {{^  }}SomeClass v4(42.5);

  SomeClass v5 = 42, *p1 = nullptr;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: SomeClass v5 = 42;
  // CHECK-FIXES: {{^  }}SomeClass *p1 = nullptr;

  Point P1(0., 2.), P2{2., 0.};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: Point P1(0., 2.);
  // CHECK-FIXES: {{^  }}Point P2{2., 0.};

  Rectangle R1({0., 0.}, {1., -2.}), R2{{0., 1.}, {1., 0.}}, R3(P1, P2), R4{P1, P2};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: Rectangle R1({0., 0.}, {1., -2.});
  // CHECK-FIXES: {{^  }}Rectangle R2{{[{][{]}}0., 1.}, {1., 0.{{[}][}]}};
  // CHECK-FIXES: {{^  }}Rectangle R3(P1, P2);
  // CHECK-FIXES: {{^  }}Rectangle R4{P1, P2};
}

void f6() {
  int array1[] = {1, 2, 3, 4}, array2[] = {1, 2, 3}, value1, value2 = 42;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int array1[] = {1, 2, 3, 4};
  // CHECK-FIXES: {{^  }}int array2[] = {1, 2, 3};
  // CHECK-FIXES: {{^  }}int value1;
  // CHECK-FIXES: {{^  }}int value2 = 42;
}

template <typename T>
struct TemplatedType {
  TemplatedType() = default;
  TemplatedType(T value);
};

void f7() {
  TemplatedType<int> TT1(42), TT2{42}, TT3;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: TemplatedType<int> TT1(42);
  // CHECK-FIXES: {{^  }}TemplatedType<int> TT2{42};
  // CHECK-FIXES: {{^  }}TemplatedType<int> TT3;
  //
  TemplatedType<int *> *TT4(nullptr), TT5, **TT6 = nullptr, *const *const TT7{nullptr};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: TemplatedType<int *> *TT4(nullptr);
  // CHECK-FIXES: {{^  }}TemplatedType<int *> TT5;
  // CHECK-FIXES: {{^  }}TemplatedType<int *> **TT6 = nullptr;
  // CHECK-FIXES: {{^  }}TemplatedType<int *> *const *const TT7{nullptr};

  TemplatedType<int &> **TT8(nullptr), *TT9;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: TemplatedType<int &> **TT8(nullptr);
  // CHECK-FIXES: {{^  }}TemplatedType<int &> *TT9;

  TemplatedType<int *> TT10{nullptr}, *TT11(nullptr);
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: TemplatedType<int *> TT10{nullptr};
  // CHECK-FIXES: {{^  }}TemplatedType<int *> *TT11(nullptr);
}

void forbidden_transformations() {
  for (int i = 0, j = 42; i < j; ++i)
    ;
}

#define NULL 0
#define MY_NICE_TYPE int **
#define VAR_NAME(name) name##__LINE__
#define A_BUNCH_OF_VARIABLES int m1 = 42, m2 = 43, m3 = 44;

void macros() {
  int *p1 = NULL, *p2 = NULL;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int *p1 = NULL;
  // CHECK-FIXES: {{^  }}int *p2 = NULL;

  // Macros are involved, so there will be no transformation
  MY_NICE_TYPE p3, v1, v2;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability

  int VAR_NAME(v3),
      VAR_NAME(v4),
      VAR_NAME(v5);
  // CHECK-MESSAGES: [[@LINE-3]]:3: warning: multiple declarations in a single statement reduces readability

  A_BUNCH_OF_VARIABLES
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability

  int Unconditional,
  // Explanatory comment.
#if CONFIGURATION
      IfConfigured = 42,
#else
      IfConfigured = 0;
#endif
  // CHECK-MESSAGES: [[@LINE-7]]:3: warning: multiple declarations in a single statement reduces readability
}

void dontTouchParameter(int param1, int param2) {}

struct StructOne {
  StructOne() {}
  StructOne(int b) {}

  int member1, member2;
  // TODO: Handle FieldDecl's as well
};

using PointerType = int;

struct {
  int i;
} AS1, AS2;
struct TemT {
  template <typename T>
  T *getAs() {
    return nullptr;
  }
} TT1, TT2;

void complex_typedefs() {
  typedef int *IntPtr;
  typedef int ArrayType[2];
  typedef int FunType(void);

  IntPtr intptr1, intptr2 = nullptr, intptr3;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: IntPtr intptr1;
  // CHECK-FIXES: {{^  }}IntPtr intptr2 = nullptr;
  // CHECK-FIXES: {{^  }}IntPtr intptr3;

  IntPtr *DoublePtr1 = nullptr, **TriplePtr, SinglePtr = nullptr;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: IntPtr *DoublePtr1 = nullptr;
  // CHECK-FIXES: {{^  }}IntPtr **TriplePtr;
  // CHECK-FIXES: {{^  }}IntPtr SinglePtr = nullptr;

  IntPtr intptr_array1[2], intptr_array2[4] = {nullptr, nullptr, nullptr, nullptr};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: IntPtr intptr_array1[2];
  // CHECK-FIXES: {{^  }}IntPtr intptr_array2[4] = {nullptr, nullptr, nullptr, nullptr};

  ArrayType arraytype1, arraytype2 = {1}, arraytype3;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: ArrayType arraytype1;
  // CHECK-FIXES: {{^  }}ArrayType arraytype2 = {1};
  // CHECK-FIXES: {{^  }}ArrayType arraytype3;

  // Don't touch function declarations.
  FunType funtype1, funtype2, functype3;

  for (int index1 = 0, index2 = 0;;) {
    int localFor1 = 1, localFor2 = 2;
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: multiple declarations in a single statement reduces readability
    // CHECK-FIXES: int localFor1 = 1;
    // CHECK-FIXES: {{^    }}int localFor2 = 2;
  }

  StructOne s1, s2(23), s3, s4(3), *sptr = new StructOne(2);
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: StructOne s1;
  // CHECK-FIXES: {{^  }}StructOne s2(23);
  // CHECK-FIXES: {{^  }}StructOne s3;
  // CHECK-FIXES: {{^  }}StructOne s4(3);
  // CHECK-FIXES: {{^  }}StructOne *sptr = new StructOne(2);

  struct StructOne cs1, cs2(42);
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: struct StructOne cs1;
  // CHECK-FIXES: {{^  }}struct StructOne cs2(42);

  int *ptrArray[3], dummy, **ptrArray2[5], twoDim[2][3], *twoDimPtr[2][3];
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int *ptrArray[3];
  // CHECK-FIXES: {{^  }}int dummy;
  // CHECK-FIXES: {{^  }}int **ptrArray2[5];
  // CHECK-FIXES: {{^  }}int twoDim[2][3];
  // CHECK-FIXES: {{^  }}int *twoDimPtr[2][3];

  {
    void f1(int), g1(int, float);
  }

  {
    void gg(int, float);

    void (*f2)(int), (*g2)(int, float) = gg;
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: multiple declarations in a single statement reduces readability
    // CHECK-FIXES: void (*f2)(int);
    // CHECK-FIXES: {{^    }}void (*g2)(int, float) = gg;

    void /*(*/ (/*(*/ *f3)(int), (*g3)(int, float);
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: multiple declarations in a single statement reduces readability
    // CHECK-FIXES: void /*(*/ (/*(*/ *f3)(int);
    // CHECK-FIXES: {{^    }}void /*(*/ (*g3)(int, float);
  }

  // clang-format off
  auto returner = []() { return int(32); };
  int intfunction = returner(), intarray[] =
          {
                  1,
                  2,
                  3,
                  4
          }, bb = 4;
  // CHECK-MESSAGES: [[@LINE-7]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int intfunction = returner();
  // CHECK-FIXES: {{^  }}int intarray[] =
  // CHECK-FIXES: {{^          }}{
  // CHECK-FIXES: {{^                  }}1,
  // CHECK-FIXES: {{^                  }}2,
  // CHECK-FIXES: {{^                  }}3,
  // CHECK-FIXES: {{^                  }}4
  // CHECK-FIXES: {{^          }}};
  // CHECK-FIXES: {{^  }}int bb = 4;
  // clang-format on

  TemT *T1 = &TT1, *T2 = &TT2;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: TemT *T1 = &TT1;
  // CHECK-FIXES: {{^  }}TemT *T2 = &TT2;

  const PointerType *PT1 = T1->getAs<PointerType>(),
                    *PT2 = T2->getAs<PointerType>();
  // CHECK-MESSAGES: [[@LINE-2]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: const PointerType *PT1 = T1->getAs<PointerType>();
  // CHECK-FIXES: {{^  }}const PointerType *PT2 = T2->getAs<PointerType>();

  const int *p1 = nullptr;
  const int *p2 = nullptr;

  const int *&pref1 = p1, *&pref2 = p2;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: const int *&pref1 = p1;
  // CHECK-FIXES: {{^  }}const int *&pref2 = p2;

  // clang-format off
  const char *literal1 = "clang"   "test"\
                         "one",
             *literal2 = "empty", literal3[] = "three";
  // CHECK-MESSAGES: [[@LINE-3]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: const char *literal1 = "clang"   "test"\
  // CHECK-FIXES: {{^                         }}"one";
  // CHECK-FIXES: {{^  }}const char *literal2 = "empty";
  // CHECK-FIXES: {{^  }}const char literal3[] = "three";
  // clang-format on
}

void g() try {
  int i, j;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: int i;
  // CHECK-FIXES: {{^  }}int j;
} catch (...) {
}

struct S {
  int a;
  const int b;
  void f() {}
};

void memberPointers() {
  typedef const int S::*MemPtr;
  MemPtr aaa = &S::a, bbb = &S::b;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
  // CHECK-FIXES: MemPtr aaa = &S::a;
  // CHECK-FIXES: {{^  }}MemPtr bbb = &S::b;
}

typedef int *tptr, tbt;
typedef int (&tfp)(int, long), tarr[10];
typedef int tarr2[10], tct;

template <typename A, typename B>
void should_not_be_touched(A, B);

int variable, function(void);

int call_func_with_sideeffect();
void bad_if_decl() {
  if (true)
    int i, j, k = call_func_with_sideeffect();
}