Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 1 | // RUN: %python %S/check_clang_tidy.py %s modernize-make-unique %t |
| 2 | |
| 3 | namespace std { |
| 4 | |
Angel Garcia Gomez | b9f3059 | 2015-10-05 12:20:17 +0000 | [diff] [blame^] | 5 | template <typename T> |
| 6 | class default_delete {}; |
| 7 | |
| 8 | template <typename type, typename Deleter = std::default_delete<type>> |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 9 | class unique_ptr { |
| 10 | public: |
| 11 | unique_ptr(type *ptr); |
| 12 | unique_ptr(const unique_ptr<type> &t) = delete; |
| 13 | unique_ptr(unique_ptr<type> &&t); |
| 14 | ~unique_ptr(); |
| 15 | type &operator*() { return *ptr; } |
| 16 | type *operator->() { return ptr; } |
| 17 | type *release(); |
| 18 | void reset(); |
| 19 | void reset(type *pt); |
| 20 | |
| 21 | private: |
| 22 | type *ptr; |
| 23 | }; |
| 24 | |
| 25 | } |
| 26 | |
| 27 | struct Base { |
| 28 | Base(); |
| 29 | Base(int, int); |
| 30 | }; |
| 31 | |
| 32 | struct Derived : public Base { |
| 33 | Derived(); |
| 34 | Derived(int, int); |
| 35 | }; |
| 36 | |
| 37 | struct Pair { |
| 38 | int a, b; |
| 39 | }; |
| 40 | |
| 41 | template<class T> using unique_ptr_ = std::unique_ptr<T>; |
| 42 | |
| 43 | int g(std::unique_ptr<int> P); |
| 44 | |
| 45 | std::unique_ptr<Base> getPointer() { |
| 46 | return std::unique_ptr<Base>(new Base); |
| 47 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead |
| 48 | // CHECK-FIXES: return std::make_unique<Base>(); |
| 49 | } |
| 50 | |
| 51 | void f() { |
| 52 | std::unique_ptr<int> P1 = std::unique_ptr<int>(new int()); |
| 53 | // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] |
| 54 | // CHECK-FIXES: std::unique_ptr<int> P1 = std::make_unique<int>(); |
| 55 | |
| 56 | // Without parenthesis. |
| 57 | std::unique_ptr<int> P2 = std::unique_ptr<int>(new int); |
| 58 | // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] |
| 59 | // CHECK-FIXES: std::unique_ptr<int> P2 = std::make_unique<int>(); |
| 60 | |
| 61 | // With auto. |
| 62 | auto P3 = std::unique_ptr<int>(new int()); |
| 63 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead |
| 64 | // CHECK-FIXES: auto P3 = std::make_unique<int>(); |
| 65 | |
| 66 | { |
| 67 | // No std. |
| 68 | using namespace std; |
| 69 | unique_ptr<int> Q = unique_ptr<int>(new int()); |
| 70 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead |
| 71 | // CHECK-FIXES: unique_ptr<int> Q = std::make_unique<int>(); |
| 72 | } |
| 73 | |
| 74 | std::unique_ptr<int> R(new int()); |
| 75 | |
| 76 | // Create the unique_ptr as a parameter to a function. |
| 77 | int T = g(std::unique_ptr<int>(new int())); |
| 78 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead |
| 79 | // CHECK-FIXES: int T = g(std::make_unique<int>()); |
| 80 | |
| 81 | // Arguments are correctly handled. |
| 82 | std::unique_ptr<Base> Pbase = std::unique_ptr<Base>(new Base(5, T)); |
| 83 | // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use std::make_unique instead |
| 84 | // CHECK-FIXES: std::unique_ptr<Base> Pbase = std::make_unique<Base>(5, T); |
| 85 | |
| 86 | // Works with init lists. |
| 87 | std::unique_ptr<Pair> Ppair = std::unique_ptr<Pair>(new Pair{T, 1}); |
| 88 | // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use std::make_unique instead |
| 89 | // CHECK-FIXES: std::unique_ptr<Pair> Ppair = std::make_unique<Pair>({T, 1}); |
| 90 | |
| 91 | // Only replace if the type in the template is the same than the type returned |
| 92 | // by the new operator. |
| 93 | auto Pderived = std::unique_ptr<Base>(new Derived()); |
| 94 | |
| 95 | // The pointer is returned by the function, nothing to do. |
| 96 | std::unique_ptr<Base> RetPtr = getPointer(); |
| 97 | |
| 98 | // Aliases. |
| 99 | typedef std::unique_ptr<int> IntPtr; |
| 100 | IntPtr Typedef = IntPtr(new int); |
| 101 | // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead |
| 102 | // CHECK-FIXES: IntPtr Typedef = std::make_unique<int>(); |
| 103 | |
| 104 | #define PTR unique_ptr<int> |
| 105 | std::unique_ptr<int> Macro = std::PTR(new int); |
| 106 | // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead |
| 107 | // CHECK-FIXES: std::unique_ptr<int> Macro = std::make_unique<int>(); |
| 108 | #undef PTR |
| 109 | |
| 110 | std::unique_ptr<int> Using = unique_ptr_<int>(new int); |
| 111 | // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead |
| 112 | // CHECK-FIXES: std::unique_ptr<int> Using = std::make_unique<int>(); |
| 113 | |
| 114 | // This emulates std::move. |
| 115 | std::unique_ptr<int> Move = static_cast<std::unique_ptr<int>&&>(P1); |
| 116 | |
| 117 | // Adding whitespaces. |
| 118 | auto Space = std::unique_ptr <int>(new int()); |
| 119 | // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead |
| 120 | // CHECK-FIXES: auto Space = std::make_unique<int>(); |
| 121 | |
| 122 | auto Spaces = std :: unique_ptr <int>(new int()); |
| 123 | // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead |
| 124 | // CHECK-FIXES: auto Spaces = std::make_unique<int>(); |
| 125 | } |