[PATCH] D54745: [clang-tidy] Don't generate incorrect fixes for class with deleted copy constructor in smart_ptr check.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 20 02:06:57 PST 2018


hokein created this revision.
hokein added reviewers: JonasToth, aaron.ballman.
Herald added a subscriber: xazax.hun.

The fix for aggregate initialization (`std::make_unique<Foo>(Foo {1, 2})` needs
to see Foo copy constructor, otherwise we will have a compiler error. So we
only emit the check warning.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D54745

Files:
  clang-tidy/modernize/MakeSmartPtrCheck.cpp
  test/clang-tidy/modernize-make-unique.cpp


Index: test/clang-tidy/modernize-make-unique.cpp
===================================================================
--- test/clang-tidy/modernize-make-unique.cpp
+++ test/clang-tidy/modernize-make-unique.cpp
@@ -32,6 +32,10 @@
 
 struct Empty {};
 
+struct NoCopyCtor {
+  NoCopyCtor(const NoCopyCtor&) = delete;
+};
+
 struct E {
   E(std::initializer_list<int>);
   E();
@@ -270,6 +274,11 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead
   // CHECK-FIXES: std::unique_ptr<Empty> PEmpty = std::make_unique<Empty>(Empty{});
 
+  // No fixes for classes with deleted copy constructor.
+  std::unique_ptr<NoCopyCtor> PNoCopyCtor = std::unique_ptr<NoCopyCtor>(new NoCopyCtor{});
+  // CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use std::make_unique instead
+  // CHECK-FIXES: std::unique_ptr<NoCopyCtor> PNoCopyCtor = std::unique_ptr<NoCopyCtor>(new NoCopyCtor{});
+
   // Initialization with default constructor.
   std::unique_ptr<E> PE1 = std::unique_ptr<E>(new E{});
   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
Index: clang-tidy/modernize/MakeSmartPtrCheck.cpp
===================================================================
--- clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -375,6 +375,17 @@
       //   smart_ptr<Pair>(new Pair{first, second});
       // Has to be replaced with:
       //   smart_ptr<Pair>(Pair{first, second});
+      //
+      // The fix (std::make_unique) requires to see the copy constructor of
+      // Pair, so we don't generate fix if the copy consturctor is not visible
+      // or deleted.
+      if (const auto *RD = New->getType()->getPointeeCXXRecordDecl()) {
+        for (const auto *Ctor : RD->ctors()) {
+          if (Ctor->isCopyConstructor() &&
+              (Ctor->isDeleted() || Ctor->getAccess() == AS_private))
+            return false;
+        }
+      }
       InitRange = SourceRange(
           New->getAllocatedTypeSourceInfo()->getTypeLoc().getBeginLoc(),
           New->getInitializer()->getSourceRange().getEnd());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54745.174736.patch
Type: text/x-patch
Size: 2089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181120/1e6ca052/attachment.bin>


More information about the cfe-commits mailing list