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