File: avoid-const-params-in-decls.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (187 lines) | stat: -rw-r--r-- 5,596 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// RUN: %check_clang_tidy %s readability-avoid-const-params-in-decls %t

using alias_type = bool;
using alias_const_type = const bool;


void F1(const int i);
// 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]
// CHECK-FIXES: void F1(int i);

void F2(const int *const i);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
// CHECK-FIXES: void F2(const int *i);

void F3(int const i);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
// CHECK-FIXES: void F3(int i);

void F4(alias_type const i);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
// CHECK-FIXES: void F4(alias_type i);

void F5(const int);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
// CHECK-FIXES: void F5(int);

void F6(const int *const);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
// CHECK-FIXES: void F6(const int *);

void F7(int, const int);
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: parameter 2 is const-qualified
// CHECK-FIXES: void F7(int, int);

void F8(const int, const int);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
// CHECK-MESSAGES: :[[@LINE-2]]:20: warning: parameter 2 is const-qualified
// CHECK-FIXES: void F8(int, int);

void F9(const int long_name);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'long_name'
// CHECK-FIXES: void F9(int long_name);

void F10(const int *const *const long_name);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'long_name'
// CHECK-FIXES: void F10(const int *const *long_name);

void F11(const unsigned int /*v*/);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1
// CHECK-FIXES: void F11(unsigned int /*v*/);

void F12(const bool b = true);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'b'
// CHECK-FIXES: void F12(bool b = true);

template<class T>
int F13(const bool b = true);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'b'
// CHECK-FIXES: int F13(bool b = true);
int f13 = F13<int>();

template <typename T>
struct A {};

void F14(const A<const int>);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1 is const-qualified
// CHECK-FIXES: void F14(A<const int>);

void F15(const A<const int> Named);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'Named' is const-qualified
// CHECK-FIXES: void F15(A<const int> Named);

void F16(const A<const int> *const);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1 is const-qualified
// CHECK-FIXES: void F16(const A<const int> *);

void F17(const A<const int> *const Named);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'Named' is const-qualified
// CHECK-FIXES: void F17(const A<const int> *Named);

struct Foo {
  Foo(const int i);
  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: parameter 'i'
  // CHECK-FIXES: Foo(int i);

  void operator()(const int i);
  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
  // CHECK-FIXES: void operator()(int i);
};

template <class T>
struct FooT {
  FooT(const int i);
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: parameter 'i'
  // CHECK-FIXES: FooT(int i);

  void operator()(const int i);
  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
  // CHECK-FIXES: void operator()(int i);
};
FooT<int> f(1);

template <class T>
struct BingT {
  BingT(const T i);
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i'
  // CHECK-FIXES: BingT(T i);

  void operator()(const T i);
  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
  // CHECK-FIXES: void operator()(T i);
};
BingT<int> f2(1);

template <class T>
struct NeverInstantiatedT {
  NeverInstantiatedT(const T i);
  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: parameter 'i'
  // CHECK-FIXES: NeverInstantiatedT(T i);

  void operator()(const T i);
  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
  // CHECK-FIXES: void operator()(T i);
};

// Do not match on definitions
void NF1(const int i) {}
void NF2(const int *const i) {}
void NF3(int const i) {}
void NF4(alias_type const i) {}
void NF5(const int) {}
void NF6(const int *const) {}
void NF7(int, const int) {}
void NF8(const int, const int) {}
template <class T>
int NF9(const int, const int) { return 0; }
int nf9 = NF9<int>(1, 2);

// Do not match on inline member functions
struct Bar {
  Bar(const int i) {}

  void operator()(const int i) {}
};

// Do not match on inline member functions of a templated class
template <class T>
struct BarT {
  BarT(const int i) {}

  void operator()(const int i) {}
};
BarT<int> b(1);
template <class T>
struct BatT {
  BatT(const T i) {}

  void operator()(const T i) {}
};
BatT<int> b2(1);

// Do not match on other stuff
void NF(const alias_type& i);
void NF(const int &i);
void NF(const int *i);
void NF(alias_const_type i);
void NF(const alias_type&);
void NF(const int&);
void NF(const int*);
void NF(const int* const*);
void NF(alias_const_type);

// Regression tests involving macros, which are ignored by default.
#define CONCAT(a, b) a##b
void ConstNotVisible(CONCAT(cons, t) int i);

#define CONST_INT_PARAM const int i
void ConstInMacro(CONST_INT_PARAM);

#define DECLARE_FUNCTION_WITH_ARG(x) struct InsideMacro{ x }
DECLARE_FUNCTION_WITH_ARG(
    void member_function(const int i);
);

// Regression test. We should not warn (or crash) on lambda expressions
auto lambda_with_name = [](const int n) {};
auto lambda_without_name = [](const int) {};