Skip to content

[clang-tidy] readability-redundant-smartptr-get: disable for smart pointers to arrays #141092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,41 @@ internal::Matcher<Decl> knownSmartptr() {

void registerMatchersForGetArrowStart(MatchFinder *Finder,
MatchFinder::MatchCallback *Callback) {
const auto QuacksLikeASmartptr = recordDecl(
recordDecl().bind("duck_typing"),
has(cxxMethodDecl(hasName("operator->"),
returns(qualType(pointsTo(type().bind("op->Type")))))),
has(cxxMethodDecl(hasName("operator*"), returns(qualType(references(
type().bind("op*Type")))))));
const auto MatchesOpArrow =
allOf(hasName("operator->"),
returns(qualType(pointsTo(type().bind("op->Type")))));
const auto MatchesOpStar =
allOf(hasName("operator*"),
returns(qualType(references(type().bind("op*Type")))));
const auto HasRelevantOps =
allOf(anyOf(hasMethod(MatchesOpArrow),
has(functionTemplateDecl(has(functionDecl(MatchesOpArrow))))),
anyOf(hasMethod(MatchesOpStar),
has(functionTemplateDecl(has(functionDecl(MatchesOpStar))))));

const auto QuacksLikeASmartptr =
cxxRecordDecl(cxxRecordDecl().bind("duck_typing"), HasRelevantOps);

// Make sure we are not missing the known standard types.
const auto Smartptr = anyOf(knownSmartptr(), QuacksLikeASmartptr);
const auto SmartptrAny = anyOf(knownSmartptr(), QuacksLikeASmartptr);
const auto SmartptrWithDeref =
anyOf(cxxRecordDecl(knownSmartptr(), HasRelevantOps), QuacksLikeASmartptr);

// Catch 'ptr.get()->Foo()'
Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(),
hasObjectExpression(callToGet(Smartptr))),
Callback);
Finder->addMatcher(
memberExpr(expr().bind("memberExpr"), isArrow(),
hasObjectExpression(callToGet(SmartptrWithDeref))),
Callback);

// Catch '*ptr.get()' or '*ptr->get()'
Finder->addMatcher(
unaryOperator(hasOperatorName("*"), hasUnaryOperand(callToGet(Smartptr))),
unaryOperator(hasOperatorName("*"),
hasUnaryOperand(callToGet(SmartptrWithDeref))),
Callback);

// Catch '!ptr.get()'
const auto CallToGetAsBool = callToGet(
recordDecl(Smartptr, has(cxxConversionDecl(returns(booleanType())))));
recordDecl(SmartptrAny, has(cxxConversionDecl(returns(booleanType())))));
Finder->addMatcher(
unaryOperator(hasOperatorName("!"), hasUnaryOperand(CallToGetAsBool)),
Callback);
Expand All @@ -84,7 +96,7 @@ void registerMatchersForGetArrowStart(MatchFinder *Finder,
Callback);

Finder->addMatcher(cxxDependentScopeMemberExpr(hasObjectExpression(
callExpr(has(callToGet(Smartptr))).bind("obj"))),
callExpr(has(callToGet(SmartptrAny))))),
Callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ struct unique_ptr {
explicit operator bool() const noexcept;
};

template <typename T>
struct unique_ptr<T[]> {
template <typename T2 = T>
T2* operator[](unsigned) const;
T* get() const;
explicit operator bool() const noexcept;
};

template <typename T>
struct shared_ptr {
template <typename T2 = T>
Expand All @@ -26,6 +34,14 @@ struct shared_ptr {
explicit operator bool() const noexcept;
};

template <typename T>
struct shared_ptr<T[]> {
template <typename T2 = T>
T2* operator[](unsigned) const;
T* get() const;
explicit operator bool() const noexcept;
};

} // namespace std

struct Bar {
Expand Down Expand Up @@ -92,3 +108,31 @@ void Positive() {
// CHECK-MESSAGES: if (NULL == x.get());
// CHECK-FIXES: if (NULL == x);
}

void test_smart_ptr_to_array() {
std::unique_ptr<int[]> i;
// The array specialization does not have operator*(), so make sure
// we do not incorrectly suggest sizeof(*i) here.
// FIXME: alternatively, we could suggest sizeof(i[0])
auto sz = sizeof(*i.get());

std::shared_ptr<Bar[]> s;
// The array specialization does not have operator->() either
s.get()->Do();

bool b1 = !s.get();
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant get() call
// CHECK-FIXES: bool b1 = !s;

if (s.get()) {}
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
// CHECK-FIXES: if (s) {}

int x = s.get() ? 1 : 2;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant get() call
// CHECK-FIXES: int x = s ? 1 : 2;

bool b2 = s.get() == nullptr;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
// CHECK-FIXES: bool b2 = s == nullptr;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ struct unique_ptr {
explicit operator bool() const noexcept;
};

template <typename T>
struct unique_ptr<T[]> {
T& operator[](unsigned) const;
T* get() const;
explicit operator bool() const noexcept;
};

template <typename T>
struct shared_ptr {
T& operator*() const;
Expand All @@ -20,6 +27,13 @@ struct shared_ptr {
explicit operator bool() const noexcept;
};

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

template <typename T>
struct vector {
vector();
Expand Down Expand Up @@ -283,3 +297,31 @@ void test_redundant_get_with_member() {
// CHECK-FIXES: f(**i->get()->getValue());
}
}

void test_smart_ptr_to_array() {
std::unique_ptr<int[]> i;
// The array specialization does not have operator*(), so make sure
// we do not incorrectly suggest sizeof(*i) here.
// FIXME: alternatively, we could suggest sizeof(i[0])
auto sz = sizeof(*i.get());

std::shared_ptr<Inner[]> s;
// The array specialization does not have operator->() either
s.get()->getValue();

bool b1 = !s.get();
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant get() call
// CHECK-FIXES: bool b1 = !s;

if (s.get()) {}
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
// CHECK-FIXES: if (s) {}

int x = s.get() ? 1 : 2;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant get() call
// CHECK-FIXES: int x = s ? 1 : 2;

bool b2 = s.get() == nullptr;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
// CHECK-FIXES: bool b2 = s == nullptr;
}
Loading