Skip to content

Commit 547f89d

Browse files
committed
[clang-tidy] Ignore a case where the fix of make_unique check introduces side effect.
Summary: Previously, ptr.reset(new char[5]) will be replaced with `p = make_unique<char[]>(5)`, the fix has side effect -- doing default initialization, it may cause performace regression (we are bitten by this rececntly) The check should be conservative for these cases. Reviewers: alexfh Subscribers: xazax.hun, cfe-commits Differential Revision: https://reviews.llvm.org/D53377 llvm-svn: 344733
1 parent 5ee0188 commit 547f89d

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ void MakeSmartPtrCheck::check(const MatchFinder::MatchResult &Result) {
121121
if (New->getNumPlacementArgs() != 0)
122122
return;
123123

124+
// Be conservative for cases where we construct an array without any
125+
// initalization.
126+
// For example,
127+
// P.reset(new int[5]) // check fix: P = make_unique<int []>(5)
128+
//
129+
// The fix of the check has side effect, it introduces default initialization
130+
// which maybe unexpected and cause performance regression.
131+
if (New->isArray() && !New->hasInitializer())
132+
return;
124133
if (Construct)
125134
checkConstruct(SM, Result.Context, Construct, Type, New);
126135
else if (Reset)

clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,18 +403,15 @@ void initialization(int T, Base b) {
403403
// CHECK-FIXES: FFs = std::make_unique<Foo[]>(Num2);
404404

405405
std::unique_ptr<int[]> FI;
406-
FI.reset(new int[5]);
407-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
408-
// CHECK-FIXES: FI = std::make_unique<int[]>(5);
409-
FI.reset(new int[5]());
406+
FI.reset(new int[5]()); // default initialization.
410407
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
411408
// CHECK-FIXES: FI = std::make_unique<int[]>(5);
409+
410+
// The check doesn't give warnings and fixes for cases where the original new
411+
// expresion doesn't do any initialization.
412+
FI.reset(new int[5]);
412413
FI.reset(new int[Num]);
413-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
414-
// CHECK-FIXES: FI = std::make_unique<int[]>(Num);
415414
FI.reset(new int[Num2]);
416-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
417-
// CHECK-FIXES: FI = std::make_unique<int[]>(Num2);
418415
}
419416

420417
void aliases() {

0 commit comments

Comments
 (0)