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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

FabianWolff
Copy link
Member

Currently 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());
}
<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.

@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: None (FabianWolff)

Changes

Currently we generate an incorrect suggestion for shared/unique pointers to arrays; for instance (Godbolt):

#include &lt;memory&gt;

void test_shared_ptr_to_array() {
  std::shared_ptr&lt;int[]&gt; i;
  auto s = sizeof(*i.get());
}
&lt;source&gt;: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.


Full diff: https://github.com/llvm/llvm-project/pull/141092.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp (+4-1)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get-msvc.cpp (+16)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp (+15)
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());
+}

@FabianWolff FabianWolff requested a review from kadircet May 22, 2025 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants