Index: clang-tidy/readability/RedundantSmartptrGetCheck.cpp =================================================================== --- clang-tidy/readability/RedundantSmartptrGetCheck.cpp +++ clang-tidy/readability/RedundantSmartptrGetCheck.cpp @@ -51,6 +51,20 @@ 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 @@ 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()). } Index: test/clang-tidy/readability-redundant-smartptr-get.cpp =================================================================== --- test/clang-tidy/readability-redundant-smartptr-get.cpp +++ test/clang-tidy/readability-redundant-smartptr-get.cpp @@ -9,6 +9,7 @@ T& operator*() const; T* operator->() const; T* get() const; + explicit operator bool() const noexcept; }; template @@ -16,6 +17,7 @@ T& operator*() const; T* operator->() const; T* get() const; + explicit operator bool() const noexcept; }; } // namespace std @@ -28,6 +30,7 @@ Bar* operator->(); Bar& operator*(); Bar* get(); + explicit operator bool() const; }; struct int_ptr { int* get(); @@ -110,6 +113,23 @@ // 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 @@ // 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 @@ int_ptr ip; bool bb = ip.get() == nullptr; + bb = !ip.get(); + bb = ip.get() ? true : false; }