Skip to content

Commit e25a0e9

Browse files
author
Florian Gross
committed
Fixed: Duck-typing in readability-redundant-smartptr-get didn't catch MSVC STL smart pointers.
Differential Revision: https://reviews.llvm.org/D61209 llvm-svn: 359801
1 parent a558ee8 commit e25a0e9

File tree

2 files changed

+110
-13
lines changed

2 files changed

+110
-13
lines changed

clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) {
3030
.bind("redundant_get");
3131
}
3232

33+
internal::Matcher<Decl> knownSmartptr() {
34+
return recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr"));
35+
}
36+
3337
void registerMatchersForGetArrowStart(MatchFinder *Finder,
3438
MatchFinder::MatchCallback *Callback) {
3539
const auto QuacksLikeASmartptr = recordDecl(
@@ -39,21 +43,23 @@ void registerMatchersForGetArrowStart(MatchFinder *Finder,
3943
has(cxxMethodDecl(hasName("operator*"), returns(qualType(references(
4044
type().bind("op*Type")))))));
4145

46+
// Make sure we are not missing the known standard types.
47+
const auto Smartptr = anyOf(knownSmartptr(), QuacksLikeASmartptr);
48+
4249
// Catch 'ptr.get()->Foo()'
43-
Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(),
44-
hasObjectExpression(ignoringImpCasts(
45-
callToGet(QuacksLikeASmartptr)))),
46-
Callback);
50+
Finder->addMatcher(
51+
memberExpr(expr().bind("memberExpr"), isArrow(),
52+
hasObjectExpression(ignoringImpCasts(callToGet(Smartptr)))),
53+
Callback);
4754

4855
// Catch '*ptr.get()' or '*ptr->get()'
4956
Finder->addMatcher(
50-
unaryOperator(hasOperatorName("*"),
51-
hasUnaryOperand(callToGet(QuacksLikeASmartptr))),
57+
unaryOperator(hasOperatorName("*"), hasUnaryOperand(callToGet(Smartptr))),
5258
Callback);
5359

5460
// Catch '!ptr.get()'
55-
const auto CallToGetAsBool = ignoringParenImpCasts(callToGet(recordDecl(
56-
QuacksLikeASmartptr, has(cxxConversionDecl(returns(booleanType()))))));
61+
const auto CallToGetAsBool = ignoringParenImpCasts(callToGet(
62+
recordDecl(Smartptr, has(cxxConversionDecl(returns(booleanType()))))));
5763
Finder->addMatcher(
5864
unaryOperator(hasOperatorName("!"), hasUnaryOperand(CallToGetAsBool)),
5965
Callback);
@@ -71,18 +77,15 @@ void registerMatchersForGetEquals(MatchFinder *Finder,
7177
// This one is harder to do with duck typing.
7278
// The operator==/!= that we are looking for might be member or non-member,
7379
// might be on global namespace or found by ADL, might be a template, etc.
74-
// For now, lets keep a list of known standard types.
75-
76-
const auto IsAKnownSmartptr =
77-
recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr"));
80+
// For now, lets keep it to the known standard types.
7881

7982
// Matches against nullptr.
8083
Finder->addMatcher(
8184
binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
8285
hasEitherOperand(ignoringImpCasts(
8386
anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
8487
integerLiteral(equals(0))))),
85-
hasEitherOperand(callToGet(IsAKnownSmartptr))),
88+
hasEitherOperand(callToGet(knownSmartptr()))),
8689
Callback);
8790

8891
// FIXME: Match and fix if (l.get() == r.get()).
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t
2+
3+
#define NULL __null
4+
5+
namespace std {
6+
7+
// MSVC headers define operator templates instead of plain operators.
8+
9+
template <typename T>
10+
struct unique_ptr {
11+
template <typename T2 = T>
12+
T2& operator*() const;
13+
template <typename T2 = T>
14+
T2* operator->() const;
15+
T* get() const;
16+
explicit operator bool() const noexcept;
17+
};
18+
19+
template <typename T>
20+
struct shared_ptr {
21+
template <typename T2 = T>
22+
T2& operator*() const;
23+
template <typename T2 = T>
24+
T2* operator->() const;
25+
T* get() const;
26+
explicit operator bool() const noexcept;
27+
};
28+
29+
} // namespace std
30+
31+
struct Bar {
32+
void Do();
33+
void ConstDo() const;
34+
};
35+
36+
void Positive() {
37+
std::unique_ptr<Bar>* up;
38+
(*up->get()).Do();
39+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
40+
// CHECK-MESSAGES: (*up->get()).Do();
41+
// CHECK-FIXES: (**up).Do();
42+
43+
std::unique_ptr<int> uu;
44+
std::shared_ptr<double> *ss;
45+
bool bb = uu.get() == nullptr;
46+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
47+
// CHECK-MESSAGES: uu.get() == nullptr;
48+
// CHECK-FIXES: bool bb = uu == nullptr;
49+
50+
if (up->get());
51+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
52+
// CHECK-MESSAGES: if (up->get());
53+
// CHECK-FIXES: if (*up);
54+
if ((uu.get()));
55+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
56+
// CHECK-MESSAGES: if ((uu.get()));
57+
// CHECK-FIXES: if ((uu));
58+
bb = !ss->get();
59+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant get() call
60+
// CHECK-MESSAGES: bb = !ss->get();
61+
// CHECK-FIXES: bb = !*ss;
62+
63+
bb = nullptr != ss->get();
64+
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: redundant get() call
65+
// CHECK-MESSAGES: nullptr != ss->get();
66+
// CHECK-FIXES: bb = nullptr != *ss;
67+
68+
bb = std::unique_ptr<int>().get() == NULL;
69+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
70+
// CHECK-MESSAGES: bb = std::unique_ptr<int>().get() == NULL;
71+
// CHECK-FIXES: bb = std::unique_ptr<int>() == NULL;
72+
bb = ss->get() == NULL;
73+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
74+
// CHECK-MESSAGES: bb = ss->get() == NULL;
75+
// CHECK-FIXES: bb = *ss == NULL;
76+
77+
std::unique_ptr<int> x, y;
78+
if (x.get() == nullptr);
79+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
80+
// CHECK-MESSAGES: if (x.get() == nullptr);
81+
// CHECK-FIXES: if (x == nullptr);
82+
if (nullptr == y.get());
83+
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: redundant get() call
84+
// CHECK-MESSAGES: if (nullptr == y.get());
85+
// CHECK-FIXES: if (nullptr == y);
86+
if (x.get() == NULL);
87+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
88+
// CHECK-MESSAGES: if (x.get() == NULL);
89+
// CHECK-FIXES: if (x == NULL);
90+
if (NULL == x.get());
91+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant get() call
92+
// CHECK-MESSAGES: if (NULL == x.get());
93+
// CHECK-FIXES: if (NULL == x);
94+
}

0 commit comments

Comments
 (0)