brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.2 KiB · ffddec8 Raw
65 lines · cpp
1// RUN: %check_clang_tidy %s misc-misplaced-const %t -- -- -DUSING2// RUN: %check_clang_tidy %s misc-misplaced-const %t -- -- -DTYPEDEF3 4#ifdef TYPEDEF5typedef int int_;6typedef int *ptr_to_int;7typedef const int *ptr_to_const_int;8#endif9#ifdef USING10using int_ = int;11using ptr_to_int = int *;12using ptr_to_const_int = const int *;13#endif14 15void const_pointers() {16  if (const int *i = 0) {17    i = 0;18    // *i = 0;19  }20 21  if (const int_ *i = 0) {22    i = 0;23    // *i = 0;24  }25 26  if (const ptr_to_const_int i = 0) {27    // i = 0;28    // *i = 0;29  }30 31  // Potentially quite unexpectedly the int can be modified here32  // CHECK-MESSAGES: :[[@LINE+1]]:24: warning: 'i' declared with a const-qualified {{.*}}; results in the type being 'int *const' instead of 'const int *'33  if (const ptr_to_int i = 0) {34    //i = 0;35 36    *i = 0;37  }38}39 40template <typename Ty>41struct S {42  const Ty *i;43  const Ty &i2;44};45 46template struct S<int>;47template struct S<ptr_to_int>; // ok48template struct S<ptr_to_const_int>;49 50template <typename Ty>51struct U {52  const Ty *i;53  const Ty &i2;54};55 56template struct U<int *>; // ok57 58struct T {59  typedef void (T::*PMF)();60 61  void f() {62    const PMF val = &T::f; // ok63  }64};65