[clang-tools-extra] r322497 - [clang-tidy] Expand readability-redundant-smartptr-get to understand implicit converions to bool in more contexts.
Samuel Benzaquen via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 15 10:03:20 PST 2018
Author: sbenza
Date: Mon Jan 15 10:03:20 2018
New Revision: 322497
URL: http://llvm.org/viewvc/llvm-project?rev=322497&view=rev
Log:
[clang-tidy] Expand readability-redundant-smartptr-get to understand implicit converions to bool in more contexts.
Summary: Expand readability-redundant-smartptr-get to understand implicit converions to bool in more contexts.
Reviewers: hokein
Subscribers: klimek, xazax.hun, cfe-commits
Differential Revision: https://reviews.llvm.org/D41998
Modified:
clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp
Modified: clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp?rev=322497&r1=322496&r2=322497&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp Mon Jan 15 10:03:20 2018
@@ -51,6 +51,20 @@ void registerMatchersForGetArrowStart(Ma
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(MatchF
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()).
}
Modified: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp?rev=322497&r1=322496&r2=322497&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp Mon Jan 15 10:03:20 2018
@@ -9,6 +9,7 @@ struct unique_ptr {
T& operator*() const;
T* operator->() const;
T* get() const;
+ explicit operator bool() const noexcept;
};
template <typename T>
@@ -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;
}
More information about the cfe-commits
mailing list