Skip to content

Commit

Permalink
[clang-tidy] Expand readability-redundant-smartptr-get to understand …
Browse files Browse the repository at this point in the history
…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

llvm-svn: 322497
  • Loading branch information
sbenzaquen committed Jan 15, 2018
1 parent f630047 commit c814872
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
Expand Up @@ -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,
Expand All @@ -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()).
}

Expand Down
Expand Up @@ -9,13 +9,15 @@ struct unique_ptr {
T& operator*() const;
T* operator->() const;
T* get() const;
explicit operator bool() const noexcept;
};

template <typename T>
struct shared_ptr {
T& operator*() const;
T* operator->() const;
T* get() const;
explicit operator bool() const noexcept;
};

} // namespace std
Expand All @@ -28,6 +30,7 @@ struct BarPtr {
Bar* operator->();
Bar& operator*();
Bar* get();
explicit operator bool() const;
};
struct int_ptr {
int* get();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand All @@ -175,4 +191,6 @@ void Negative() {

int_ptr ip;
bool bb = ip.get() == nullptr;
bb = !ip.get();
bb = ip.get() ? true : false;
}

0 comments on commit c814872

Please sign in to comment.