From bc0c423a46adde261cee400a74a84311c84ce287 Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Fri, 5 Dec 2014 11:59:05 +0000 Subject: [clang-tidy] Add clang-tidy check for unique_ptr's reset+release -> move Replace x.reset(y.release()); with x = std::move(y); If y is rvalue, replace with x = y; instead. http://reviews.llvm.org/D6485 Patch by Alexey Sokolov! llvm-svn: 223460 --- .../clang-tidy/misc-uniqueptr-reset-release.cpp | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp (limited to 'clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp') diff --git a/clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp b/clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp new file mode 100644 index 00000000000..763a428ecd9 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp @@ -0,0 +1,48 @@ +// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-uniqueptr-reset-release %t +// REQUIRES: shell + +namespace std { +template +struct unique_ptr { + unique_ptr(); + explicit unique_ptr(T *); + template + unique_ptr(unique_ptr &&); + void reset(T *); + T *release(); +}; +} // namespace std + +struct Foo {}; +struct Bar : Foo {}; + +std::unique_ptr Create(); +std::unique_ptr &Look(); +std::unique_ptr *Get(); + +void f() { + std::unique_ptr a, b; + std::unique_ptr c; + std::unique_ptr *x = &a; + std::unique_ptr *y = &b; + + a.reset(b.release()); + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer ptr1 = std::move(ptr2) over ptr1.reset(ptr2.release()) [misc-uniqueptr-reset-release] + // CHECK-FIXES: a = std::move(b); + a.reset(c.release()); + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer ptr1 = std::move(ptr2) + // CHECK-FIXES: a = std::move(c); + a.reset(Create().release()); + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer ptr1 = std::move(ptr2) + // CHECK-FIXES: a = Create(); + x->reset(y->release()); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: prefer ptr1 = std::move(ptr2) + // CHECK-FIXES: *x = std::move(*y); + Look().reset(Look().release()); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: prefer ptr1 = std::move(ptr2) + // CHECK-FIXES: Look() = std::move(Look()); + Get()->reset(Get()->release()); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: prefer ptr1 = std::move(ptr2) + // CHECK-FIXES: *Get() = std::move(*Get()); +} + -- cgit v1.2.3