| 1 | // RUN: %check_clang_tidy %s performance-unnecessary-copy-initialization %t -- -config="{CheckOptions: {performance-unnecessary-copy-initialization.AllowedTypes: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$'}}" -- |
| 2 | |
| 3 | struct SmartPointer { |
| 4 | ~SmartPointer(); |
| 5 | }; |
| 6 | |
| 7 | struct smart_pointer { |
| 8 | ~smart_pointer(); |
| 9 | }; |
| 10 | |
| 11 | struct SmartPtr { |
| 12 | ~SmartPtr(); |
| 13 | }; |
| 14 | |
| 15 | struct smart_ptr { |
| 16 | ~smart_ptr(); |
| 17 | }; |
| 18 | |
| 19 | struct SmartReference { |
| 20 | ~SmartReference(); |
| 21 | }; |
| 22 | |
| 23 | struct smart_reference { |
| 24 | ~smart_reference(); |
| 25 | }; |
| 26 | |
| 27 | struct SmartRef { |
| 28 | ~SmartRef(); |
| 29 | }; |
| 30 | |
| 31 | struct smart_ref { |
| 32 | ~smart_ref(); |
| 33 | }; |
| 34 | |
| 35 | struct OtherType { |
| 36 | ~OtherType(); |
| 37 | void constMethod() const; |
| 38 | }; |
| 39 | |
| 40 | template <typename T> struct SomeComplexTemplate { |
| 41 | ~SomeComplexTemplate(); |
| 42 | }; |
| 43 | |
| 44 | typedef SomeComplexTemplate<int> NotTooComplexRef; |
| 45 | |
| 46 | const SmartPointer &getSmartPointer(); |
| 47 | const smart_pointer &get_smart_pointer(); |
| 48 | const SmartPtr &getSmartPtr(); |
| 49 | const smart_ptr &get_smart_ptr(); |
| 50 | const SmartReference &getSmartReference(); |
| 51 | const smart_reference &get_smart_reference(); |
| 52 | const SmartRef &getSmartRef(); |
| 53 | const smart_ref &get_smart_ref(); |
| 54 | const OtherType &getOtherType(); |
| 55 | const NotTooComplexRef &getNotTooComplexRef(); |
| 56 | |
| 57 | void negativeSmartPointer() { |
| 58 | const auto P = getSmartPointer(); |
| 59 | } |
| 60 | |
| 61 | void negative_smart_pointer() { |
| 62 | const auto p = get_smart_pointer(); |
| 63 | } |
| 64 | |
| 65 | void negativeSmartPtr() { |
| 66 | const auto P = getSmartPtr(); |
| 67 | } |
| 68 | |
| 69 | void negative_smart_ptr() { |
| 70 | const auto p = get_smart_ptr(); |
| 71 | } |
| 72 | |
| 73 | void negativeSmartReference() { |
| 74 | const auto R = getSmartReference(); |
| 75 | } |
| 76 | |
| 77 | void negative_smart_reference() { |
| 78 | const auto r = get_smart_reference(); |
| 79 | } |
| 80 | |
| 81 | void negativeSmartRef() { |
| 82 | const auto R = getSmartRef(); |
| 83 | } |
| 84 | |
| 85 | void negative_smart_ref() { |
| 86 | const auto r = get_smart_ref(); |
| 87 | } |
| 88 | |
| 89 | void positiveOtherType() { |
| 90 | const auto O = getOtherType(); |
| 91 | // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'O' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization] |
| 92 | // CHECK-FIXES: const auto& O = getOtherType(); |
| 93 | O.constMethod(); |
| 94 | } |
| 95 | |
| 96 | void negativeNotTooComplexRef() { |
| 97 | const NotTooComplexRef R = getNotTooComplexRef(); |
| 98 | // Using `auto` here would result in the "canonical" type which does not match |
| 99 | // the pattern. |
| 100 | } |
| 101 | |