https://github.com/akshaykumars614 updated https://github.com/llvm/llvm-project/pull/100177
>From 642e558be0a1362f2b5cc4180dab7729f0ba8528 Mon Sep 17 00:00:00 2001 From: akshaykumars614 <akshaykumars...@gmail.com> Date: Tue, 23 Jul 2024 14:32:24 -0400 Subject: [PATCH 1/3] 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()); + } +} >From 0421519378d819ce9c872efefb71096caad744eb Mon Sep 17 00:00:00 2001 From: akshaykumars614 <akshaykumars...@gmail.com> Date: Tue, 23 Jul 2024 15:45:27 -0400 Subject: [PATCH 2/3] clang-tidy: readability-redundant-smartptr-get does not remove (#97964) updated Release Notes --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 374e89b8226a6..48eae358075ff 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -500,7 +500,7 @@ Changes in existing checks - Improved :doc:`readability-redundant-smartptr-get <clang-tidy/checks/readability/redundant-smartptr-get>` check to - remove '->' when reduntant get() is removed. + remove '->', when reduntant get() is removed. - Improved :doc:`readability-static-accessed-through-instance <clang-tidy/checks/readability/static-accessed-through-instance>` check to >From f2afb94f5299dfb02d8b3acb4689aba888d9b04b Mon Sep 17 00:00:00 2001 From: akshaykumars614 <akshaykumars...@gmail.com> Date: Tue, 23 Jul 2024 17:30:02 -0400 Subject: [PATCH 3/3] clang-tidy: readability-redundant-smartptr-get does not remove (#97964) simplified testcase --- .../readability/redundant-smartptr-get.cpp | 26 +------------------ 1 file changed, 1 insertion(+), 25 deletions(-) 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 e9ecc06d53959..ec4ca4cb79484 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 @@ -28,18 +28,7 @@ struct vector { 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; } - }; + using iterator = T*; iterator begin(); iterator end(); @@ -48,19 +37,6 @@ struct vector { 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 { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits