146ae26e7SJonas Toth // RUN: %check_clang_tidy %s misc-const-correctness %t \
246ae26e7SJonas Toth // RUN: -config='{CheckOptions: \
3*1af159e9SPiotr Zegar // RUN: {misc-const-correctness.AnalyzeValues: true,\
4*1af159e9SPiotr Zegar // RUN: misc-const-correctness.WarnPointersAsValues: true,\
5*1af159e9SPiotr Zegar // RUN: misc-const-correctness.TransformPointersAsValues: true}}' \
646ae26e7SJonas Toth // RUN: -- -fno-delayed-template-parsing
746ae26e7SJonas Toth
potential_const_pointer()846ae26e7SJonas Toth void potential_const_pointer() {
946ae26e7SJonas Toth double np_local0[10] = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};
1046ae26e7SJonas Toth double *p_local0 = &np_local0[1];
1146ae26e7SJonas Toth // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'double *' can be declared 'const'
1246ae26e7SJonas Toth // CHECK-FIXES: double *const p_local0
13e66345d5SJonas Toth
14e66345d5SJonas Toth using doublePtr = double*;
15e66345d5SJonas Toth using doubleArray = double[15];
16e66345d5SJonas Toth doubleArray np_local1;
17e66345d5SJonas Toth doublePtr p_local1 = &np_local1[0];
18e66345d5SJonas Toth // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local1' of type 'doublePtr' (aka 'double *') can be declared 'const'
19e66345d5SJonas Toth // CHECK-FIXES: doublePtr const p_local1
20e66345d5SJonas Toth }
21e66345d5SJonas Toth
range_for()22e66345d5SJonas Toth void range_for() {
23e66345d5SJonas Toth int np_local0[2] = {1, 2};
24e66345d5SJonas Toth int *p_local0[2] = {&np_local0[0], &np_local0[1]};
25e66345d5SJonas Toth // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int *[2]' can be declared 'const'
26e66345d5SJonas Toth // CHECK-FIXES: int *const p_local0[2]
27e66345d5SJonas Toth for (const int *p_local1 : p_local0) {
28e66345d5SJonas Toth // CHECK-MESSAGES: [[@LINE-1]]:8: warning: variable 'p_local1' of type 'const int *' can be declared 'const'
29e66345d5SJonas Toth // CHECK-FIXES: for (const int *const p_local1 : p_local0)
30e66345d5SJonas Toth }
31e66345d5SJonas Toth
32e66345d5SJonas Toth int *p_local2[2] = {nullptr, nullptr};
33e66345d5SJonas Toth // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local2' of type 'int *[2]' can be declared 'const'
34e66345d5SJonas Toth // CHECK-FIXES: int *const p_local2[2]
35e66345d5SJonas Toth for (const auto *con_ptr : p_local2) {
36e66345d5SJonas Toth }
37e66345d5SJonas Toth
38e66345d5SJonas Toth }
39e66345d5SJonas Toth
40e66345d5SJonas Toth template <typename T>
41e66345d5SJonas Toth struct SmallVectorBase {
42e66345d5SJonas Toth T data[4];
push_backSmallVectorBase43e66345d5SJonas Toth void push_back(const T &el) {}
sizeSmallVectorBase44e66345d5SJonas Toth int size() const { return 4; }
beginSmallVectorBase45e66345d5SJonas Toth T *begin() { return data; }
beginSmallVectorBase46e66345d5SJonas Toth const T *begin() const { return data; }
endSmallVectorBase47e66345d5SJonas Toth T *end() { return data + 4; }
endSmallVectorBase48e66345d5SJonas Toth const T *end() const { return data + 4; }
49e66345d5SJonas Toth };
50e66345d5SJonas Toth
51e66345d5SJonas Toth template <typename T>
52e66345d5SJonas Toth struct SmallVector : SmallVectorBase<T> {};
53e66345d5SJonas Toth
54e66345d5SJonas Toth template <class T>
EmitProtocolMethodList(T && Methods)55e66345d5SJonas Toth void EmitProtocolMethodList(T &&Methods) {
56e66345d5SJonas Toth // Note: If the template is uninstantiated the analysis does not figure out,
57e66345d5SJonas Toth // that p_local0 could be const. Not sure why, but probably bails because
58e66345d5SJonas Toth // some expressions are type-dependent.
59e66345d5SJonas Toth SmallVector<const int *> p_local0;
60e66345d5SJonas Toth // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'SmallVector<const int *>' can be declared 'const'
61e66345d5SJonas Toth // CHECK-FIXES: SmallVector<const int *> const p_local0
62e66345d5SJonas Toth SmallVector<const int *> np_local0;
63e66345d5SJonas Toth for (const auto *I : Methods) {
64e66345d5SJonas Toth if (I == nullptr)
65e66345d5SJonas Toth np_local0.push_back(I);
66e66345d5SJonas Toth }
67e66345d5SJonas Toth p_local0.size();
68e66345d5SJonas Toth }
instantiate()69e66345d5SJonas Toth void instantiate() {
70e66345d5SJonas Toth int *p_local0[4] = {nullptr, nullptr, nullptr, nullptr};
71e66345d5SJonas Toth // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int *[4]' can be declared 'const'
72e66345d5SJonas Toth // CHECK-FIXES: int *const p_local0[4]
73e66345d5SJonas Toth EmitProtocolMethodList(p_local0);
7446ae26e7SJonas Toth }
75