1// RUN: %check_clang_tidy %s readability-avoid-const-params-in-decls %t
2
3using alias_type = bool;
4using alias_const_type = const bool;
5
6
7void 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
11void F2(const int *const i);
12// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: parameter 'i' is const-qualified
13// CHECK-FIXES: void F2(const int *i);
14
15void F3(int const i);
16// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: parameter 'i' is const-qualified
17// CHECK-FIXES: void F3(int i);
18
19void F4(alias_type const i);
20// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: parameter 'i' is const-qualified
21// CHECK-FIXES: void F4(alias_type i);
22
23void F5(const int);
24// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
25// CHECK-FIXES: void F5(int);
26
27void F6(const int *const);
28// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: parameter 1 is const-qualified
29// CHECK-FIXES: void F6(const int *);
30
31void F7(int, const int);
32// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: parameter 2 is const-qualified
33// CHECK-FIXES: void F7(int, int);
34
35void 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
40void F9(const int long_name);
41// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'long_name'
42// CHECK-FIXES: void F9(int long_name);
43
44void F10(const int *const *const long_name);
45// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: parameter 'long_name'
46// CHECK-FIXES: void F10(const int *const *long_name);
47
48void F11(const unsigned int /*v*/);
49// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1
50// CHECK-FIXES: void F11(unsigned int /*v*/);
51
52void F12(const bool b = true);
53// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'b'
54// CHECK-FIXES: void F12(bool b = true);
55
56template<class T>
57int F13(const bool b = true);
58// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'b'
59// CHECK-FIXES: int F13(bool b = true);
60int f13 = F13<int>();
61
62template <typename T>
63struct A {};
64
65void 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
69void 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
73void F16(const A<const int> *const);
74// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: parameter 1 is const-qualified
75// CHECK-FIXES: void F16(const A<const int> *);
76
77void F17(const A<const int> *const Named);
78// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: parameter 'Named' is const-qualified
79// CHECK-FIXES: void F17(const A<const int> *Named);
80
81struct 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
91template <class T>
92struct 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};
101FooT<int> f(1);
102
103template <class T>
104struct 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};
113BingT<int> f2(1);
114
115template <class T>
116struct 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
127void NF1(const int i) {}
128void NF2(const int *const i) {}
129void NF3(int const i) {}
130void NF4(alias_type const i) {}
131void NF5(const int) {}
132void NF6(const int *const) {}
133void NF7(int, const int) {}
134void NF8(const int, const int) {}
135template <class T>
136int NF9(const int, const int) { return 0; }
137int nf9 = NF9<int>(1, 2);
138
139// Do not match on inline member functions
140struct Bar {
141 Bar(const int i) {}
142
143 void operator()(const int i) {}
144};
145
146// Do not match on inline member functions of a templated class
147template <class T>
148struct BarT {
149 BarT(const int i) {}
150
151 void operator()(const int i) {}
152};
153BarT<int> b(1);
154template <class T>
155struct BatT {
156 BatT(const T i) {}
157
158 void operator()(const T i) {}
159};
160BatT<int> b2(1);
161
162// Do not match on other stuff
163void NF(const alias_type& i);
164void NF(const int &i);
165void NF(const int *i);
166void NF(alias_const_type i);
167void NF(const alias_type&);
168void NF(const int&);
169void NF(const int*);
170void NF(const int* const*);
171void NF(alias_const_type);
172
173// Regression tests involving macros, which are ignored by default.
174#define CONCAT(a, b) a##b
175void ConstNotVisible(CONCAT(cons, t) int i);
176
177#define CONST_INT_PARAM const int i
178void ConstInMacro(CONST_INT_PARAM);
179
180#define DECLARE_FUNCTION_WITH_ARG(x) struct InsideMacro{ x }
181DECLARE_FUNCTION_WITH_ARG(
182 void member_function(const int i);
183);
184
185// Regression test. We should not warn (or crash) on lambda expressions
186auto lambda_with_name = [](const int n) {};
187auto lambda_without_name = [](const int) {};
188

source code of clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp