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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
// RUN: %check_clang_tidy %s performance-for-range-copy %t -- -- -fno-delayed-template-parsing
namespace std {
template <typename _Tp>
struct remove_reference { typedef _Tp type; };
template <typename _Tp>
struct remove_reference<_Tp&> { typedef _Tp type; };
template <typename _Tp>
struct remove_reference<_Tp&&> { typedef _Tp type; };
template <typename _Tp>
constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) {
return static_cast<typename std::remove_reference<_Tp>::type &&>(__t);
}
} // std
template <typename T>
struct Iterator {
void operator++() {}
const T& operator*() {
static T* TT = new T();
return *TT;
}
bool operator!=(const Iterator &) { return false; }
typedef const T& const_reference;
};
template <typename T>
struct View {
View() = default;
T begin() { return T(); }
T begin() const { return T(); }
T end() { return T(); }
T end() const { return T(); }
typedef typename T::const_reference const_reference;
};
struct ConstructorConvertible {
};
struct S {
S();
S(const S &);
S(const ConstructorConvertible&) {}
~S();
S &operator=(const S &);
};
struct Convertible {
operator S() const {
return S();
}
};
void negativeConstReference() {
for (const S &S1 : View<Iterator<S>>()) {
}
}
void negativeUserDefinedConversion() {
Convertible C[0];
for (const S S1 : C) {
}
}
void negativeImplicitConstructorConversion() {
ConstructorConvertible C[0];
for (const S S1 : C) {
}
}
template <typename T>
void uninstantiated() {
for (const S S1 : View<Iterator<S>>()) {}
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: the loop variable's type is not a reference type; this creates a copy in each iteration; consider making this a reference [performance-for-range-copy]
// CHECK-FIXES: {{^}} for (const S& S1 : View<Iterator<S>>()) {}
// Don't warn on dependent types.
for (const T t1 : View<Iterator<T>>()) {
}
}
template <typename T>
void instantiated() {
for (const S S2 : View<Iterator<S>>()) {}
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: the loop variable's type is {{.*}}
// CHECK-FIXES: {{^}} for (const S& S2 : View<Iterator<S>>()) {}
for (const T T2 : View<Iterator<T>>()) {}
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: the loop variable's type is {{.*}}
// CHECK-FIXES: {{^}} for (const T& T2 : View<Iterator<T>>()) {}
}
template <typename T>
void instantiatedNegativeTypedefConstReference() {
for (typename T::const_reference T2 : T()) {
S S1 = T2;
}
}
void f() {
instantiated<int>();
instantiated<S>();
instantiatedNegativeTypedefConstReference<View<Iterator<S>>>();
}
struct Mutable {
Mutable() {}
Mutable(const Mutable &) = default;
Mutable(Mutable&&) = default;
Mutable(const Mutable &, const Mutable &) {}
void setBool(bool B) {}
bool constMethod() const {
return true;
}
Mutable& operator[](int I) {
return *this;
}
bool operator==(const Mutable &Other) const {
return true;
}
~Mutable() {}
};
struct Point {
~Point() {}
int x, y;
};
Mutable& operator<<(Mutable &Out, bool B) {
Out.setBool(B);
return Out;
}
bool operator!=(const Mutable& M1, const Mutable& M2) {
return false;
}
void use(const Mutable &M);
void use(int I);
void useTwice(const Mutable &M1, const Mutable &M2);
void useByValue(Mutable M);
void useByConstValue(const Mutable M);
void mutate(Mutable *M);
void mutate(Mutable &M);
void onceConstOnceMutated(const Mutable &M1, Mutable &M2);
void negativeVariableIsMutated() {
for (auto M : View<Iterator<Mutable>>()) {
mutate(M);
}
for (auto M : View<Iterator<Mutable>>()) {
mutate(&M);
}
for (auto M : View<Iterator<Mutable>>()) {
M.setBool(true);
}
}
void negativeOnceConstOnceMutated() {
for (auto M : View<Iterator<Mutable>>()) {
onceConstOnceMutated(M, M);
}
}
void negativeVarIsMoved() {
for (auto M : View<Iterator<Mutable>>()) {
auto Moved = std::move(M);
}
}
void negativeNonConstOperatorIsInvoked() {
for (auto NonConstOperatorInvokee : View<Iterator<Mutable>>()) {
auto& N = NonConstOperatorInvokee[0];
}
}
void negativeNonConstNonMemberOperatorInvoked() {
for (auto NonConstOperatorInvokee : View<Iterator<Mutable>>()) {
NonConstOperatorInvokee << true;
}
}
void negativeConstCheapToCopy() {
for (const int I : View<Iterator<int>>()) {
}
}
void negativeConstCheapToCopyTypedef() {
typedef const int ConstInt;
for (ConstInt C : View<Iterator<ConstInt>>()) {
}
}
void negativeCheapToCopy() {
for (int I : View<Iterator<int>>()) {
use(I);
}
}
void negativeCheapToCopyTypedef() {
typedef int Int;
for (Int I : View<Iterator<Int>>()) {
use(I);
}
}
void positiveOnlyConstMethodInvoked() {
for (auto M : View<Iterator<Mutable>>()) {
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]
// CHECK-FIXES: for (const auto& M : View<Iterator<Mutable>>()) {
M.constMethod();
}
}
void positiveOnlyUsedAsConstArguments() {
for (auto UsedAsConst : View<Iterator<Mutable>>()) {
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]
// CHECK-FIXES: for (const auto& UsedAsConst : View<Iterator<Mutable>>()) {
use(UsedAsConst);
useTwice(UsedAsConst, UsedAsConst);
useByValue(UsedAsConst);
useByConstValue(UsedAsConst);
}
}
void positiveOnlyAccessedFieldAsConst() {
for (auto UsedAsConst : View<Iterator<Point>>()) {
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]
// CHECK-FIXES: for (const auto& UsedAsConst : View<Iterator<Point>>()) {
use(UsedAsConst.x);
use(UsedAsConst.y);
}
}
void positiveOnlyUsedInCopyConstructor() {
for (auto A : View<Iterator<Mutable>>()) {
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]
// CHECK-FIXES: for (const auto& A : View<Iterator<Mutable>>()) {
Mutable Copy = A;
Mutable Copy2(A);
}
}
void positiveTwoConstConstructorArgs() {
for (auto A : View<Iterator<Mutable>>()) {
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]
// CHECK-FIXES: for (const auto& A : View<Iterator<Mutable>>()) {
Mutable Copy(A, A);
}
}
void PositiveConstMemberOperatorInvoked() {
for (auto ConstOperatorInvokee : View<Iterator<Mutable>>()) {
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]
// CHECK-FIXES: for (const auto& ConstOperatorInvokee : View<Iterator<Mutable>>()) {
bool result = ConstOperatorInvokee == Mutable();
}
}
void PositiveConstNonMemberOperatorInvoked() {
for (auto ConstOperatorInvokee : View<Iterator<Mutable>>()) {
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]
// CHECK-FIXES: for (const auto& ConstOperatorInvokee : View<Iterator<Mutable>>()) {
bool result = ConstOperatorInvokee != Mutable();
}
}
void IgnoreLoopVariableNotUsedInLoopBody() {
for (auto _ : View<Iterator<S>>()) {
}
}
template <typename T>
struct ValueReturningIterator {
void operator++() {}
T operator*() { return T(); }
bool operator!=(const ValueReturningIterator &) { return false; }
typedef const T &const_reference;
};
void negativeValueIterator() {
// Check does not trigger for iterators that return elements by value.
for (const S SS : View<ValueReturningIterator<S>>()) {
}
}
View<Iterator<S>> createView(S) { return View<Iterator<S>>(); }
void positiveValueIteratorUsedElseWhere() {
for (const S SS : createView(*ValueReturningIterator<S>())) {
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: the loop variable's type is not
// a reference type; this creates a copy in each iteration; consider making
// this a reference [performance-for-range-copy] CHECK-FIXES: for (const S&
// SS : createView(*ValueReturningIterator<S>())) {
}
}
void positiveConstMemberExpr() {
struct Struct {
Mutable Member;
};
for (Struct SS : View<Iterator<Struct>>()) {
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: loop variable is copied
// CHECK-FIXES: for (const Struct& SS : View<Iterator<Struct>>()) {
auto MemberCopy = SS.Member;
const auto &ConstRef = SS.Member;
bool b = SS.Member.constMethod();
use(SS.Member);
useByConstValue(SS.Member);
useByValue(SS.Member);
}
}
void negativeNonConstMemberExpr() {
struct Struct {
Mutable Member;
};
for (Struct SS : View<Iterator<Struct>>()) {
SS.Member.setBool(true);
}
for (Struct SS : View<Iterator<Struct>>()) {
SS.Member[1];
}
for (Struct SS : View<Iterator<Struct>>()) {
mutate(SS.Member);
}
for (Struct SS : View<Iterator<Struct>>()) {
mutate(&SS.Member);
}
}
|