Alexander Kornienko | 5aebfe2 | 2016-01-29 15:54:26 +0000 | [diff] [blame] | 1 | // RUN: %check_clang_tidy %s performance-for-range-copy %t -config="{CheckOptions: [{key: "performance-for-range-copy.WarnOnAllAutoCopies", value: 1}]}" -- -std=c++11 |
| 2 | |
| 3 | template <typename T> |
| 4 | struct Iterator { |
| 5 | void operator++() {} |
| 6 | const T& operator*() { |
| 7 | static T* TT = new T(); |
| 8 | return *TT; |
| 9 | } |
| 10 | bool operator!=(const Iterator &) { return false; } |
| 11 | }; |
| 12 | template <typename T> |
| 13 | struct View { |
| 14 | T begin() { return T(); } |
| 15 | T begin() const { return T(); } |
| 16 | T end() { return T(); } |
| 17 | T end() const { return T(); } |
| 18 | }; |
| 19 | |
| 20 | struct S { |
| 21 | S(); |
| 22 | S(const S &); |
| 23 | ~S(); |
| 24 | S &operator=(const S &); |
| 25 | }; |
| 26 | |
| 27 | void NegativeLoopVariableNotAuto() { |
| 28 | for (S S1 : View<Iterator<S>>()) { |
| 29 | S* S2 = &S1; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | void PositiveTriggeredForAutoLoopVariable() { |
| 34 | for (auto S1 : View<Iterator<S>>()) { |
| 35 | // CHECK-MESSAGES: [[@LINE-1]]:13: 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] |
| 36 | // CHECK-FIXES: for (const auto& S1 : View<Iterator<S>>()) { |
| 37 | S* S2 = &S1; |
| 38 | } |
| 39 | } |