[PATCH] D36786: [clang-tidy] Don't generate fixes for initializer_list constructor in make_unique check.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 16 01:59:22 PDT 2017


hokein created this revision.
Herald added subscribers: xazax.hun, JDevlieghere.

The current fix will break the compilation -- because braced list is not
deducible in std::make_unique (with the use of forwarding) without
specifying the type explicitly.

We could support it in the future.


https://reviews.llvm.org/D36786

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
@@ -250,7 +250,6 @@
   // Initialization with the initializer-list constructor.
   std::unique_ptr<E> PE2 = std::unique_ptr<E>(new E{1, 2});
   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
-  // CHECK-FIXES: std::unique_ptr<E> PE2 = std::make_unique<E>({1, 2});
 
   // Initialization with default constructor.
   std::unique_ptr<F> PF1 = std::unique_ptr<F>(new F());
@@ -265,33 +264,27 @@
   // Initialization with the initializer-list constructor.
   std::unique_ptr<F> PF3 = std::unique_ptr<F>(new F{1});
   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
-  // CHECK-FIXES: std::unique_ptr<F> PF3 = std::make_unique<F>({1});
 
   // Initialization with the initializer-list constructor.
   std::unique_ptr<F> PF4 = std::unique_ptr<F>(new F{1, 2});
   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
-  // CHECK-FIXES: std::unique_ptr<F> PF4 = std::make_unique<F>({1, 2});
 
   // Initialization with the initializer-list constructor.
   std::unique_ptr<F> PF5 = std::unique_ptr<F>(new F({1, 2}));
   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
-  // CHECK-FIXES: std::unique_ptr<F> PF5 = std::make_unique<F>({1, 2});
 
   // Initialization with the initializer-list constructor as the default
   // constructor is not present.
   std::unique_ptr<G> PG1 = std::unique_ptr<G>(new G{});
   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
-  // CHECK-FIXES: std::unique_ptr<G> PG1 = std::make_unique<G>({});
 
   // Initialization with the initializer-list constructor.
   std::unique_ptr<G> PG2 = std::unique_ptr<G>(new G{1});
   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
-  // CHECK-FIXES: std::unique_ptr<G> PG2 = std::make_unique<G>({1});
 
   // Initialization with the initializer-list constructor.
   std::unique_ptr<G> PG3 = std::unique_ptr<G>(new G{1, 2});
   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
-  // CHECK-FIXES: std::unique_ptr<G> PG3 = std::make_unique<G>({1, 2});
 
   std::unique_ptr<Foo> FF = std::unique_ptr<Foo>(new Foo());
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning:
Index: clang-tidy/modernize/MakeSmartPtrCheck.cpp
===================================================================
--- clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -274,19 +274,17 @@
     SourceRange InitRange;
     if (const auto *NewConstruct = New->getConstructExpr()) {
       if (NewConstruct->isStdInitListInitialization()) {
-        // Direct Initialization with the initializer-list constructor.
-        //   struct S { S(std::initializer_list<T>); };
+        // FIXME: Add fixes for direct initialization with the initializer-list
+        // constructor. Unlike ordinal cases, braced list can not be deduced in
+        // std::make_smart_ptr, we need to specify the type explicitly in the
+        // fixes, see below.
+        //   struct S { S(std::initializer_list<int>); };
         //   smart_ptr<S>(new S{1, 2, 3});
         //   smart_ptr<S>(new S{}); // use initializer-list consturctor
         // The brace has to be kept, so this has to be replaced with:
-        //   std::make_smart_ptr<S>({1, 2, 3});
-        //   std::make_smart_ptr<S>({});
-        unsigned NumArgs = NewConstruct->getNumArgs();
-        if (NumArgs == 0) {
-          return;
-        }
-        InitRange = SourceRange(NewConstruct->getArg(0)->getLocStart(),
-                                NewConstruct->getArg(NumArgs - 1)->getLocEnd());
+        //   std::make_smart_ptr<S>(std::initializer_list<int>({1, 2, 3}));
+        //   std::make_smart_ptr<S>(std::initializer_list<int>({}));
+        return;
       } else {
         // Direct initialization with ordinary constructors.
         //   struct S { S(int x); S(); };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36786.111320.patch
Type: text/x-patch
Size: 4083 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170816/adcb6b47/attachment-0001.bin>


More information about the cfe-commits mailing list