diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp index dd6866f5a326b..1abd70bd048a4 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp @@ -51,6 +51,20 @@ void registerMatchersForGetArrowStart(MatchFinder *Finder, unaryOperator(hasOperatorName("*"), hasUnaryOperand(callToGet(QuacksLikeASmartptr))), Callback); + + // Catch '!ptr.get()' + const auto CallToGetAsBool = ignoringParenImpCasts(callToGet(recordDecl( + QuacksLikeASmartptr, has(cxxConversionDecl(returns(booleanType())))))); + Finder->addMatcher( + unaryOperator(hasOperatorName("!"), hasUnaryOperand(CallToGetAsBool)), + Callback); + + // Catch 'if(ptr.get())' + Finder->addMatcher(ifStmt(hasCondition(CallToGetAsBool)), Callback); + + // Catch 'ptr.get() ? X : Y' + Finder->addMatcher(conditionalOperator(hasCondition(CallToGetAsBool)), + Callback); } void registerMatchersForGetEquals(MatchFinder *Finder, @@ -72,11 +86,6 @@ void registerMatchersForGetEquals(MatchFinder *Finder, hasEitherOperand(callToGet(IsAKnownSmartptr))), Callback); - // Matches against if(ptr.get()) - Finder->addMatcher( - ifStmt(hasCondition(ignoringImpCasts(callToGet(IsAKnownSmartptr)))), - Callback); - // FIXME: Match and fix if (l.get() == r.get()). } diff --git a/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get.cpp b/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get.cpp index ce4c34be4061e..685a57cc681dd 100644 --- a/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get.cpp +++ b/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get.cpp @@ -9,6 +9,7 @@ struct unique_ptr { T& operator*() const; T* operator->() const; T* get() const; + explicit operator bool() const noexcept; }; template @@ -16,6 +17,7 @@ struct shared_ptr { T& operator*() const; T* operator->() const; T* get() const; + explicit operator bool() const noexcept; }; } // namespace std @@ -28,6 +30,7 @@ struct BarPtr { Bar* operator->(); Bar& operator*(); Bar* get(); + explicit operator bool() const; }; struct int_ptr { int* get(); @@ -110,6 +113,23 @@ void Positive() { // CHECK-MESSAGES: uu.get() == nullptr; // CHECK-FIXES: bool bb = uu == nullptr; + if (up->get()); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call + // CHECK-MESSAGES: if (up->get()); + // CHECK-FIXES: if (*up); + if ((uu.get())); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call + // CHECK-MESSAGES: if ((uu.get())); + // CHECK-FIXES: if ((uu)); + bb = !ss->get(); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant get() call + // CHECK-MESSAGES: bb = !ss->get(); + // CHECK-FIXES: bb = !*ss; + bb = u.get() ? true : false; + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call + // CHECK-MESSAGES: bb = u.get() ? true : false; + // CHECK-FIXES: bb = u ? true : false; + bb = nullptr != ss->get(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: redundant get() call // CHECK-MESSAGES: nullptr != ss->get(); @@ -146,10 +166,6 @@ void Positive() { // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant get() call // CHECK-MESSAGES: if (NULL == x.get()); // CHECK-FIXES: if (NULL == x); - if (x.get()); - // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call - // CHECK-MESSAGES: if (x.get()); - // CHECK-FIXES: if (x); } void Negative() { @@ -175,4 +191,6 @@ void Negative() { int_ptr ip; bool bb = ip.get() == nullptr; + bb = !ip.get(); + bb = ip.get() ? true : false; }