Skip to content
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

clang-tidy: readability-redundant-smartptr-get does not remove -> (#97964) #98757

Closed
wants to merge 7 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 11 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ 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/readability-redundant-smartptr-get>` check to
improves code readability and efficiency by removing unnecessary
dereferences.

- 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
Expand All @@ -513,6 +518,12 @@ Changes in existing checks
usages of ``std::string_view::compare``. Added a `StringLikeClasses` option
to detect usages of ``compare`` method in custom string-like classes.

- Improved :doc:`readability-redundant-smartptr-get
<clang-tidy/checks/readability/readability-redundant-smartptr-get>` identify
PiotrZSL marked this conversation as resolved.
Show resolved Hide resolved
and fix redundant calls to the `get()` method on smart pointers. This
improves code readability and efficiency by removing unnecessary
dereferences.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t


#define NULL __null

namespace std {
Expand All @@ -20,6 +21,46 @@ 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 {
Expand Down Expand Up @@ -235,3 +276,13 @@ 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());
PiotrZSL marked this conversation as resolved.
Show resolved Hide resolved
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
// CHECK-FIXES: f(**i);
}
}
Loading