-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: None (FabianWolff) ChangesCurrently we generate an incorrect suggestion for shared/unique pointers to arrays; for instance (Godbolt): #include <memory>
void test_shared_ptr_to_array() {
std::shared_ptr<int[]> i;
auto s = sizeof(*i.get());
}
Full diff: https://github.com/llvm/llvm-project/pull/141092.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
index be52af77ae0a5..a31489a388fff 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
@@ -44,7 +44,10 @@ internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) {
}
internal::Matcher<Decl> knownSmartptr() {
- return recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr"));
+ return recordDecl(
+ hasAnyName("::std::unique_ptr", "::std::shared_ptr"),
+ unless(anyOf(has(cxxMethodDecl(hasName("operator[]"))),
+ has(functionTemplateDecl(hasName("operator[]"))))));
}
void registerMatchersForGetArrowStart(MatchFinder *Finder,
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get-msvc.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get-msvc.cpp
index bd8990a27b263..3b8e474c6fe8f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get-msvc.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get-msvc.cpp
@@ -26,6 +26,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 {
@@ -92,3 +100,11 @@ void Positive() {
// CHECK-MESSAGES: if (NULL == x.get());
// CHECK-FIXES: if (NULL == x);
}
+
+void test_shared_ptr_to_array() {
+ std::shared_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 s = sizeof(*i.get());
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp
index ec4ca4cb79484..6abe5d7e4e123 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp
@@ -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;
@@ -283,3 +290,11 @@ void test_redundant_get_with_member() {
// CHECK-FIXES: f(**i->get()->getValue());
}
}
+
+void test_unique_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 s = sizeof(*i.get());
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks a lot for taking a lookg here!
clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
Outdated
Show resolved
Hide resolved
3ed915b
to
3ef9135
Compare
clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
Outdated
Show resolved
Hide resolved
You can test this locally with the following command:git-clang-format --diff HEAD~1 HEAD --extensions cpp -- clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get-msvc.cpp clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp View the diff from clang-format here.diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
index baa977750..9774d93ff 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
@@ -66,8 +66,8 @@ void registerMatchersForGetArrowStart(MatchFinder *Finder,
// Make sure we are not missing the known standard types.
const auto SmartptrAny = anyOf(knownSmartptr(), QuacksLikeASmartptr);
- const auto SmartptrWithDeref =
- anyOf(cxxRecordDecl(knownSmartptr(), HasRelevantOps), QuacksLikeASmartptr);
+ const auto SmartptrWithDeref = anyOf(
+ cxxRecordDecl(knownSmartptr(), HasRelevantOps), QuacksLikeASmartptr);
// Catch 'ptr.get()->Foo()'
Finder->addMatcher(
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks a lot!
@kadircet, I think we generally shouldn't merge code that doesn't pass Also, an entry in ReleaseNotes.rst about this change is missing. (Did not see it at first look, sorry) |
Currently we generate an incorrect suggestion for shared/unique pointers to arrays; for instance (Godbolt):
sizeof(*i)
is incorrect, though, because the array specialization ofstd::shared/unique_ptr
does not have anoperator*()
. 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.