|
| 1 | +// RUN: %check_clang_tidy %s modernize-make-unique %t -- \ |
| 2 | +// RUN: -config="{CheckOptions: [{key: modernize-make-unique.MakeSmartPtrType, value: '::base::unique_ptr'}, \ |
| 3 | +// RUN: {key: modernize-make-unique.MakeSmartPtrFunction, value: 'base::make_unique'}]}" \ |
| 4 | +// RUN: -- -std=c++11 -nostdinc++ |
| 5 | + |
| 6 | +namespace std { |
| 7 | + template<typename T> |
| 8 | + struct default_delete { |
| 9 | + void operator()(T* ptr) const { delete ptr; } |
| 10 | + }; |
| 11 | +} |
| 12 | + |
| 13 | +namespace base { |
| 14 | + |
| 15 | +using nullptr_t = decltype(nullptr); |
| 16 | + |
| 17 | +template <typename T, typename Deleter = std::default_delete<T>> |
| 18 | +class unique_ptr { |
| 19 | +public: |
| 20 | + typedef T element_type; |
| 21 | + typedef T* pointer; |
| 22 | + |
| 23 | + constexpr unique_ptr() noexcept : ptr_(nullptr) {} |
| 24 | + constexpr unique_ptr(nullptr_t) noexcept : ptr_(nullptr) {} |
| 25 | + explicit unique_ptr(T* p) noexcept : ptr_(p) {} |
| 26 | + unique_ptr(unique_ptr&& r) noexcept : ptr_(r.ptr_) { r.ptr_ = nullptr; } |
| 27 | + unique_ptr(const unique_ptr&) = delete; |
| 28 | + unique_ptr& operator=(const unique_ptr&) = delete; |
| 29 | + unique_ptr& operator=(unique_ptr&& r) noexcept { |
| 30 | + T* old = ptr_; |
| 31 | + ptr_ = r.ptr_; |
| 32 | + r.ptr_ = nullptr; |
| 33 | + delete old; |
| 34 | + return *this; |
| 35 | + } |
| 36 | + void reset(T* p = nullptr) noexcept { |
| 37 | + T* old = ptr_; |
| 38 | + ptr_ = p; |
| 39 | + delete old; |
| 40 | + } |
| 41 | + T* get() const noexcept { return ptr_; } |
| 42 | + T& operator*() const noexcept { return *ptr_; } |
| 43 | + T* operator->() const noexcept { return ptr_; } |
| 44 | + explicit operator bool() const noexcept { return ptr_ != nullptr; } |
| 45 | + ~unique_ptr() { delete ptr_; } |
| 46 | +private: |
| 47 | + T* ptr_; |
| 48 | +}; |
| 49 | + |
| 50 | +template <typename T> |
| 51 | +unique_ptr<T> make_unique() { |
| 52 | + return unique_ptr<T>(new T()); |
| 53 | +} |
| 54 | + |
| 55 | +template <typename T, typename Arg1> |
| 56 | +unique_ptr<T> make_unique(const Arg1& arg1) { |
| 57 | + return unique_ptr<T>(new T(arg1)); |
| 58 | +} |
| 59 | + |
| 60 | +template <typename T, typename Arg1, typename Arg2> |
| 61 | +unique_ptr<T> make_unique(const Arg1& arg1, const Arg2& arg2) { |
| 62 | + return unique_ptr<T>(new T(arg1, arg2)); |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace base |
| 66 | + |
| 67 | +struct Base { |
| 68 | + Base() {} |
| 69 | + Base(int, int) {} |
| 70 | +}; |
| 71 | + |
| 72 | +void test() { |
| 73 | + base::unique_ptr<Base> P1 = base::unique_ptr<Base>(new Base()); |
| 74 | + // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: use base::make_unique instead [modernize-make-unique] |
| 75 | + // CHECK-FIXES: base::unique_ptr<Base> P1 = base::make_unique<Base>(); |
| 76 | + |
| 77 | + P1.reset(new Base(1, 2)); |
| 78 | + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use base::make_unique instead [modernize-make-unique] |
| 79 | + // CHECK-FIXES: P1 = base::make_unique<Base>(1, 2); |
| 80 | + |
| 81 | + P1 = base::unique_ptr<Base>(new Base(1, 2)); |
| 82 | + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use base::make_unique instead [modernize-make-unique] |
| 83 | + // CHECK-FIXES: P1 = base::make_unique<Base>(1, 2); |
| 84 | +} |
| 85 | + |
| 86 | +base::unique_ptr<Base> factory() { |
| 87 | + return base::unique_ptr<Base>(new Base); |
| 88 | + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use base::make_unique instead [modernize-make-unique] |
| 89 | + // CHECK-FIXES: return base::make_unique<Base>(); |
| 90 | +} |
0 commit comments