1 // RUN: %check_clang_tidy %s readability-avoid-const-params-in-decls %t
2 
3 using alias_type = bool;
4 using alias_const_type = const bool;
5 
6 
7 void F1(const int i);
8 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls]
9 // CHECK-FIXES: void F1(int i);
10 
11 void F2(const int *const i);
12 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
13 // CHECK-FIXES: void F2(const int *i);
14 
15 void F3(int const i);
16 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
17 // CHECK-FIXES: void F3(int i);
18 
19 void F4(alias_type const i);
20 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
21 // CHECK-FIXES: void F4(alias_type i);
22 
23 void F5(const int);
24 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
25 // CHECK-FIXES: void F5(int);
26 
27 void F6(const int *const);
28 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
29 // CHECK-FIXES: void F6(const int *);
30 
31 void F7(int, const int);
32 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: parameter 2 is const-qualified
33 // CHECK-FIXES: void F7(int, int);
34 
35 void F8(const int, const int);
36 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
37 // CHECK-MESSAGES: :[[@LINE-2]]:20: warning: parameter 2 is const-qualified
38 // CHECK-FIXES: void F8(int, int);
39 
40 void F9(const int long_name);
41 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'long_name'
42 // CHECK-FIXES: void F9(int long_name);
43 
44 void F10(const int *const *const long_name);
45 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'long_name'
46 // CHECK-FIXES: void F10(const int *const *long_name);
47 
48 void F11(const unsigned int /*v*/);
49 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1
50 // CHECK-FIXES: void F11(unsigned int /*v*/);
51 
52 void F12(const bool b = true);
53 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'b'
54 // CHECK-FIXES: void F12(bool b = true);
55 
56 template<class T>
57 int F13(const bool b = true);
58 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'b'
59 // CHECK-FIXES: int F13(bool b = true);
60 int f13 = F13<int>();
61 
62 template <typename T>
63 struct A {};
64 
65 void F14(const A<const int>);
66 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1 is const-qualified
67 // CHECK-FIXES: void F14(A<const int>);
68 
69 void F15(const A<const int> Named);
70 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'Named' is const-qualified
71 // CHECK-FIXES: void F15(A<const int> Named);
72 
73 void F16(const A<const int> *const);
74 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1 is const-qualified
75 // CHECK-FIXES: void F16(const A<const int> *);
76 
77 void F17(const A<const int> *const Named);
78 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'Named' is const-qualified
79 // CHECK-FIXES: void F17(const A<const int> *Named);
80 
81 struct Foo {
82   Foo(const int i);
83   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: parameter 'i'
84   // CHECK-FIXES: Foo(int i);
85 
86   void operator()(const int i);
87   // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
88   // CHECK-FIXES: void operator()(int i);
89 };
90 
91 template <class T>
92 struct FooT {
93   FooT(const int i);
94   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: parameter 'i'
95   // CHECK-FIXES: FooT(int i);
96 
97   void operator()(const int i);
98   // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
99   // CHECK-FIXES: void operator()(int i);
100 };
101 FooT<int> f(1);
102 
103 template <class T>
104 struct BingT {
105   BingT(const T i);
106   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i'
107   // CHECK-FIXES: BingT(T i);
108 
109   void operator()(const T i);
110   // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
111   // CHECK-FIXES: void operator()(T i);
112 };
113 BingT<int> f2(1);
114 
115 template <class T>
116 struct NeverInstantiatedT {
117   NeverInstantiatedT(const T i);
118   // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: parameter 'i'
119   // CHECK-FIXES: NeverInstantiatedT(T i);
120 
121   void operator()(const T i);
122   // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
123   // CHECK-FIXES: void operator()(T i);
124 };
125 
126 // Do not match on definitions
NF1(const int i)127 void NF1(const int i) {}
NF2(const int * const i)128 void NF2(const int *const i) {}
NF3(int const i)129 void NF3(int const i) {}
NF4(alias_type const i)130 void NF4(alias_type const i) {}
NF5(const int)131 void NF5(const int) {}
NF6(const int * const)132 void NF6(const int *const) {}
NF7(int,const int)133 void NF7(int, const int) {}
NF8(const int,const int)134 void NF8(const int, const int) {}
135 template <class T>
NF9(const int,const int)136 int NF9(const int, const int) { return 0; }
137 int nf9 = NF9<int>(1, 2);
138 
139 // Do not match on inline member functions
140 struct Bar {
BarBar141   Bar(const int i) {}
142 
operator ()Bar143   void operator()(const int i) {}
144 };
145 
146 // Do not match on inline member functions of a templated class
147 template <class T>
148 struct BarT {
BarTBarT149   BarT(const int i) {}
150 
operator ()BarT151   void operator()(const int i) {}
152 };
153 BarT<int> b(1);
154 template <class T>
155 struct BatT {
BatTBatT156   BatT(const T i) {}
157 
operator ()BatT158   void operator()(const T i) {}
159 };
160 BatT<int> b2(1);
161 
162 // Do not match on other stuff
163 void NF(const alias_type& i);
164 void NF(const int &i);
165 void NF(const int *i);
166 void NF(alias_const_type i);
167 void NF(const alias_type&);
168 void NF(const int&);
169 void NF(const int*);
170 void NF(const int* const*);
171 void NF(alias_const_type);
172 
173 // Regression test for when the 'const' token is not in the code.
174 #define CONCAT(a, b) a##b
175 void ConstNotVisible(CONCAT(cons, t) int i);
176 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: parameter 'i'
177 // We warn, but we can't give a fix
178 // CHECK-FIXES: void ConstNotVisible(CONCAT(cons, t) int i);
179 
180 // Regression test. We should not warn (or crash) on lambda expressions
__anon0b64554a0102(const int n) 181 auto lambda_with_name = [](const int n) {};
__anon0b64554a0202(const int) 182 auto lambda_without_name = [](const int) {};
183