From be5d4487b45e7ec729f4ddfab049289a91d3a7ba Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 9 Aug 2017 17:03:42 +0000 Subject: [PATCH] [clang-tidy] Fix another crash in make-unique check. Summary: The crash happens when calling `reset` method without any preceding operation like "->" or ".", this could happen in a subclass of the "std::unique_ptr". Reviewers: alexfh Reviewed By: alexfh Subscribers: JDevlieghere, xazax.hun, cfe-commits Differential Revision: https://reviews.llvm.org/D36452 llvm-svn: 310496 --- .../clang-tidy/modernize/MakeSmartPtrCheck.cpp | 7 +++++++ clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp index c73462c..a164df9 100644 --- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -199,6 +199,13 @@ void MakeSmartPtrCheck::checkReset(SourceManager &SM, return; } + // There are some cases where we don't have operator ("." or "->") of the + // "reset" expression, e.g. call "reset()" method directly in the subclass of + // "std::unique_ptr<>". We skip these cases. + if (OperatorLoc.isInvalid()) { + return; + } + auto Diag = diag(ResetCallStart, "use %0 instead") << MakeSmartPtrFunctionName; diff --git a/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp b/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp index f1264cb..ea1db0c 100644 --- a/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp +++ b/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp @@ -415,3 +415,16 @@ void macro() { g2(t); } #undef DEFINE + +class UniqueFoo : public std::unique_ptr { + public: + void foo() { + reset(new Foo); + this->reset(new Foo); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use std::make_unique instead + // CHECK-FIXES: *this = std::make_unique(); + (*this).reset(new Foo); + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead + // CHECK-FIXES: (*this) = std::make_unique(); + } +}; -- 2.7.4