| 1 | // RUN: %check_clang_tidy %s performance-inefficient-string-concatenation %t |
|---|---|
| 2 | |
| 3 | namespace std { |
| 4 | template <typename T> |
| 5 | class basic_string { |
| 6 | public: |
| 7 | basic_string() {} |
| 8 | ~basic_string() {} |
| 9 | basic_string<T> *operator+=(const basic_string<T> &); |
| 10 | friend basic_string<T> operator+(const basic_string<T> &, const basic_string<T> &); |
| 11 | }; |
| 12 | typedef basic_string<char> string; |
| 13 | typedef basic_string<wchar_t> wstring; |
| 14 | } |
| 15 | |
| 16 | void f(std::string) {} |
| 17 | std::string g(std::string); |
| 18 | |
| 19 | int main() { |
| 20 | std::string mystr1, mystr2; |
| 21 | std::wstring mywstr1, mywstr2; |
| 22 | auto myautostr1 = mystr1; |
| 23 | auto myautostr2 = mystr2; |
| 24 | |
| 25 | for (int i = 0; i < 10; ++i) { |
| 26 | f(mystr1 + mystr2 + mystr1); |
| 27 | // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: string concatenation results in allocation of unnecessary temporary strings; consider using 'operator+=' or 'string::append()' instead |
| 28 | mystr1 = mystr1 + mystr2; |
| 29 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: string concatenation |
| 30 | mystr1 = mystr2 + mystr2 + mystr2; |
| 31 | // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: string concatenation |
| 32 | mystr1 = mystr2 + mystr1; |
| 33 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: string concatenation |
| 34 | mywstr1 = mywstr2 + mywstr1; |
| 35 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: string concatenation |
| 36 | mywstr1 = mywstr2 + mywstr2 + mywstr2; |
| 37 | // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: string concatenation |
| 38 | myautostr1 = myautostr1 + myautostr2; |
| 39 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: string concatenation |
| 40 | |
| 41 | mywstr1 = mywstr2 + mywstr2; |
| 42 | mystr1 = mystr2 + mystr2; |
| 43 | mystr1 += mystr2; |
| 44 | f(mystr2 + mystr1); |
| 45 | mystr1 = g(mystr1); |
| 46 | } |
| 47 | return 0; |
| 48 | } |
| 49 |
