1 // RUN: %check_clang_tidy %s performance-unnecessary-copy-initialization %t -- -config="{CheckOptions: [{key: performance-unnecessary-copy-initialization.AllowedTypes, value: '[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 }; 38 39 template <typename T> struct SomeComplexTemplate { 40 ~SomeComplexTemplate(); 41 }; 42 43 typedef SomeComplexTemplate<int> NotTooComplexRef; 44 45 const SmartPointer &getSmartPointer(); 46 const smart_pointer &get_smart_pointer(); 47 const SmartPtr &getSmartPtr(); 48 const smart_ptr &get_smart_ptr(); 49 const SmartReference &getSmartReference(); 50 const smart_reference &get_smart_reference(); 51 const SmartRef &getSmartRef(); 52 const smart_ref &get_smart_ref(); 53 const OtherType &getOtherType(); 54 const NotTooComplexRef &getNotTooComplexRef(); 55 negativeSmartPointer()56void negativeSmartPointer() { 57 const auto P = getSmartPointer(); 58 } 59 negative_smart_pointer()60void negative_smart_pointer() { 61 const auto p = get_smart_pointer(); 62 } 63 negativeSmartPtr()64void negativeSmartPtr() { 65 const auto P = getSmartPtr(); 66 } 67 negative_smart_ptr()68void negative_smart_ptr() { 69 const auto p = get_smart_ptr(); 70 } 71 negativeSmartReference()72void negativeSmartReference() { 73 const auto R = getSmartReference(); 74 } 75 negative_smart_reference()76void negative_smart_reference() { 77 const auto r = get_smart_reference(); 78 } 79 negativeSmartRef()80void negativeSmartRef() { 81 const auto R = getSmartRef(); 82 } 83 negative_smart_ref()84void negative_smart_ref() { 85 const auto r = get_smart_ref(); 86 } 87 positiveOtherType()88void positiveOtherType() { 89 const auto O = getOtherType(); 90 // 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] 91 // CHECK-FIXES: const auto& O = getOtherType(); 92 } 93 negativeNotTooComplexRef()94void negativeNotTooComplexRef() { 95 const NotTooComplexRef R = getNotTooComplexRef(); 96 // Using `auto` here would result in the "canonical" type which does not match 97 // the pattern. 98 } 99