Skip to content

Commit 47d5e94

Browse files
authored
[clang-tidy] readability-redundant-smartptr-get: disable for smart pointers to arrays (#141092)
Currently we generate an incorrect suggestion for shared/unique pointers to arrays; for instance ([Godbolt](https://godbolt.org/z/Tens1reGP)): ```c++ #include <memory> void test_shared_ptr_to_array() { std::shared_ptr<int[]> i; auto s = sizeof(*i.get()); } ``` ``` <source>:5:20: warning: redundant get() call on smart pointer [readability-redundant-smartptr-get] 5 | auto s = sizeof(*i.get()); | ^~~~~~~ | i 1 warning generated. ``` `sizeof(*i)` is incorrect, though, because the array specialization of `std::shared/unique_ptr` does not have an `operator*()`. Therefore I have disabled this check for smart pointers to arrays for now; future work could, of course, improve on this by suggesting, say, `sizeof(i[0])` in the above example.
1 parent 94929b7 commit 47d5e94

File tree

3 files changed

+111
-13
lines changed

3 files changed

+111
-13
lines changed

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,41 @@ internal::Matcher<Decl> knownSmartptr() {
4949

5050
void registerMatchersForGetArrowStart(MatchFinder *Finder,
5151
MatchFinder::MatchCallback *Callback) {
52-
const auto QuacksLikeASmartptr = recordDecl(
53-
recordDecl().bind("duck_typing"),
54-
has(cxxMethodDecl(hasName("operator->"),
55-
returns(qualType(pointsTo(type().bind("op->Type")))))),
56-
has(cxxMethodDecl(hasName("operator*"), returns(qualType(references(
57-
type().bind("op*Type")))))));
52+
const auto MatchesOpArrow =
53+
allOf(hasName("operator->"),
54+
returns(qualType(pointsTo(type().bind("op->Type")))));
55+
const auto MatchesOpStar =
56+
allOf(hasName("operator*"),
57+
returns(qualType(references(type().bind("op*Type")))));
58+
const auto HasRelevantOps =
59+
allOf(anyOf(hasMethod(MatchesOpArrow),
60+
has(functionTemplateDecl(has(functionDecl(MatchesOpArrow))))),
61+
anyOf(hasMethod(MatchesOpStar),
62+
has(functionTemplateDecl(has(functionDecl(MatchesOpStar))))));
63+
64+
const auto QuacksLikeASmartptr =
65+
cxxRecordDecl(cxxRecordDecl().bind("duck_typing"), HasRelevantOps);
5866

5967
// Make sure we are not missing the known standard types.
60-
const auto Smartptr = anyOf(knownSmartptr(), QuacksLikeASmartptr);
68+
const auto SmartptrAny = anyOf(knownSmartptr(), QuacksLikeASmartptr);
69+
const auto SmartptrWithDeref =
70+
anyOf(cxxRecordDecl(knownSmartptr(), HasRelevantOps), QuacksLikeASmartptr);
6171

6272
// Catch 'ptr.get()->Foo()'
63-
Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(),
64-
hasObjectExpression(callToGet(Smartptr))),
65-
Callback);
73+
Finder->addMatcher(
74+
memberExpr(expr().bind("memberExpr"), isArrow(),
75+
hasObjectExpression(callToGet(SmartptrWithDeref))),
76+
Callback);
6677

6778
// Catch '*ptr.get()' or '*ptr->get()'
6879
Finder->addMatcher(
69-
unaryOperator(hasOperatorName("*"), hasUnaryOperand(callToGet(Smartptr))),
80+
unaryOperator(hasOperatorName("*"),
81+
hasUnaryOperand(callToGet(SmartptrWithDeref))),
7082
Callback);
7183

7284
// Catch '!ptr.get()'
7385
const auto CallToGetAsBool = callToGet(
74-
recordDecl(Smartptr, has(cxxConversionDecl(returns(booleanType())))));
86+
recordDecl(SmartptrAny, has(cxxConversionDecl(returns(booleanType())))));
7587
Finder->addMatcher(
7688
unaryOperator(hasOperatorName("!"), hasUnaryOperand(CallToGetAsBool)),
7789
Callback);
@@ -84,7 +96,7 @@ void registerMatchersForGetArrowStart(MatchFinder *Finder,
8496
Callback);
8597

8698
Finder->addMatcher(cxxDependentScopeMemberExpr(hasObjectExpression(
87-
callExpr(has(callToGet(Smartptr))).bind("obj"))),
99+
callExpr(has(callToGet(SmartptrAny))))),
88100
Callback);
89101
}
90102

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get-msvc.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ struct unique_ptr {
1616
explicit operator bool() const noexcept;
1717
};
1818

19+
template <typename T>
20+
struct unique_ptr<T[]> {
21+
template <typename T2 = T>
22+
T2* operator[](unsigned) const;
23+
T* get() const;
24+
explicit operator bool() const noexcept;
25+
};
26+
1927
template <typename T>
2028
struct shared_ptr {
2129
template <typename T2 = T>
@@ -26,6 +34,14 @@ struct shared_ptr {
2634
explicit operator bool() const noexcept;
2735
};
2836

37+
template <typename T>
38+
struct shared_ptr<T[]> {
39+
template <typename T2 = T>
40+
T2* operator[](unsigned) const;
41+
T* get() const;
42+
explicit operator bool() const noexcept;
43+
};
44+
2945
} // namespace std
3046

3147
struct Bar {
@@ -92,3 +108,31 @@ void Positive() {
92108
// CHECK-MESSAGES: if (NULL == x.get());
93109
// CHECK-FIXES: if (NULL == x);
94110
}
111+
112+
void test_smart_ptr_to_array() {
113+
std::unique_ptr<int[]> i;
114+
// The array specialization does not have operator*(), so make sure
115+
// we do not incorrectly suggest sizeof(*i) here.
116+
// FIXME: alternatively, we could suggest sizeof(i[0])
117+
auto sz = sizeof(*i.get());
118+
119+
std::shared_ptr<Bar[]> s;
120+
// The array specialization does not have operator->() either
121+
s.get()->Do();
122+
123+
bool b1 = !s.get();
124+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant get() call
125+
// CHECK-FIXES: bool b1 = !s;
126+
127+
if (s.get()) {}
128+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
129+
// CHECK-FIXES: if (s) {}
130+
131+
int x = s.get() ? 1 : 2;
132+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant get() call
133+
// CHECK-FIXES: int x = s ? 1 : 2;
134+
135+
bool b2 = s.get() == nullptr;
136+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
137+
// CHECK-FIXES: bool b2 = s == nullptr;
138+
}

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ struct unique_ptr {
1212
explicit operator bool() const noexcept;
1313
};
1414

15+
template <typename T>
16+
struct unique_ptr<T[]> {
17+
T& operator[](unsigned) const;
18+
T* get() const;
19+
explicit operator bool() const noexcept;
20+
};
21+
1522
template <typename T>
1623
struct shared_ptr {
1724
T& operator*() const;
@@ -20,6 +27,13 @@ struct shared_ptr {
2027
explicit operator bool() const noexcept;
2128
};
2229

30+
template <typename T>
31+
struct shared_ptr<T[]> {
32+
T& operator[](unsigned) const;
33+
T* get() const;
34+
explicit operator bool() const noexcept;
35+
};
36+
2337
template <typename T>
2438
struct vector {
2539
vector();
@@ -283,3 +297,31 @@ void test_redundant_get_with_member() {
283297
// CHECK-FIXES: f(**i->get()->getValue());
284298
}
285299
}
300+
301+
void test_smart_ptr_to_array() {
302+
std::unique_ptr<int[]> i;
303+
// The array specialization does not have operator*(), so make sure
304+
// we do not incorrectly suggest sizeof(*i) here.
305+
// FIXME: alternatively, we could suggest sizeof(i[0])
306+
auto sz = sizeof(*i.get());
307+
308+
std::shared_ptr<Inner[]> s;
309+
// The array specialization does not have operator->() either
310+
s.get()->getValue();
311+
312+
bool b1 = !s.get();
313+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant get() call
314+
// CHECK-FIXES: bool b1 = !s;
315+
316+
if (s.get()) {}
317+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
318+
// CHECK-FIXES: if (s) {}
319+
320+
int x = s.get() ? 1 : 2;
321+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant get() call
322+
// CHECK-FIXES: int x = s ? 1 : 2;
323+
324+
bool b2 = s.get() == nullptr;
325+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
326+
// CHECK-FIXES: bool b2 = s == nullptr;
327+
}

0 commit comments

Comments
 (0)