[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 193421. Dosi-Dough marked 2 inline comments as done. Dosi-Dough added a comment. added minor style changes and removed unnecessary inline comments. Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); +} + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -20,6 +20,7 @@ abseil-string-find-startswith abseil-time-subtraction abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,32 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr`` then recommends using +``absl::wrap_unique(new T(...))``. + +.. code-block:: c++ + + class A { + public: +static A* NewA() { return new A(); } + + private: +A() = default; + }; + + std::unique_ptr a; + + // Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + // Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + // Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + // Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,13 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index:
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 192541. Dosi-Dough marked 4 inline comments as done. Dosi-Dough added a comment. fixed bracketed return and added updated license CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); +} + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -20,6 +20,7 @@ abseil-string-find-startswith abseil-time-subtraction abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,32 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr`` then recommends using +``absl::wrap_unique(new T(...))``. + +.. code-block:: c++ + + class A { + public: +static A* NewA() { return new A(); } + + private: +A() {} + }; + + std::unique_ptr a; + + // Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + // Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + // Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + // Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,13 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/abseil/WrapUniqueCheck.h
[PATCH] D57435: [clang-tidy] created wrap-unique check
Dosi-Dough created this revision. Dosi-Dough added reviewers: EricWF, JonasToth. Dosi-Dough added a project: clang-tools-extra. Herald added subscribers: xazax.hun, mgorny. based on abseil tip of the week 126 https://abseil.io/tips/126 Created a check which looks for instances of a factory function that uses a non-public constructor and return a std::unique_ptr. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D57435 Files: clang-tidy/.DS_Store clang-tidy/abseil/.DS_Store clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/.DS_Store test/clang-tidy/.DS_Store test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,91 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); + +} + + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -17,6 +17,7 @@ abseil-str-cat-append abseil-string-find-startswith abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,34 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== + +Checks for instances of static function within a class being called and +returning a std:unique_ptr type. Also checks for instances where reset +is called on a static function which returns std::unique_ptr. + +.. code-block:: c++ + + class A { +public: + static A* NewA() { +return new A(); + } + +private: + A() {} + }; + + std::unique_ptr a; + + //Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + //Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + //Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + //Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,13 @@ Improvements to clang-tidy -- +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 184431. Dosi-Dough marked 11 inline comments as done. Dosi-Dough added a comment. fixed white space/ formatting issues. removed .DS_Store. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/.DS_Store clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/WrapUniqueCheck.cpp docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- test/clang-tidy/abseil-wrap-unique.cpp +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -1,6 +1,5 @@ // RUN: %check_clang_tidy %s abseil-wrap-unique %t - namespace std { template @@ -29,7 +28,6 @@ }; } // namespace std - class A { public: static A* NewA() { @@ -85,7 +83,5 @@ //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) //std::unique_ptr e(new int[2] {1,2}); - } - Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- docs/clang-tidy/checks/abseil-wrap-unique.rst +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -2,33 +2,30 @@ abseil-wrap-unique == - -Checks for instances of static function within a class being called and -returning a std:unique_ptr type. Also checks for instances where reset -is called on a static function which returns std::unique_ptr. +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr`` then recommends using +``absl::wrap_unique(new T(...))``. .. code-block:: c++ - class A { -public: - static A* NewA() { -return new A(); - } + public: +static A* NewA() { return new A(); } -private: - A() {} + private: +A() {} }; - + std::unique_ptr a; - - //Original - reset called with a static function returning a std::unqiue_ptr + + // Original - reset called with a static function returning a std::unqiue_ptr a.reset(A::NewA()); - //Suggested - reset ptr with absl::WrapUnique + // Suggested - reset ptr with absl::WrapUnique a = absl::WrapUnique(A::NewA()); - //Original - std::unique_ptr initialized with static function + // Original - std::unique_ptr initialized with static function std::unique_ptr b(A::NewA()); - //Suggested - initialize with absl::WrapUnique instead + // Suggested - initialize with absl::WrapUnique instead auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,19 +67,20 @@ Improvements to clang-tidy -- -- New :doc:`abseil-wrap-unique - ` check. - - Looks for instances of factory functions which uses a non-public constructor - that returns a std::unqiue_ptr then recommends using - absl::wrap_unique(new T(...)). - - New :doc:`abseil-duration-conversion-cast ` check. Checks for casts of ``absl::Duration`` conversion functions, and recommends the right conversion function instead. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + + Improvements to include-fixer - Index: clang-tidy/abseil/WrapUniqueCheck.cpp === --- clang-tidy/abseil/WrapUniqueCheck.cpp +++ clang-tidy/abseil/WrapUniqueCheck.cpp @@ -1,4 +1,4 @@ -//===--- WrapUniqueCheck.cpp - clang-tidy -===// +//===--- WrapUniqueCheck.cpp - clang-tidy -===// // // The LLVM Compiler Infrastructure // @@ -7,10 +7,10 @@ // //===--===// -#include #include "WrapUniqueCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include using namespace clang::ast_matchers; @@ -21,26 +21,30 @@ std::string WrapUniqueCheck::getArgs(const SourceManager *SM, const CallExpr *MemExpr) { llvm::StringRef ArgRef = Lexer::getSourceText( -CharSourceRange::getCharRange( - MemExpr->getSourceRange()), *SM, LangOptions()); + CharSourceRange::getCharRange(MemExpr->getSourceRange()), *SM, + LangOptions()); return (ArgRef.str().length() > 0) ? ArgRef.str() + ")" : "()"; } void WrapUniqueCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher( - cxxMemberCallExpr( -callee(cxxMethodDecl(hasName("reset"), - ofClass(cxxRecordDecl(hasName("std::unique_ptr"), decl(), -
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 184437. Dosi-Dough added a comment. based on feedback: fixed whitespacing / formatting. Removed .DS_Store Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); +} + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -17,6 +17,7 @@ abseil-str-cat-append abseil-string-find-startswith abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,31 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr`` then recommends using +``absl::wrap_unique(new T(...))``. + +.. code-block:: c++ + class A { + public: +static A* NewA() { return new A(); } + + private: +A() {} + }; + + std::unique_ptr a; + + // Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + // Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + // Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + // Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -73,6 +73,14 @@ Checks for casts of ``absl::Duration`` conversion functions, and recommends the right conversion function instead. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + + Improvements to include-fixer - Index: clang-tidy/abseil/WrapUniqueCheck.h === ---
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 194621. Dosi-Dough marked 2 inline comments as done. Dosi-Dough added a comment. added whitespace to check doc Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); +} + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -20,6 +20,7 @@ abseil-string-find-startswith abseil-time-subtraction abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,35 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== + +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr`` then recommends using +``absl::wrap_unique(new T(...))``. + +Examples: + +.. code-block:: c++ + + class A { + public: +static A* NewA() { return new A(); } + + private: +A() = default; + }; + + std::unique_ptr a; + + // Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + // Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + // Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + // Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,13 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/abseil/WrapUniqueCheck.h
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 194622. Dosi-Dough added a comment. added white space to check doc. Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); +} + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -20,6 +20,7 @@ abseil-string-find-startswith abseil-time-subtraction abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,35 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== + +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr`` then recommends using +``absl::wrap_unique(new T(...))``. + +Examples: + +.. code-block:: c++ + + class A { + public: +static A* NewA() { return new A(); } + + private: +A() = default; + }; + + std::unique_ptr a; + + // Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + // Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + // Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + // Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,13 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/abseil/WrapUniqueCheck.h
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 194608. Dosi-Dough added a comment. fixed docs and white spacing Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); +} + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -20,6 +20,7 @@ abseil-string-find-startswith abseil-time-subtraction abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,33 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr`` then recommends using ``absl::wrap_unique(new T(...))``. + +Examples: + +.. code-block:: c++ + + class A { + public: +static A* NewA() { return new A(); } + + private: +A() = default; + }; + + std::unique_ptr a; + + // Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + // Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + // Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + // Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,13 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/abseil/WrapUniqueCheck.h
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 194618. Dosi-Dough marked 3 inline comments as done. Dosi-Dough added a comment. removed parens from turnary, added whitespace. Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); +} + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -20,6 +20,7 @@ abseil-string-find-startswith abseil-time-subtraction abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,34 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== +Looks for instances of factory functions which uses a non-public constructor + +that returns a ``std::unqiue_ptr`` then recommends using ``absl::wrap_unique(new T(...))``. + +Examples: + +.. code-block:: c++ + + class A { + public: +static A* NewA() { return new A(); } + + private: +A() = default; + }; + + std::unique_ptr a; + + // Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + // Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + // Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + // Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,13 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/abseil/WrapUniqueCheck.h
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 194607. Dosi-Dough marked an inline comment as done. Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); +} + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -20,6 +20,7 @@ abseil-string-find-startswith abseil-time-subtraction abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,32 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr`` then recommends using +``absl::wrap_unique(new T(...))``. + +.. code-block:: c++ + + class A { + public: +static A* NewA() { return new A(); } + + private: +A() = default; + }; + + std::unique_ptr a; + + // Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + // Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + // Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + // Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,13 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/abseil/WrapUniqueCheck.h === --- /dev/null +++
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough added a comment. Hi I was wondering if there were any other changes anyone recomends should be made to this revision? Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough updated this revision to Diff 190552. Dosi-Dough marked 11 inline comments as done. Dosi-Dough added a comment. Herald added a subscriber: jdoerfert. Herald added a project: clang. added style and performance refactoring based on code reviews Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/WrapUniqueCheck.cpp clang-tidy/abseil/WrapUniqueCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-wrap-unique.cpp Index: test/clang-tidy/abseil-wrap-unique.cpp === --- /dev/null +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s abseil-wrap-unique %t + +namespace std { + +template +class default_delete {}; + +template > +class unique_ptr { +public: + unique_ptr() {} + unique_ptr(type *ptr) {} + unique_ptr(const unique_ptr ) = delete; + unique_ptr(unique_ptr &) {} + ~unique_ptr() {} + type *() { return *ptr; } + type *operator->() { return ptr; } + type *release() { return ptr; } + void reset() {} + void reset(type *pt) {} + void reset(type pt) {} + unique_ptr =(unique_ptr &&) { return *this; } + template + unique_ptr =(unique_ptr &&) { return *this; } + +private: + type *ptr; +}; +} // namespace std + +class A { + public: + static A* NewA() { +return new A(); + } + + private: + A() {} +}; + +class B { + public: + static B* NewB(int bIn) { +return new B(); + } + + private: + B() {} +}; + +struct C { + int x; + int y; +}; +/* +std::unique_ptr returnPointer() { + return std::unique_ptr(A::NewA()); +} +*/ +void positives() { + std::unique_ptr a; + a.reset(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: a = absl::WrapUnique(A::NewA()) + + std::unique_ptr b(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto b = absl::WrapUnique(A::NewA()) + + int cIn; + std::unique_ptr c(B::NewB(cIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: Perfer absl::WrapUnique to constructing unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: auto c = absl::WrapUnique(B::NewB(cIn)) + + int dIn; + std::unique_ptr d; + d.reset(B::NewB(dIn)); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: d = absl::WrapUnique(B::NewB(dIn)) + + auto e = std::unique_ptr(A::NewA()); + //CHECK-MESSAGE: :[[@LINE-1]]:3: warning: prefer absl::WrapUnique for resetting unique_ptr [abseil-wrap-unique] + //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) + + //std::unique_ptr e(new int[2] {1,2}); +} + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -20,6 +20,7 @@ abseil-string-find-startswith abseil-time-subtraction abseil-upgrade-duration-conversions + abseil-wrap-unique android-cloexec-accept android-cloexec-accept4 android-cloexec-creat Index: docs/clang-tidy/checks/abseil-wrap-unique.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -0,0 +1,31 @@ +.. title:: clang-tidy - abseil-wrap-unique + +abseil-wrap-unique +== +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr`` then recommends using +``absl::wrap_unique(new T(...))``. + +.. code-block:: c++ + class A { + public: +static A* NewA() { return new A(); } + + private: +A() {} + }; + + std::unique_ptr a; + + // Original - reset called with a static function returning a std::unqiue_ptr + a.reset(A::NewA()); + + // Suggested - reset ptr with absl::WrapUnique + a = absl::WrapUnique(A::NewA()); + + // Original - std::unique_ptr initialized with static function + std::unique_ptr b(A::NewA()); + + // Suggested - initialize with absl::WrapUnique instead + auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,13 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`abseil-wrap-unique + ` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr`` then recommends using + ``absl::wrap_unique(new T(...))``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough added a comment. Gentle ping Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough added a comment. gentle ping Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D57435: [clang-tidy] Add abseil-wrap-unique check
Dosi-Dough added a comment. @EricWF @JonasToth I was wondering what other changes I should make to submit my check upstream? Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits