Aaron Ballman | cf6cefd | 2016-06-07 17:22:47 +0000 | [diff] [blame^] | 1 | // RUN: %check_clang_tidy %s misc-misplaced-const %t |
| 2 | |
| 3 | typedef int plain_i; |
| 4 | typedef int *ip; |
| 5 | typedef const int *cip; |
| 6 | |
| 7 | typedef void (*func_ptr)(void); |
| 8 | |
| 9 | void func(void) { |
| 10 | // ok |
| 11 | const int *i0 = 0; |
| 12 | const plain_i *i1 = 0; |
| 13 | const cip i2 = 0; // const applies to both pointer and pointee. |
| 14 | |
| 15 | // Not ok |
| 16 | const ip i3 = 0; |
| 17 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: 'i3' declared with a const-qualified typedef type; results in the type being 'int *const' instead of 'const int *' |
| 18 | |
| 19 | ip const i4 = 0; |
| 20 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: 'i4' declared with a const-qualified |
| 21 | |
| 22 | const volatile ip i5 = 0; |
| 23 | // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 'i5' declared with a const-qualified typedef type; results in the type being 'int *const volatile' instead of 'const int *volatile' |
| 24 | } |
| 25 | |
| 26 | void func2(const plain_i *i1, |
| 27 | const cip i2, |
| 28 | const ip i3, |
| 29 | // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 'i3' declared with a const-qualified |
| 30 | const int *i4) { |
| 31 | } |
| 32 | |
| 33 | struct S { |
| 34 | const int *i0; |
| 35 | const plain_i *i1; |
| 36 | const cip i2; |
| 37 | const ip i3; |
| 38 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: 'i3' declared with a const-qualified |
| 39 | }; |
| 40 | |
| 41 | // Function pointers should not be diagnosed because a function |
| 42 | // pointer type can never be const. |
| 43 | void func3(const func_ptr fp) { |
| 44 | const func_ptr fp2 = fp; |
| 45 | } |