1// RUN: %check_clang_tidy %s misc-misplaced-const %t -- -- -DUSING
2// RUN: %check_clang_tidy %s misc-misplaced-const %t -- -- -DTYPEDEF
3
4#ifdef TYPEDEF
5typedef int int_;
6typedef int *ptr_to_int;
7typedef const int *ptr_to_const_int;
8#endif
9#ifdef USING
10using int_ = int;
11using ptr_to_int = int *;
12using ptr_to_const_int = const int *;
13#endif
14
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 here
32 // 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>; // ok
48template 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 *>; // ok
57
58struct T {
59 typedef void (T::*PMF)();
60
61 void f() {
62 const PMF val = &T::f; // ok
63 }
64};
65

source code of clang-tools-extra/test/clang-tidy/checkers/misc/misplaced-const.cpp