cppcoreguidelines-prefer-member-initializer.cpp 11.8 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 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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
// RUN: %check_clang_tidy %s cppcoreguidelines-prefer-member-initializer %t -- -- -fcxx-exceptions

extern void __assert_fail (__const char *__assertion, __const char *__file,
    unsigned int __line, __const char *__function)
     __attribute__ ((__noreturn__));
#define assert(expr) \
  ((expr)  ? (void)(0)  : __assert_fail (#expr, __FILE__, __LINE__, __func__))

class Simple1 {
  int n;
  double x;

public:
  Simple1() {
    // CHECK-FIXES: Simple1() : n(0), x(0.0) {
    n = 0;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
    x = 0.0;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'x' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  Simple1(int nn, double xx) {
    // CHECK-FIXES: Simple1(int nn, double xx) : n(nn), x(xx) {
    n = nn;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
    x = xx;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'x' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  ~Simple1() = default;
};

class Simple2 {
  int n;
  double x;

public:
  Simple2() : n(0) {
    // CHECK-FIXES: Simple2() : n(0), x(0.0) {
    x = 0.0;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'x' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  Simple2(int nn, double xx) : n(nn) {
    // CHECK-FIXES: Simple2(int nn, double xx) : n(nn), x(xx) {
    x = xx;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'x' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  ~Simple2() = default;
};

class Simple3 {
  int n;
  double x;

public:
  Simple3() : x(0.0) {
    // CHECK-FIXES: Simple3() : n(0), x(0.0) {
    n = 0;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  Simple3(int nn, double xx) : x(xx) {
    // CHECK-FIXES: Simple3(int nn, double xx) : n(nn), x(xx) {
    n = nn;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  ~Simple3() = default;
};

int something_int();
double something_double();

class Simple4 {
  int n;

public:
  Simple4() {
    // CHECK-FIXES: Simple4() : n(something_int()) {
    n = something_int();
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  ~Simple4() = default;
};

static bool dice();

class Complex1 {
  int n;
  int m;

public:
  Complex1() : n(0) {
    if (dice())
      m = 1;
    // NO-MESSAGES: initialization of 'm' is nested in a conditional expression
  }

  ~Complex1() = default;
};

class Complex2 {
  int n;
  int m;

public:
  Complex2() : n(0) {
    if (!dice())
      return;
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a conditional expression
  }

  ~Complex2() = default;
};

class Complex3 {
  int n;
  int m;

public:
  Complex3() : n(0) {
    while (dice())
      m = 1;
    // NO-MESSAGES: initialization of 'm' is nested in a conditional loop
  }

  ~Complex3() = default;
};

class Complex4 {
  int n;
  int m;

public:
  Complex4() : n(0) {
    while (!dice())
      return;
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a conditional loop
  }

  ~Complex4() = default;
};

class Complex5 {
  int n;
  int m;

public:
  Complex5() : n(0) {
    do {
      m = 1;
      // NO-MESSAGES: initialization of 'm' is nested in a conditional loop
    } while (dice());
  }

  ~Complex5() = default;
};

class Complex6 {
  int n;
  int m;

public:
  Complex6() : n(0) {
    do {
      return;
    } while (!dice());
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a conditional loop
  }

  ~Complex6() = default;
};

class Complex7 {
  int n;
  int m;

public:
  Complex7() : n(0) {
    for (int i = 2; i < 1; ++i) {
      m = 1;
    }
    // NO-MESSAGES: initialization of 'm' is nested into a conditional loop
  }

  ~Complex7() = default;
};

class Complex8 {
  int n;
  int m;

public:
  Complex8() : n(0) {
    for (int i = 0; i < 2; ++i) {
      return;
    }
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a conditional loop
  }

  ~Complex8() = default;
};

class Complex9 {
  int n;
  int m;

public:
  Complex9() : n(0) {
    switch (dice()) {
    case 1:
      m = 1;
      // NO-MESSAGES: initialization of 'm' is nested in a conditional expression
      break;
    default:
      break;
    }
  }

  ~Complex9() = default;
};

class Complex10 {
  int n;
  int m;

public:
  Complex10() : n(0) {
    switch (dice()) {
    case 1:
      return;
      break;
    default:
      break;
    }
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a conditional expression
  }

  ~Complex10() = default;
};

class E {};
int risky(); // may throw

class Complex11 {
  int n;
  int m;

public:
  Complex11() : n(0) {
    try {
      risky();
      m = 1;
      // NO-MESSAGES: initialization of 'm' follows is nested in a try-block
    } catch (const E& e) {
      return;
    }
  }

  ~Complex11() = default;
};

class Complex12 {
  int n;
  int m;

public:
  Complex12() : n(0) {
    try {
      risky();
    } catch (const E& e) {
      return;
    }
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a try-block
  }

  ~Complex12() = default;
};

class Complex13 {
  int n;
  int m;

public:
  Complex13() : n(0) {
    return;
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a return statement
  }

  ~Complex13() = default;
};

class Complex14 {
  int n;
  int m;

public:
  Complex14() : n(0) {
    goto X;
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a goto statement
  X:
    ;
  }

  ~Complex14() = default;
};

void returning();

class Complex15 {
  int n;
  int m;

public:
  Complex15() : n(0) {
    // CHECK-FIXES: Complex15() : n(0), m(1) {
    returning();
    m = 1;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'm' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  ~Complex15() = default;
};

[[noreturn]] void not_returning();

class Complex16 {
  int n;
  int m;

public:
  Complex16() : n(0) {
    not_returning();
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a non-returning function call
  }

  ~Complex16() = default;
};

class Complex17 {
  int n;
  int m;

public:
  Complex17() : n(0) {
    throw 1;
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows a 'throw' statement;
  }

  ~Complex17() = default;
};

class Complex18 {
  int n;

public:
  Complex18() try {
    n = risky();
    // NO-MESSAGES: initialization of 'n' in a 'try' body;
  } catch (const E& e) {
    n = 0;
  }

  ~Complex18() = default;
};

class Complex19 {
  int n;
public:
  Complex19() {
    // CHECK-FIXES: Complex19() : n(0) {
    n = 0;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  explicit Complex19(int) {
    // CHECK-FIXES: Complex19(int) : n(12) {
    n = 12;
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }

  ~Complex19() = default;
};

class Complex20 {
  int n;
  int m;

public:
  Complex20(int k) : n(0) {
    assert(k > 0);
    m = 1;
    // NO-MESSAGES: initialization of 'm' follows an assertion
  }

  ~Complex20() = default;
};

class VeryComplex1 {
  int n1, n2, n3;
  double x1, x2, x3;
  int n4, n5, n6;
  double x4, x5, x6;

  VeryComplex1() : n3(something_int()), x3(something_double()),
                   n5(something_int()), x4(something_double()),
                   x5(something_double()) {
    // CHECK-FIXES: VeryComplex1() : n2(something_int()), n1(something_int()), n3(something_int()), x2(something_double()), x1(something_double()), x3(something_double()),
    // CHECK-FIXES:                  n4(something_int()), n5(something_int()), n6(something_int()), x4(something_double()),
    // CHECK-FIXES:                  x5(something_double()), x6(something_double()) {

// FIXME: Order of elements on the constructor initializer list should match
//        the order of the declaration of the fields. Thus the correct fixes
//        should look like these:
//
    // C ECK-FIXES: VeryComplex1() : n2(something_int()), n1(something_int()), n3(something_int()), x2(something_double()), x1(something_double()), x3(something_double()),
    // C ECK-FIXES:                  n4(something_int()), n5(something_int()), n6(something_int()), x4(something_double()),
    // C ECK-FIXES:                  x5(something_double()), x6(something_double()) {
//
//        However, the Diagnostics Engine processes fixes in the order of the
//        diagnostics and insertions to the same position are handled in left to
//        right order thus in the case two adjacent fields are initialized
//        inside the constructor in reverse order the provided fix is a
//        constructor initializer list that does not match the order of the
//        declaration of the fields.

    x2 = something_double();
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'x2' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
    n2 = something_int();
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n2' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
    x6 = something_double();
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'x6' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
    x1 = something_double();
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'x1' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
    n6 = something_int();
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n6' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
    n1 = something_int();
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n1' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
    n4 = something_int();
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'n4' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  }
};

struct Outside {
  int n;
  double x;
  Outside();
};

Outside::Outside() {
    // CHECK-FIXES: Outside::Outside() : n(1), x(1.0) {
  n = 1;
    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'n' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
  x = 1.0;
    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'x' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    // CHECK-FIXES: {{^\ *$}}
}