76 lines · cpp
1// RUN: %check_clang_tidy %s misc-const-correctness %t \2// RUN: -config='{CheckOptions: {\3// RUN: misc-const-correctness.AnalyzeValues: false,\4// RUN: misc-const-correctness.AnalyzeReferences: false,\5// RUN: misc-const-correctness.AnalyzePointers: true,\6// RUN: misc-const-correctness.WarnPointersAsValues: false,\7// RUN: misc-const-correctness.WarnPointersAsPointers: true,\8// RUN: misc-const-correctness.TransformPointersAsValues: false,\9// RUN: misc-const-correctness.TransformPointersAsPointers: true\10// RUN: }}' \11// RUN: -- -fno-delayed-template-parsing12 13void pointee_to_const() {14 int a[] = {1, 2};15 int *p_local0 = &a[0];16 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'p_local0' of type 'int *' can be declared 'const'17 // CHECK-FIXES: int const*p_local0 = &a[0];18 p_local0 = &a[1];19}20 21void array_of_pointer_to_const() {22 int a[] = {1, 2};23 int *p_local0[1] = {&a[0]};24 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'p_local0' of type 'int *[1]' can be declared 'const'25 // CHECK-FIXES: int const*p_local0[1] = {&a[0]};26 p_local0[0] = &a[1];27}28 29template<class T>30void template_fn() {31 T a[] = {1, 2};32 T *p_local0 = &a[0];33 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'p_local0' of type 'char *' can be declared 'const'34 // CHECK-FIXES: T const*p_local0 = &a[0];35 p_local0 = &a[1];36}37 38void instantiate() {39 template_fn<char>();40 template_fn<int>();41 template_fn<char const>();42}43 44using const_int = int const;45void ignore_const_alias() {46 const_int a[] = {1, 2};47 const_int *p_local0 = &a[0];48 p_local0 = &a[1];49}50 51void *return_non_const() {52 void *const a = nullptr;53 return a;54}55 56void function_pointer_basic() {57 void (*const fp)() = nullptr;58 fp();59}60 61void takeNonConstRef(int *&r);62 63void ignoreNonConstRefOps() {64 // init with non-const ref65 int* p0 {nullptr};66 int*& r1 = p0;67 68 // non-const ref param69 int* p1 {nullptr};70 takeNonConstRef(p1);71 72 // cast73 int* p2 {nullptr};74 int*& r2 = (int*&)p2;75}76