[clang-tools-extra] clang-tidy: readability-redundant-smartptr-get does not remove (#97964) (PR #100177)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 23 11:37:47 PDT 2024


https://github.com/akshaykumars614 created https://github.com/llvm/llvm-project/pull/100177

added a check to remove '->' if exists

added testcase and modified Release Notes

pull request in continue of https://github.com/llvm/llvm-project/pull/98757

>From 642e558be0a1362f2b5cc4180dab7729f0ba8528 Mon Sep 17 00:00:00 2001
From: akshaykumars614 <akshaykumars614 at gmail.com>
Date: Tue, 23 Jul 2024 14:32:24 -0400
Subject: [PATCH] clang-tidy: readability-redundant-smartptr-get does not
 remove (#97964)

added a check to remove '->' if exists

added testcase and modified Release Notes
---
 .../readability/RedundantSmartptrGetCheck.cpp |  4 ++
 clang-tools-extra/docs/ReleaseNotes.rst       |  4 ++
 .../readability/redundant-smartptr-get.cpp    | 72 +++++++++++++++++++
 3 files changed, 80 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
index 8837ac16e8828..be52af77ae0a5 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
@@ -164,6 +164,10 @@ void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
   StringRef SmartptrText = Lexer::getSourceText(
       CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
       *Result.SourceManager, getLangOpts());
+  // Check if the last two characters are "->" and remove them
+  if (SmartptrText.ends_with("->")) {
+    SmartptrText = SmartptrText.drop_back(2);
+  }
   // Replace foo->get() with *foo, and foo.get() with foo.
   std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str();
   diag(GetCall->getBeginLoc(), "redundant get() call on smart pointer")
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a23483e6df6d2..374e89b8226a6 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -498,6 +498,10 @@ Changes in existing checks
   false-positives when type of the member does not match the type of the
   initializer.
 
+- Improved :doc:`readability-redundant-smartptr-get
+  <clang-tidy/checks/readability/redundant-smartptr-get>` check to
+  remove '->' when reduntant get() is removed.
+
 - Improved :doc:`readability-static-accessed-through-instance
   <clang-tidy/checks/readability/static-accessed-through-instance>` check to
   support calls to overloaded operators as base expression and provide fixes to
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 01f12b6bfe6ea..e9ecc06d53959 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
@@ -20,6 +20,47 @@ struct shared_ptr {
   explicit operator bool() const noexcept;
 };
 
+template <typename T>
+struct vector {
+  vector();
+  bool operator==(const vector<T>& other) const;
+  bool operator!=(const vector<T>& other) const;
+  unsigned long size() const;
+  bool empty() const;
+
+  // Basic iterator implementation for testing
+  struct iterator {
+    T* ptr;
+    iterator(T* p) : ptr(p) {}
+    T& operator*() { return *ptr; }
+    T* operator->() { return ptr; }
+    iterator& operator++() {
+      ++ptr;
+      return *this;
+    }
+    bool operator!=(const iterator& other) const { return ptr != other.ptr; }
+  };
+
+  iterator begin();
+  iterator end();
+
+  T* data;
+  unsigned long sz;
+};
+
+template <typename T>
+vector<T>::vector() : data(nullptr), sz(0) {}
+
+template <typename T>
+typename vector<T>::iterator vector<T>::begin() {
+  return iterator(data);
+}
+
+template <typename T>
+typename vector<T>::iterator vector<T>::end() {
+  return iterator(data + sz);
+}
+
 }  // namespace std
 
 struct Bar {
@@ -235,3 +276,34 @@ void Negative() {
   if (MACRO(x) == nullptr)
     ;
 }
+
+void test_redundant_get() {
+  std::vector<std::shared_ptr<int>> v;
+  auto f = [](int) {};
+  for (auto i = v.begin(); i != v.end(); ++i) {
+    f(*i->get());
+    // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
+    // CHECK-FIXES: f(**i);
+  }
+}
+
+struct Inner {
+  int a;
+  int *getValue()  { return &a; }
+};
+
+struct Example {
+  Inner inner;
+  Inner* get() { return &inner; }
+  int *getValue()  { return inner.getValue(); }
+};
+
+void test_redundant_get_with_member() {
+  std::vector<std::shared_ptr<Example>> v;
+  auto f = [](int) {};
+  for (auto i = v.begin(); i != v.end(); ++i) {
+    f(*(*i).get()->get()->getValue());
+    // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
+    // CHECK-FIXES: f(**i->get()->getValue());
+ }
+}



More information about the cfe-commits mailing list